npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-chat-webhook

v1.0.0

Published

An SDK for Google Chat webhooks

Downloads

2,643

Readme

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

2. API

  • Sending text message
await client.sendText("This is a basic text thread.");
  • Formatting text helper
// ~_*block struck italic text*_~
const formatted = client.getFormattedMarkup(`bold struck italic text`, {
  bold: true,
  italic: true,
  strikethrough: true,
});

// `some inline code`
const monospace = client.getFormattedMarkup(`some inline code`, { monospace: true });

// ```
// multi
// line
// code
// ```
const monospaceBlock = client.getFormattedMarkup(`multi\nline\ncode`, { monospaceBlock: true });
  • Formatting mention helper
// <users/all>
const AT_ALL = client.getMentionMarkup(MentionType.ALL);
// <users/sample-user-id>
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
// <https://sample.com/|Sample Website>
const link = client.getLinkMarkup(`https://sample.com/`, `Sample Website`);
// <https://sample.com/|https://sample.com/>
const link = client.getLinkMarkup(`https://sample.com/`);
  • Sending card message
    See sample below.
const card: CardMessage = { ... };
await googleChat.sendCard(card);

3. Sample

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
 * ```
 */
const simpleMessage: GoogleChatWebhook.SimpleTextMessage = `*Bold text*\n\n`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`,
          subtitle: `Fresh inspiration every day`,
          imageUrl: `https://www.appgefahren.de/wp-content/uploads/2020/01/unsplash-icon.jpg`,
          imageStyle: CardImageStyle.AVATAR,
        },
        sections: [
          {
            widgets: [
              {
                image: {
                  imageUrl: `https://images.unsplash.com/photo-1541960071727-c531398e7494?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80`,
                },
              },
              {
                buttons: [
                  {
                    imageButton: {
                      icon: BuiltInIcon.BOOKMARK,
                      onClick: {
                        openLink: { url: `https://unsplash.com/photos/wxWulfjN-G0/download?force=true&w=640` },
                      },
                    },
                  },
                  {
                    textButton: {
                      text: `Explore more...`,
                      onClick: {
                        openLink: { url: `https://unsplash.com/` },
                      },
                    },
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  };
  await googleChat.sendCard(card);

Sources

Google Chat API documentation: