Creating asynchronous bot requests in Hangouts Chat

栏目: Python · 发布时间: 7年前

Source: Creating asynchronous bot requests in Hangouts Chat from Google Cloud

Hangouts Chat bots can do many things, including providing alerts or notifications—messages that are not dependent on users initiating the conversation. In our previous DevByte , we explained the Hangouts Chat bot framework, as well as how to build a bot that responds synchronously to a user message. Today, we walk you through how to build a bot that uses asynchronous requests to notify a user.

An asynchronous request is where a bot acts based on information from another application. This enables an internal service to message you when your long-running job completes, or when a server has just gone down. 

In this video, we demonstrate how alerts and notifications are “out-of-band” messages, which means they can come in at any time (and not only as a response to a user message). The Hangouts Chat bot framework provides two ways to send asynchronous messages to a room or as a direct message (DM): the HTTP-based REST API and “incoming webhooks.”

1. Using the HTTP-based REST API to deliver asynchronous messages

First, you’ll need a bot . Then, to enable that bot to send asynchronous messages, you’ll need to use the REST API . First, you have to create a Google service account , which essentially creates a special sort of account that allows the bot to perform special actions, like initiating messages.

Now that you’re set up, putting the pieces together is pretty easy. Here’s a brief example using Python that uses the API to asynchronously send a message to a space. To start, you setup the service account with your credentials. Then, initialize the bot, provide a space and message, and send it off.

2. Using webhooks to create quick entry points for asynchronous messages

If you have an existing application, such as a monitoring service or your Customer Relationship Management (CRM) system, that you want to enable to send alerts or notifications as Chat messages, you can also use incoming webhooks . This approach allows you to send messages into any space without configuring a full bot. Webhooks also allow you to integrate your custom workflows, so that your team, for example, can receive a message when a new customer is added to the CRM.

Incoming webhooks are simply endpoints that you send an HTTP POST request to, you can send messages to the space using whatever method you like.. To show you how, here are two examples.

First, a Python snippet that uses an incoming webhook to communicate into a space asynchronously. You’ll notice the message creation is simple, and all you need is the URL of that webhook to POST a request to.

To get started with asynchronous messages, take a look at the Hangouts Chat developer documentation , especially the specific pages linked to above. Or read more in this post . Happy botting!

除非特别声明,此文章内容采用 知识共享署名 3.0 许可,代码示例采用 Apache 2.0 许可。更多细节请查看我们的 服务条款

Tags: Cloud


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

掘金大数据

掘金大数据

程新洲、朱常波、晁昆 / 机械工业出版社 / 2019-1 / 59.00元

在数据横向融合的时代,充分挖掘数据金矿及盘活数据资产,是企业发展和转型的关键所在。电信运营商以其数据特殊性,必将成为大数据领域的领航者、生力军。各行业的大数据从业者要如何从电信业的大数据中挖掘价值呢? 本书彻底揭开电信运营商数据的神秘面纱,系统介绍了大数据的发展历程,主要的数据挖掘方法,电信运营商在网络运行及业务运营方面的数据资源特征,基于用户、业务、网络、终端及内在联系的电信运营商大数据分......一起来看看 《掘金大数据》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具