Skip to content

Instantly share code, notes, and snippets.

@K1-Style
Last active December 17, 2021 23:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K1-Style/e77406c90b57860de4a7b41721728c94 to your computer and use it in GitHub Desktop.
Save K1-Style/e77406c90b57860de4a7b41721728c94 to your computer and use it in GitHub Desktop.
Slack のあるチャンネルで今日発言があった分を拾うスクリプト (go)
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(&param)
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)
}
}
@K1-Style
Copy link
Author

SLACK_GET_HISTORY_TOKEN に Slack App のユーザートークンを、
SLACK_GET_HISTORY_CHANNEL_ID に対象チャンネルのIDを環境変数で設定しておく

@K1-Style
Copy link
Author

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