diff --git a/.gitignore b/.gitignore index e0039f07..6111a07e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ +.idea/ +env/ +*.txt + +# set token in os environment variable +set_token.sh + # Created by https://www.gitignore.io/api/go # Edit at https://www.gitignore.io/?templates=go diff --git a/getPastMessage.go b/getPastMessage.go new file mode 100644 index 00000000..685e32cb --- /dev/null +++ b/getPastMessage.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "os" + "reflect" + "strconv" + "time" + + "github.com/nlopes/slack" +) + +func main() { + api := slack.New(os.Getenv("SLACK_BOT_TOKEN")) + + today := time.Now() + yesterday := today.AddDate(-1, 0, 0) + todayTimestamp := today.UnixNano() / 1000000000 + yesterdayTimestamp := yesterday.UnixNano() / 1000000000 + todayString := strconv.FormatInt(todayTimestamp, 10) + yesterdayString := strconv.FormatInt(yesterdayTimestamp, 10) + + historyParameters := slack.NewHistoryParameters() + historyParameters.Latest = todayString + historyParameters.Oldest = yesterdayString + historyParameters.Count = 1000 + + fmt.Println("date & convert Check") + fmt.Println(today) + fmt.Println(yesterday) + + fmt.Println(todayString) + fmt.Println(yesterdayString) + + fmt.Println("\nHistoryParameters check") + fmt.Println(historyParameters) + + fmt.Println("\nType Check") + fmt.Println(reflect.TypeOf(todayString)) + fmt.Println(reflect.TypeOf(yesterdayString)) + fmt.Println(reflect.TypeOf(historyParameters.Count)) + + channels, err := api.GetChannels(false) + + if err != nil { + fmt.Printf("error : %s\n", err) + return + } + + for _, channel := range channels { + if channel.Name == "daily-logs-bravo" { + fmt.Println(channel.ID) + history, err := api.GetChannelHistory(channel.ID, historyParameters) + + if err != nil { + fmt.Printf("error : %s\n", err) + return + } + + fmt.Println("HISTORY LATEST") + fmt.Println(history.Latest) + fmt.Println("--------------------------------------------------------------------------------------") + fmt.Println("HISTORY HASMORE") + fmt.Println(history.HasMore) + fmt.Println("--------------------------------------------------------------------------------------") + fmt.Println("HISTORY MESSAGE") + for idx, message := range history.Messages { + fmt.Println(idx, message) + fmt.Println("--------------------------------------------------------------------------------------") + } + } + } +} diff --git a/python/logger.py b/python/logger.py new file mode 100644 index 00000000..325e21da --- /dev/null +++ b/python/logger.py @@ -0,0 +1,84 @@ +import os +import slack +import time +from typing import Union, Dict, Tuple +from asyncio import Future +from slack.web.base_client import SlackResponse +from datetime import datetime, timedelta + + +def valid_response(response: Union[Future, SlackResponse]): + assert response["ok"] is True, f"Response Error -> {response}" + return response + + +def user_list(client:slack.WebClient) -> Dict: + response = client.users_list(limit=1000) + response = valid_response(response) + + users = {} + for user in response["members"]: + + users[user["id"]] = {"name": user["name"], + "display_name": user["profile"]["display_name"], + "favicon_img": [user["profile"]["image_24"], + user["profile"]["image_32"], + user["profile"]["image_72"], + user["profile"]["image_192"], + user["profile"]["image_512"]]} + return users + + +def channel_list(client:slack.WebClient) -> Dict: + response = client.channels_list(exclude_archived=1) + response = valid_response(response) + + channels = {} + for channel in response["channels"]: + channels[channel["name"]] = channel["id"] + return channels + + +def get_history(client:slack.WebClient, channel_id: str, start: str, end: str): + response = client.channels_history(channel=channel_id, + latest=start, + oldest=end, + count=1000, + inclusive=0) + response = valid_response(response) + + messages = [] + for message in response["messages"]: + + if is_thread(message): + continue + + messages.append({"user_id": message["user"], + "text": message["text"], + "timestamp": message["ts"]}) + + return messages + + +def is_thread(message): + keys = message.keys() + return True if "parent_user_id" in keys else False + + +def get_history_days() -> Tuple[str, str]: + today = str(time.mktime(datetime.today().timetuple())) + yesterday = str(time.mktime((datetime.today() + timedelta(days=-1)).timetuple())) + return today, yesterday + + +if __name__ == "__main__": + client = slack.WebClient(token=os.environ.get("DFAB_BOT"), ssl=False) + + channels = channel_list(client=client) + users = user_list(client) + + channel_id = channels["daily-logs"] + start, end = get_history_days() + + response = get_history(client, channel_id, start, end) + print(response)