LoginSignup
7
4

More than 5 years have passed since last update.

GASでRocket.Chatに投稿する

Posted at

Rocket.Chatに投稿する機会があったのでメモ

Rocket.Chatの設定

投稿するチャネルの作成

  • チャネルの種別
    プライベートでも問題ない。

  • チャネル名
    「_」は「-」になってしまったり、なんか変な挙動するので、できれば使用しない方が安全っぽい。

サービス連携の設定

002.PNG

003.png

004.png

名前とチャネルとユーザーを登録
※有効にし忘れない!

005.png

一番下のある「変更を保存」を押す。

006.png

「Webhook URL」と「Token」をメモ

007.png

コマンドラインで実行

curlの部分をコピーして、コマンドラインで実行してみる。


curl -X POST -H 'Content-Type: application/json' --data '{"text":"Example message"・・・

こんな感じに表示されていればOK
008.png

GASで実行


function myFunction(){
  var message = 'テスト :birthday: google apps script';
  sendRocketChat(message);
}


function sendRocketChat(message){
  var payload = {
    "text": message,
    "attachments": [
    {
      "title": "Rocket.Chat",
      "title_link": "https://rocket.chat",
      "text": "Rocket.Chat, the best open source chat",
      "image_url": "https://rocket.chat/images/mockup.png",
      "color": "#764FA5"
    }
  ]
  };

  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(payload)
  };

  var token = 'your token';
  var url = 'your webhook url' + token;

  UrlFetchApp.fetch(url, options);
}

↓ユーザー名とかは消しています
009.png

7
4
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
7
4