Creating Hangouts Chat bots with Python

Introduction

Today, on Google I / O, the Hangouts Chat team (including yourself) gave a lecture on the structure of the bot for the Hangout Talk (see the video here [~ 40 min]). If you have lost a few months ago, Google has launched the new Hangouts chat application for G Suite customers (but not consumer Gmail users today). This next-generation collaboration platform has several important features that are not available in "classic hangouts," including the ability to create chat rooms, search and the ability to attach Google Drive files. However, for developers, the biggest new feature is a bot structure and an API, allowing developers to create bots that interact with users within "spaces," that is, chat rooms or direct messages (DM).

Before starting to use bots, we analyze the typical use of the API in which your application must be authorized to access data, for example, OAuth2. Once permission is granted, your application calls the API, the API answers the request and responds with the desired results, as shown here:


The bot architecture is very different. With bots, recognize that requests come from users in chat rooms or DMs. Users direct messages to a bot, and then the Hangouts chat broadcasts these requests to the bot. The bot executes all the necessary processing (possibly by calling other APIs and services), groups the response and returns it to the chat, which in turn shows the results for the users. Here is the workflow of the bot summarized:



One of the main conclusions of this discussion is that, normally, in your applications, you call the APIs. For bots, the Hangouts talk calls you. For this reason, the service is extremely flexible for developers: you can create bots using virtually any language (not just Python), using your stack of choice and hosted in any public or private cloud. The only requirement is that, since Hangouts chat can send HTTP POST to your bot, you are ready to use it.

Also, if you think there is something missing in the diagram, because you do not see the OAuth2 or the API calls, it would also be correct. Look in a future post where I concentrate on asynchronous responses from the Hangouts Chat bots.


Types of events and bot processing

So, how is the payload when your bot receives a call from the Hangouts chat? The first thing your bot must do is determine the type of event received. You can then process the request based on this type of event and finally return an appropriate JSON payload back to the Hangouts chat to do within the space. Currently, there are four types of events in the Hangouts conversation:

ADDED_TO_SPACE
REMOVED_FROM_SPACE
MESSAGE
CARD_CLICKED

The first pair is for when a bot is added or removed from a space. In the first case, the bot would probably send a welcome message like "Thanks for adding me to this room", and maybe give some instructions on how to communicate with the bot. When a bot is removed from a space, it can no longer communicate with the participants in the chat room, so there is no response message sent in that case; The most likely action here would be to record that the bot was removed from space.


The third type of message is probably the most common scenario, where a bot has been added to a room and now a human user is sending a request. The other common type would be the last one. Instead of a message, this event occurs when users click on an item in the user interface card. The job of the bot here is to invoke the "callback" associated with the element of the card pressed. Several things can happen here: the bot can return a text message (or nothing), a UI card can be updated or a new card can be returned.

Everything described above is done in the following pseudocode (if you choose to implement in Python or in any other supported language) and it is useful to prepare you to review the official documentation:
def process_event (req, rsp):
Event = json.loads (req ['body']) # event received
if event ['type'] == 'REMOVED_FROM_SPACE':
# unanswered as bot removed from the room
returns
elif event ['type'] == 'ADDED_TO_SPACE':
# bot added to the room; send welcome message
msg = {'text': 'Thanks for adding me to this room!'}
elif event ['type'] == 'MESSAGE':
# message received during normal operations
msg = respond_to_message (event ['message'] ['text'])
elif event ['type'] == 'CARD_CLICKED':
# user result click on the card's UI
action = event ['action']
msg = respond_to_click (
action ['actionMethodName'], action ['parameter '])
    else:
        return
    rsp.send(json.dumps(msg))

Interested in python online training

Regardless of implementation language, you still need to make the necessary tweaks to run on the hosting platform you choose. Our example uses Google App Engine (GAE) for hosting; below you'll see the obvious similarities between our actual bot app and this pseudocode. 

Comments

Popular posts from this blog

13 exclusive features of the programming language Python

Scope and career opportunities in Python

Python: 7 Important Reasons Why You Should Use Python