项目作者: bevelop-official

项目描述 :
Node SDK for Google Chat webhook integration. Automate notifications from your node app to Google Chat.
高级语言: TypeScript
项目地址: git://github.com/bevelop-official/google-chat-webhook.git
创建时间: 2020-04-15T19:06:10Z
项目社区:https://github.com/bevelop-official/google-chat-webhook

开源协议:GNU General Public License v3.0

下载


google-chat-webhook

An 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

Table of contents

  1. Getting started
  2. API
  3. Sample
  4. Sources

1. Getting started

  • 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
  1. WEBHOOK_URL=${YOUR_WEBHOOK_URL_HERE} npm run sample
  • Explore the sample code in ./sample/index

2. API

  • Sending text message
  1. await client.sendText("This is a basic text thread.");
  • Formatting text helper
  1. // ~_*block struck italic text*_~
  2. const formatted = client.getFormattedMarkup(`bold struck italic text`, {
  3. bold: true,
  4. italic: true,
  5. strikethrough: true,
  6. });
  7. // `some inline code`
  8. const monospace = client.getFormattedMarkup(`some inline code`, { monospace: true });
  9. // ```
  10. // multi
  11. // line
  12. // code
  13. // ```
  14. const monospaceBlock = client.getFormattedMarkup(`multi\nline\ncode`, { monospaceBlock: true });
  • Formatting mention helper
  1. // <users/all>
  2. const AT_ALL = client.getMentionMarkup(MentionType.ALL);
  3. // <users/sample-user-id>
  4. const userSpecificMention = client.getMentionMarkup(MentionType.USER_SPECIFIC, `sample-user-id`);

Given you somehow know the users ID you may use the syntax described in ‘Messages that @mention specific users

  • Formatting link helper
  1. // <https://sample.com/|Sample Website>
  2. const link = client.getLinkMarkup(`https://sample.com/`, `Sample Website`);
  3. // <https://sample.com/|https://sample.com></https:>
  4. const link = client.getLinkMarkup(`https://sample.com/`);
  • Sending card message
    See sample below.
  1. const card: CardMessage = { ... };
  2. await googleChat.sendCard(card);

3. Sample

  1. import { GoogleChatWebhook } from "google-chat-webhook";
  2. const url = process.env.WEBHOOK_URL;
  3. if (!url) {
  4. throw new Error("Environment variable 'WEBHOOK_URL' must be set.");
  5. }
  6. const client = new GoogleChatWebhook({ url });
  7. /**
  8. * Send a simple text message to hangouts chat. Formatting is as
  9. * within the UI.
  10. * *bold text*
  11. * _italic text_
  12. * ~strike text~
  13. * `inline code`
  14. * ```
  15. * multi-line
  16. * code
  17. * ```
  18. */
  19. const simpleMessage: GoogleChatWebhook.SimpleTextMessage = `*Bold text*\n\n`inline-code`\n_Italic text_\nUnformatted text\n`;
  20. await client.sendTextMessage(simpleMessage);
  21. /**
  22. * Send a more complex card message.
  23. */
  24. const card: CardMessage = {
  25. cards: [
  26. {
  27. header: {
  28. title: `Unsplash daily bot`,
  29. subtitle: `Fresh inspiration every day`,
  30. imageUrl: `https://www.appgefahren.de/wp-content/uploads/2020/01/unsplash-icon.jpg`,
  31. imageStyle: CardImageStyle.AVATAR,
  32. },
  33. sections: [
  34. {
  35. widgets: [
  36. {
  37. image: {
  38. imageUrl: `https://images.unsplash.com/photo-1541960071727-c531398e7494?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80`,
  39. },
  40. },
  41. {
  42. buttons: [
  43. {
  44. imageButton: {
  45. icon: BuiltInIcon.BOOKMARK,
  46. onClick: {
  47. openLink: { url: `https://unsplash.com/photos/wxWulfjN-G0/download?force=true&w=640` },
  48. },
  49. },
  50. },
  51. {
  52. textButton: {
  53. text: `Explore more...`,
  54. onClick: {
  55. openLink: { url: `https://unsplash.com/` },
  56. },
  57. },
  58. },
  59. ],
  60. },
  61. ],
  62. },
  63. ],
  64. },
  65. ],
  66. };
  67. await googleChat.sendCard(card);

Sources

Google Chat API documentation: