Last active
December 17, 2021 23:16
-
-
Save K1-Style/e77406c90b57860de4a7b41721728c94 to your computer and use it in GitHub Desktop.
Slack のあるチャンネルで今日発言があった分を拾うスクリプト (go)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"strconv" | |
"github.com/slack-go/slack" | |
) | |
func main() { | |
// 現在時刻取得 | |
now := time.Now() | |
oldest := strconv.FormatInt(time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).UnixNano(), 10) | |
latest := strconv.FormatInt(time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, time.Local).UnixNano(), 10) | |
// Slack の conversation.history 実行 | |
api := slack.New(os.Getenv("SLACK_GET_HISTORY_TOKEN")) | |
param := slack.GetConversationHistoryParameters{ | |
ChannelID: os.Getenv("SLACK_GET_HISTORY_CHANNEL_ID"), | |
Oldest: oldest[:10] + "." + oldest[11:16], | |
Latest: latest[:10] + "." + latest[11:16], | |
} | |
history, err := api.GetConversationHistory(¶m) | |
if err != nil { | |
fmt.Printf("%s\n", err) | |
return | |
} | |
// 結果を見る | |
for i, m := range history.Messages { | |
fmt.Printf("[%d]timestamp:%s, user:%s, text:%s\n", i, m.Timestamp, m.User, m.Text) | |
} | |
} |
Slack App のユーザートークン
以下 conversations.history
に必要なスコープを持っていることが前提
https://api.slack.com/methods/conversations.history
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SLACK_GET_HISTORY_TOKEN
に Slack App のユーザートークンを、SLACK_GET_HISTORY_CHANNEL_ID
に対象チャンネルのIDを環境変数で設定しておく