Skip to content

Commit

Permalink
feat: collect user info, channels info, message info
Browse files Browse the repository at this point in the history
  • Loading branch information
ssaru committed Jan 27, 2020
1 parent 28e2222 commit 632c6a5
Show file tree
Hide file tree
Showing 2 changed files with 91 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
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 632c6a5

Please sign in to comment.