Hangouts Chat alerts & notifications... with asynchronous messages
von Satoshi Nakamoto

Posted by Wesley Chun (@wescpy), Developer Advocate, G Suite
Whereas most chatbots reply to person requests in a synchronous manner, there are eventualities when bots do not carry out actions based mostly on an specific person request, comparable to for alerts or notifications. In at present's DevByte video, I will present you find out how to ship messages asynchronously to rooms or direct messages (DMs) in Hangouts Chat, the group collaboration and communication software in G Suite.
What involves thoughts once you consider a bot in a chat room? Maybe a person desires the final quarter's European gross sales numbers, or possibly, they wish to search for native climate or the subsequent film showtime. Assuming there is a bot for regardless of the request is, a person will both ship a direct message (DM) to that bot or @point out the bot from inside a chat room. The bot then fields the request (despatched to it by the Hangouts Chat service), performs any obligatory magic, and responds again to the person in that "area," the generic nomenclature for a room or DM.
Our earlier DevByte video for the Hangouts Chat bot framework reveals builders what bots and the framework are all about in addition to find out how to construct considered one of some of these bots, in each Python and JavaScript. Nonetheless, acknowledge that these bots are responding synchronously to a person request. This does not suffice when customers wish to be notified when a long-running background job has accomplished, when a late bus or practice can be arriving quickly, or when considered one of their servers has simply gone down. Acknowledge that such alerts can come from a bot but in addition maybe a monitoring software. Within the newest episode of the G Suite Dev Present, learn to combine this performance in both kind of software.
From the video, you possibly can see that alerts and notifications are "out-of-band" messages, that means they'll are available in at any time. The Hangouts Chat bot framework supplies a number of methods to ship asynchronous messages to a room or DM, generically known as a "area." The primary is the HTTP-based REST API. The opposite manner is utilizing what are generally known as "incoming webhooks."
The REST API is utilized by bots to ship messages into an area. Since a bot won't ever be a human person, a Google service account is required. When you create a service account to your Hangouts Chat bot within the builders console, you possibly can obtain its credentials wanted to speak with the API. Beneath is a brief Python pattern snippet that makes use of the API to ship a message asynchronously to an area.
from apiclient import discovery
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = 'https://www.googleapis.com/auth/chat.bot'
creds = ServiceAccountCredentials.from_json_keyfile_name(
'svc_acct.json', SCOPES)
CHAT = discovery.construct('chat', 'v1', http=creds.authorize(Http()))
room = 'areas/'
message = 'textual content': 'Hiya world!'
CHAT.areas().messages().create(dad or mum=room, physique=message).execute()
The choice to utilizing the API with service accounts is the idea of incoming webhooks. Webhooks are a fast and straightforward option to ship messages into any room or DM with out configuring a full bot, i.e., monitoring apps. Webhooks additionally permit you to combine your customized workflows, comparable to when a brand new buyer is added to the company CRM (buyer relationship administration system), in addition to others talked about above. Beneath is a Python snippet that makes use of an incoming webhook to speak into an area asynchronously.
import requests
import json
URL = 'https://chat.googleapis.com/...&thread_key=T12345'
message = 'textual content': 'Hiya world!'
requests.submit(URL, information=json.dumps(message))
Since incoming webhooks are merely endpoints you HTTP POST to, you possibly can even use curl to ship a message to a Hangouts Chat area from the command-line:
curl
-X POST
-H 'Content material-Kind: software/json'
'https://chat.googleapis.com/...&thread_key=T12345'
-d '"textual content": "Hiya!"'
To get began, check out the Hangouts Chat developer documentation, particularly the particular pages linked to above. We hope this video helps you are taking your bot growth expertise to the subsequent degree by exhibiting you find out how to ship messages to the Hangouts Chat service asynchronously.
Read the full article
Satoshi Nakamoto
Keine Verbindung
Verbindung wird wiederhergestellt
Etwas ist schiefgelaufen
Wir sind gleich wieder da