An SDK for Google Chat webhooks
npm install google-chat-webhookAn SDK for Google Chats Incoming Webhooks. Enables you to notify Google Chat chatrooms with simple text message threads or high fidelity interactive card interfaces.
_A sample text message thread. Formatting can be done just as in user interface._
!Simple text thread
_A sample interactive card thread. Embed images, basic html text blocks, rows of buttons and more advanced keyValue widgets._
!Interactive card thread
_Other sample card with keyValue widget._
!Interactive card thread #2
1. Getting started
2. API
3. Sample
4. Sources
- Create or select an existing chatroom in Google Chat
- Via the settings button choose Configure Webhooks
- Create new webhook
- Copy webhooks new URL
- Run the sample
```
WEBHOOK_URL=${YOUR_WEBHOOK_URL_HERE} npm run sample
- Explore the sample code in ./sample/index
- Sending text message
`ts`
await client.sendText("This is a basic text thread.");
- Formatting text helper
``tsbold struck italic text
// ~_block struck italic text_~
const formatted = client.getFormattedMarkup(, {
bold: true,
italic: true,
strikethrough: true,
});
// some inline codesome inline code
const monospace = client.getFormattedMarkup(, { monospace: true });
// ``
// multi
// line
// code
// multi\nline\ncode
const monospaceBlock = client.getFormattedMarkup(, { monospaceBlock: true });``
- Formatting mention helper
`tssample-user-id
//
const AT_ALL = client.getMentionMarkup(MentionType.ALL);
//
const userSpecificMention = client.getMentionMarkup(MentionType.USER_SPECIFIC, );`
Given you somehow know the users ID you may use the syntax described in 'Messages that @mention specific users'
- Formatting link helper
`tshttps://sample.com/
//
const link = client.getLinkMarkup(, Sample Website);https://sample.com/
//
const link = client.getLinkMarkup();`
- Sending card message
See sample below.
`ts
const card: CardMessage = { ... };
await googleChat.sendCard(card);
`
``ts
import { GoogleChatWebhook } from "google-chat-webhook";
const url = process.env.WEBHOOK_URL;
if (!url) {
throw new Error("Environment variable 'WEBHOOK_URL' must be set.");
}
const client = new GoogleChatWebhook({ url });
/**
* Send a simple text message to hangouts chat. Formatting is as
* within the UI.
bold text*
* _italic text_
* ~strike text~
* inline code`
* `
* multi-line
* code
* Bold text\n\n
*/
const simpleMessage: GoogleChatWebhook.SimpleTextMessage = inline-code\n_Italic text_\nUnformatted text\n;
await client.sendTextMessage(simpleMessage);
/**
* Send a more complex card message.
*/
const card: CardMessage = {
cards: [
{
header: {
title: Unsplash daily bot,Fresh inspiration every day
subtitle: ,https://www.appgefahren.de/wp-content/uploads/2020/01/unsplash-icon.jpg
imageUrl: ,https://images.unsplash.com/photo-1541960071727-c531398e7494?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80
imageStyle: CardImageStyle.AVATAR,
},
sections: [
{
widgets: [
{
image: {
imageUrl: ,https://unsplash.com/photos/wxWulfjN-G0/download?force=true&w=640
},
},
{
buttons: [
{
imageButton: {
icon: BuiltInIcon.BOOKMARK,
onClick: {
openLink: { url: },Explore more...
},
},
},
{
textButton: {
text: ,https://unsplash.com/
onClick: {
openLink: { url: },```
},
},
},
],
},
],
},
],
},
],
};
await googleChat.sendCard(card);
Google Chat API documentation:
- Use incoming webhooks
- Card formatting messages
- Simple text messages