Skip to content

Commit

Permalink
Feature/#1 (#2)
Browse files Browse the repository at this point in the history
*  test getting past message in "Slack"

*  check message structure

*  feat: collect user info, channels info, message info

 #1
  • Loading branch information
ssaru authored Jan 27, 2020
1 parent 45f281a commit 4f7a336
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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

Expand Down
73 changes: 73 additions & 0 deletions getPastMessage.go
Original file line number Diff line number Diff line change
@@ -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("--------------------------------------------------------------------------------------")
}
}
}
}
84 changes: 84 additions & 0 deletions python/logger.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4f7a336

Please sign in to comment.