2
3

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 1 year has passed since last update.

Line Works Api 2.0を使ってGoogleスプレッドシートでGasでBot通知

Last updated at Posted at 2023-08-05
function sendLine(text, channelId) {
  
  //LINE WORKS APIのリクエストURLを定義 (下の XXX… を Bot ID に、YYY… をトークルームのチャンネルID に変更する)
  const url = `https://www.worksapis.com/v1.0/bots/id/channels/${channelId}/messages`
  
  //LINE WORKSへ接続するための基本設定 (下の XXX… をそれぞれの値に変更する)
  const config = {
    SCOPE: 'bot,bot.read',
    CLIENT_ID: '',
    CLIENT_SECRET: '',
    SERVICE_ACCOUNT: '',
    PRIVATE_KEY: ''
  }


  //LINE WORKS API の Access Token を取得する
  const accessToken = getAccessToken(config).access_token;

        //LINE WORKS へ Post するデータを作成する
        const options = {
          method: 'post',
          headers: {
            'Authorization': `Bearer ${accessToken}`,
            'Content-Type': 'application/json'
          },
          payload: JSON.stringify({
            'content': {
            'type': 'text',
            'text': text
            }
          })
        }
      
        // LINE WORKS へ Post する
        const response = UrlFetchApp.fetch(url, options);
}

//LINE WORKS API から Access Token を取得する関数
function getAccessToken(config) {
  const time = Date.now();
  const header = Utilities.base64Encode(JSON.stringify({ 'alg': 'RS256', 'typ': 'JWT' }));
  const claimSet = Utilities.base64Encode(JSON.stringify({
    'iss': config.CLIENT_ID,
    'sub': config.SERVICE_ACCOUNT,
    'iat': Math.floor(time / 1000),
    'exp': Math.floor(time / 1000 + 3600)
  }));
  const signature = Utilities.base64Encode(Utilities.computeRsaSha256Signature(`${header}.${claimSet}`, config.PRIVATE_KEY));
  const jwt = `${header}.${claimSet}.${signature}`;

  const endpoint = 'https://auth.worksmobile.com/oauth2/v2.0/token';
  const options = {
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    payload: {
      'assertion': jwt,
      'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      'client_id': config.CLIENT_ID,
      'client_secret': config.CLIENT_SECRET,
      'scope': config.SCOPE
    }
  }
  return JSON.parse(UrlFetchApp.fetch(endpoint, options));
}


2
3
2

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?