ガスと スラックAPIの 会話.歴史でチャンネル IDから内部のメッセージリストを取得する
前提
前回の記事でチャンネルリストを取得できた
今度はチャンネルの中身のメッセージたちを取得したい.
スラックAPIを読んでチャンネルの中のメッセージの取得方法を確認
https://api.slack.com/messaging/retrieving#conversations
公式によると
GET https://slack.com/api/conversations.history
Authorization: Bearer xoxb-your-token
{
channel: "CONVERSATION_ID_HERE"
}
会話.歴史から ゲット で{ channel :"ConversationRange id "
を ペイロードで渡せば取れるっぽい
APIリクエストの共通ラッパーを作る
前回の記事の
https://qiita.com/seratch/items/2158cb0abed5b8e12809
セラテックさんの APIラッパーを参考にして
function callWebApi(token, apiMethod, payload) {
const response = UrlFetchApp.fetch(
`https://www.slack.com/api/${apiMethod}`,
{
method: "post",
contentType: "application/x-www-form-urlencoded",
headers: { "Authorization": `Bearer ${token}` },
payload: payload,
}
);
console.log(`Web API (${apiMethod}) response: ${response}`)
return response;
}
APIリクエストのラッパーを 会話。高位と チャンネルで呼び出す
これに
const apiResponse = callWebApi(
token,
"conversations.history",
{ channel: "C0385KDLRD5"},
);
会話.歴史を渡して納付金として 前回の記事で 会話.リストから所得した
ボット作りチャンネルの ID C 0385 KDLRD 5 を入れて実行すると
Web API (conversations.history) response: {"ok":true,"messages":[{"type":"message","subtype":"channel_join","ts":"1648155092.889729","user":"U038DHKP87Q","text":"<@U038DHKP87Q> has joined the channel"}],"has_more":false,"pin_count":0,"channel_actions_ts":null,"channel_actions_count":0}
ここのチャンネルメッセージたちが取得できた.
https://qiita.com/seratch/items/2158cb0abed5b8e12809#web-api-%E3%81%AF%E3%82%B7%E3%83%B3%E3%83%97%E3%83%AB%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E4%BD%BF%E3%81%84%E3%81%BE%E3%81%97%E3%82%87%E3%81%86
この記事では 「ランダム」とチャンネル名を渡すように書いてあるが、
これでは動かなかった.
Web API (conversations.history) response: {"ok":false,"error":"channel_not_found"}
一般指定での実行結果の詳細を見てみる
前回 会話.リストを実行するして
{"id":"C038NHHFN3E","name":"general",...},
一般の IDが取得できた.この 一般の タグ:C 038 NHHN 3 Eで実行してみる
Web API (conversations.history) response: {"ok":true,"messages":[
すると、会話.リストのような出だしになるが{
"type":"message",
"subtype":"channel_purpose",
"ts":"1648513399.644649",
"user":"U038DHKP87Q",
"text":"set the channel description: "Edited 03-29\","
"purpose":"Edited 03-29"
},
{
"client_msg_id":"489452e3-72bb-4276-86da-b41a36ab3bb7",
"type":"message",
"text":"09:47 text",
"user":"U038DHKP87Q",
"ts":"1648169260.574219",
"team":"T038NHHEJJY",
"blocks":[{"type":"rich_text","block_id":"PA+","elements":[{"type":"rich_text_section","elements":[{"type":"text","text":"09:47 text"}]}]}]
},
{
"client_msg_id":"98fd1e24-2b1a-4028-a6d8-4d79980c702c",
"type":"message",
"text":"3rd message",
"user":"U038DHKP87Q",
"ts":"1648168642.874689",
"team":"T038NHHEJJY",
"blocks":[{"type":"rich_text","block_id":"2cN","elements":[{"type":"rich_text_section","elements":[{"type":"text","text":"3rd message"}]}]}]},
{
"type":"message",
"subtype":"channel_join",
"ts":"1648154999.666459",
"user":"U038DHKP87Q",
"text":"<@U038DHKP87Q> has joined the channel"}],
"has_more":false,
"pin_count":0,
"channel_actions_ts":null,
"channel_actions_count":0
}
チャネリング と言うチャンネルの説明の変更、チャネラムと言うチャンネルの参加者の通知、
などの サブタイプがあるシステムメッセージや
IDを指定します. があり テキストの他に
同じ内容の リッチテキストのブロックがある
通常のユーザーが投稿したメッセージが
これらが新しい順に並べられる.
結果を JSONでパースする
https://zenn.dev/nariakiiwatani/articles/8ed4a7bb0d5d0b
この 岩谷成明さんの記事を参考にして
const JSONParsedResponse = JSON.parse(response)
console.log(`apiMethod: (${apiMethod})`)
console.log(`JSONParsedResponse`)
console.log(JSONParsedResponse)
JSON応答するを使ってみると{ ok: true,
messages:
[ { type: 'message',
subtype: 'channel_purpose',
ts: '1648513399.644649',
user: 'U038DHKP87Q',
text: 'set the channel description: "Edited 03-29',"
purpose: 'Edited 03-29' },
{ client_msg_id: '489452e3-72bb-4276-86da-b41a36ab3bb7',
type: 'message',
text: '09:47 text',
user: 'U038DHKP87Q',
ts: '1648169260.574219',
team: 'T038NHHEJJY',
blocks: [Object] },
{ client_msg_id: '98fd1e24-2b1a-4028-a6d8-4d79980c702c',
type: 'message',
text: '3rd message',
user: 'U038DHKP87Q',
ts: '1648168642.874689',
team: 'T038NHHEJJY',
blocks: [Object] },
恐ろしく美しく出力された.しかし、ブロックは省略されてしまった.
配列になっているので、これに
console.log(JSONParsedResponse.messages[2])
中身のメッセージ配列の 2 つめを指定すると{ client_msg_id: '98fd1e24-2b1a-4028-a6d8-4d79980c702c',
type: 'message',
text: '3rd message',
user: 'U038DHKP87Q',
ts: '1648168642.874689',
team: 'T038NHHEJJY',
blocks: [ { type: 'rich_text', block_id: '2cN', elements: [Object] } ] }
最新から 2つめのメッセージのみが表示されたメッセージ配列の指定のものだけを取った配列を作る
const messages = JSONParsedResponse.messages
newMessages = messages.map(function (v) {
return [
v.client_msg_id,
v.type,
v.text,
v.user,
v.ts,
v.reply_count || 0,
v.reply_users_count || 0,
];
});
console.log(newMessages)
[ [ undefined,
'message',
'set the channel description: Edited 03-29',
'U038DHKP87Q',
'1648513399.644649',
0,
0 ],
[ '489452e3-72bb-4276-86da-b41a36ab3bb7',
'message',
'09:47 text',
'U038DHKP87Q',
'1648169260.574219',
0,
0 ],
これで JSONオブジェクトから CSVにちかい配列になったのでシートに書き出しやすくなった
次回
https://zenn.dev/nariakiiwatani/articles/8ed4a7bb0d5d0b
この記事に
パーソンJSONして マップして 逆して
必要な ID ts ( timestamp ?)text , user ,
のみを配列にして
スプレッドシートアプリの Getrangeと 設定値で埋め込む方法が書いてあった
これを試してみる
Reference
この問題について(ガスと スラックAPIの 会話.歴史でチャンネル IDから内部のメッセージリストを取得する), 我々は、より多くの情報をここで見つけました https://dev.to/kaede_io/gas-to-slack-api-no-conversationshistory-detiyanneru-id-karazhong-shen-woqu-de-suru-1cdaテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol