anonymous No title
No License JavaScript
2022年06月12日
Copy Clone
const 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));
}

const snedMessageToRoom = () => {
  const config = {
    SCOPE: 'bot,bot.read',
    CLIENT_ID: 'クライアントID', // ★
    CLIENT_SECRET: 'クライアントシークレット',// ★
    SERVICE_ACCOUNT: 'サービスアカウント',// ★
    PRIVATE_KEY: ` 
-----BEGIN PRIVATE KEY-----
asvdfvbas // ★
awfeawefa // ★
awefawefw // ★
-----END PRIVATE KEY-----
`
  }

  const accessToken = getAccessToken(config).access_token;
  const botId = 'ボットID'; // ★
  const channelId = 'チャンネルID'; // ★
  const url = `https://www.worksapis.com/v1.0/bots/${botId}/channels/${channelId}/messages`;
  const options = {
    method: 'post',
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify({
      'content': {
        'type': 'text',
        'text': 'hello'
      }
    })
  }

  const response = UrlFetchApp.fetch(url, options);
}

const 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));
}

const snedMessageToRoom = () => {
  const config = {
    SCOPE: 'bot,bot.read',
    CLIENT_ID: 'クライアントID', // ★
    CLIENT_SECRET: 'クライアントシークレット',// ★
    SERVICE_ACCOUNT: 'サービスアカウント',// ★
    PRIVATE_KEY: ` 
-----BEGIN PRIVATE KEY-----
asvdfvbas // ★
awfeawefa // ★
awefawefw // ★
-----END PRIVATE KEY-----
`
  }

  const accessToken = getAccessToken(config).access_token;
  const botId = 'ボットID'; // ★
  const channelId = 'チャンネルID'; // ★
  const url = `https://www.worksapis.com/v1.0/bots/${botId}/channels/${channelId}/messages`;
  const options = {
    method: 'post',
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify({
      'content': {
        'type': 'text',
        'text': 'hello'
      }
    })
  }

  const response = UrlFetchApp.fetch(url, options);
}

No one still commented. Please first comment.
Output