hangouts-chat-webhook
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

hangouts-chat-webhook

Fluent api to send webhook to Hangouts chat

license npm Dependency Status devDependency Status

Installation

$ npm install --save hangouts-chat-webhook

Usage

We don't include any http client in order to let you choose your preferred one. Below is a simple example with superagent.

import * as superagent from "superagent";
import { Button, Card, CardHeader, Icon, Image, KeyValue, Message, OnClick, OpenLink, Section, TextButton, TextParagraph, WidgetMarkup } from "hangouts-chat-webhook";
 
const message: Message = new Message()
    .addCard(new Card()
        .setHeader(new CardHeader()
            .setTitle("Google Chat")
            .setSubtitle("Incoming Webhook")
        )
        .addSection(new Section()
            .addWidget(new WidgetMarkup()
                .setImage(new Image()
                    .setImageUrl("https://www.gstatic.com/images/branding/product/2x/chat_64dp.png")
                )
            )
            .addWidget(new WidgetMarkup()
                .setKeyValue(new KeyValue()
                    .setIcon(Icon.DESCRIPTION)
                    .setTopLabel("Key")
                    .setContent("Value")
                )
            )
            .addWidget(new WidgetMarkup()
                .setTextParagraph(new TextParagraph()
                    .setText("Incoming webhook")
                )
            )
            .addWidget(new WidgetMarkup()
                .addButton(new Button()
                    .setTextButton(new TextButton()
                        .setText("Open")
                        .setOnClick(new OnClick()
                            .setOpenLink(new OpenLink()
                                .setUrl("https://gsuite.google.com/products/chat/")
                            )
                        )
                    )
                )
            )
        )
    );
 
superagent.post("https://chat.googleapis.com/v1/spaces/...")
    .set("Content-Type", "application/json; charset=UTF-8")
    .send(message)
    .then((res: superagent.Response) => {
        console.log(res.status, res.body);
    })
    .catch((err: any) => {
        console.error(err.response && err.response.text ? JSON.parse(err.response.text) : err);
    })
;
 

Warning

The Api list is coming directly from Google Api, BUT a lot of method are present (ex: keyValue.setBottomLabel) but don't work with hangouts chat, resulting a response code 200 and a blank message. No way for developper to understand the reason.

Also, the Hangouts Chat message formats documentation is not complete and don't allow to clearly understand how to build message correctly.

You have to test directly on hangout chat in order to be sure that the message will work.

License

MIT © tchiotludo

Package Sidebar

Install

npm i hangouts-chat-webhook

Weekly Downloads

15

Version

0.1.1

License

MIT

Unpacked Size

88.3 kB

Total Files

108

Last publish

Collaborators

  • tchiotludo