Slack Web API Client for GAS(Google Apps Script)
TypeScript で書かれた Google Apps Script 用の Slack Web API 用のライブラリ です。
- シンプルで使いやすい Slack メッセージング機能
- チャンネルへのメッセージ送信
- リッチなメッセージフォーマット(アタッチメント)のサポート
- エラーハンドリングとリトライ機能
- Apps Script プロジェクトで、「エディタ」→「ライブラリ」を選択
- スクリプト ID を入力:
1TygC_BEClAGRFhVkq66ZRjKkXd1lSXKgPwcyM-0ruwUPU9K8_PAMl9FT - 最新バージョンを選択して「追加」
appsscript.jsonに以下の設定を追加:
{ "timeZone": "Asia/Tokyo", "dependencies": { "libraries": [ { "userSymbol": "SlackWebApi", "version": "0", "libraryId": "1TygC_BEClAGRFhVkq66ZRjKkXd1lSXKgPwcyM-0ruwUPU9K8_PAMl9FT", "developmentMode": true } ] }, "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8" }- TypeScript用の型定義ファイル
types/apps-slack-web-api.d.tsを自分のプロジェクトのsrc/types/apps-slack-web-api.d.tsなどにコピーする
function sendSimpleMessage() { const token = 'xoxb-your-token'; const channelId = '#general'; const text = 'Hello from Google Apps Script!'; SlackWebApi.postChatMessage(token, channelId, text); }function sendRichMessage() { const token = 'xoxb-your-token'; const channelId = '#general'; const attachmentData = [ { color: '#36a64f', pretext: 'Optional pretext that appears above the attachment', author_name: 'Author Name', author_link: 'http://example.com', title: 'Attachment Title', title_link: 'http://example.com', text: 'Main attachment text that can include *markdown*', fields: [ { title: 'Field Title', value: 'Field value and formatting', short: true, }, ], footer: 'Footer text', ts: Date.now() / 1000, }, ]; SlackWebApi.postChatMessage( token, channelId, 'Message with rich formatting', { attachments: JSON.stringify(attachmentData), } ); }function useWebClient() { const token = 'xoxb-your-token'; const web = SlackWebApi.createWebClient(token); const response = web.chat.postMessage({ channel: '#general', text: 'Hello!', }); }プルリクエストや Issue は大歓迎です。
