33
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GASからhangout chatにPostする

Last updated at Posted at 2018-03-09

#準備
##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

33
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?