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


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

查看所有标签

猜你喜欢:

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

人月神话(英文版)

人月神话(英文版)

[美] Frederick P. Brooks, Jr. / 人民邮电出版社 / 2010-8 / 29.00元

本书内容源于作者Brooks在IBM公司任System/360计算机系列以及其庞大的软件系统OS/360项目经理时的实践经验。在本书中,Brooks为人们管理复杂项目提供了最具洞察力的见解,既有很多发人深省的观点,又有大量软件工程的实践,为每个复杂项目的管理者给出了自己的真知灼见。 大型编程项目深受由于人力划分产生的管理问题的困扰,保持产品本身的概念完整性是一个至关重要的需求。本书探索了达成......一起来看看 《人月神话(英文版)》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具