β

Developing bots for Hangouts Chat

谷歌中国开发者社区 (GDG) 3 阅读
for

Source: Developing bots for Hangouts Chat from Google Developer

Posted by Wesley Chun (@ wescpy ), Developer Advocate, G Suite

We recently introduced Hangouts Chat to general availability. This next-generation messaging platform gives G Suite users a new place to communicate and to collaborate in teams. It features archive & search, tighter G Suite integration, and the ability to create separate, threaded chat rooms. The key new feature for developers is a bot framework and API. Whether it’s to automate common tasks, query for information, or perform other heavy-lifting, bots can really transform the way we work.

In addition to plain text replies, Hangouts Chat can also display bot responses with richer user interfaces (UIs) called cards which can render header information, structured data, images, links, buttons, etc. Furthermore, users can interact with these components, potentially updating the displayed information. In this latest episode of the G Suite Dev Show , developers learn how to create a bot that features an updating interactive card.

As you can see in the video, the most important thing when bots receive a message is to determine the event type and take the appropriate action. For example, a bot will perform any desired “paperwork” when it is added to or removed from a room or direct message (DM), generically referred to as a “space” in the vernacular.

Receiving an ordinary message sent by users is the most likely scenario; most bots do “their thing” here in serving the request. The last event type occurs when a user clicks on an interactive card. Similar to receiving a standard message, a bot performs its requisite work, including possibly updating the card itself. Below is some pseudocode summarizing these four event types and represents what a bot would likely do depending on the event type:

function processEvent(req, rsp) {
  var event = req.body; // event type received
  var message;          // JSON response message

  if (event.type == 'REMOVED_FROM_SPACE') {
      // no response as bot removed from room
      return;

  } else if (event.type == 'ADDED_TO_SPACE') {
    // bot added to room; send welcome message
    message = {text: 'Thanks for adding me!'};

  } else if (event.type == 'MESSAGE') {
    // message received during normal operation
    message = responseForMsg(event.message.text);

  } else if (event.type == 'CARD_CLICKED') {
    // user-click on card UI
    var action = event.action;
    message = responseForClick(
        action.actionMethodName, action.parameters);
  }

  rsp.send(message);
};

The bot pseudocode as well as the bot featured in the video respond synchronously . Bots performing more time-consuming operations or those issuing out-of-band notifications, can send messages to spaces in an asynchronous way. This includes messages such as job-completed notifications, alerts if a server goes down, and pings to the Sales team when a new lead is added to the CRM (Customer Relationship Management) system.

Hangouts Chat supports more than JavaScript or Python and Google Apps Script or Google App Engine . While using JavaScript running on Apps Script is one of the quickest and simplest ways to get a bot online within your organization, it can easily be ported to Node.js for a wider variety of hosting options. Similarly, App Engine allows for more scalability and supports additional languages (Java, PHP, Go, and more) beyond Python. The bot can also be ported to Flask for more hosting options. One key takeaway is the flexibility of the platform: developers can use any language, any stack, or any cloud to create and host their bot implementations. Bots only need to be able to accept HTTP POST requests coming from the Hangouts Chat service to function.

At Google I/O 2018 last week, the Hangouts Chat team leads and I delivered a longer, higher-level overview of the bot framework . This comprehensive tour of the framework includes numerous live demos of sample bots as well as in a variety of languages and platforms. Check out our ~40-minute session below.

To help you get started, check out the bot framework launch post . Also take a look at this post for a deeper dive into the Python App Engine version of the vote bot featured in the video. To learn more about developing bots for Hangouts Chat, review the concepts guides as well as the “how to” for creating bots . You can build bots for your organization, your customers, or for the world. We look forward to all the exciting bots you’re going to build!

for
作者:谷歌中国开发者社区 (GDG)
Google Developer Groups
原文地址:Developing bots for Hangouts Chat, 感谢原作者分享。

发表评论