43
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

GASからhangout chatにPostする

準備

WebhookのURLを取得する

チャットルームでWebhookを設定しWebhookURLを取得します。

chat1.png
chat2.png

投稿する

リファレンスのサンプルはPythonで書かれていますがこれをGASで書きます。
https://developers.google.com/hangouts/chat/quickstart/incoming-bot-python#step_2_create_the_python_script

function myFunction() {
  var url = 'WebhookのURL';
  var botMessage = { 'text' : 'Hello from GoogleAppsScript!'}

  var options = {
    'method': 'POST',
    'headers' : {
      'Content-Type': 'application/json; charset=UTF-8'
    },
    'payload':JSON.stringify(botMessage)
  };

  var result = UrlFetchApp.fetch(url, options);
  Logger.log(result);
}

あとは実行するだけでwebhookを設定したチャットルームに投稿されます。
chat3.png

ちなみにwebhookはURLを知っていればG Suiteドメイン外からでも投稿できるので注意。

リファレンス

https://developers.google.com/hangouts/chat/how-tos/webhooks
https://developers.google.com/hangouts/chat/quickstart/incoming-bot-python

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
43
Help us understand the problem. What are the problem?