Open3

Chatwork API

satonesatone

GASで特定のルームのすべてのメッセージ(上限100)を取得する

const TOKEN = "自分のAPIトークン"

function getMessages(roomId) {
  const headers = {
    'X-ChatWorkToken': TOKEN //ChatworkのAPIトークン
  }

  const options = {
    'method' : 'get',
    "headers" : headers,
  }

  // force=1を付けた場合、過去のデータも取得される(デフォルトはforce=0)
  let response = UrlFetchApp.fetch('https://api.chatwork.com/v2/rooms/' + roomId + '/messages?force=1', options)
  
  return JSON.parse(response)
}
satonesatone

GASで特定のルームにメッセージを送信する

const TOKEN = "自分のAPIトークン"

function sendMessage(message, roomId) {
  const headers = {
    'X-ChatWorkToken': TOKEN //ChatworkのAPIトークン
  }

  const data = {
    'body': message
  }

  const options = {
    'method' : 'post',
    "headers" : headers,
    'payload' : data
  }

  UrlFetchApp.fetch('https://api.chatwork.com/v2/rooms/' + roomId + '/messages', options)
}