-
Notifications
You must be signed in to change notification settings - Fork 0
/
oooChecker.py
90 lines (68 loc) · 3.14 KB
/
oooChecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import requests
import json
import base64
from flask import abort, Flask, jsonify, request
from rq import Queue
from oooWorker import conn
app = Flask(__name__)
def IsRequestValid(request):
isTokenValid = request.form['token'] == os.environ.get('VALID_TOKEN')
isTeamIdValid = request.form['team_id'] == os.environ.get('TEAM_ID')
return isTokenValid and isTeamIdValid
def GetChanellHistory(responseUrl, userId):
oooHistoryString = ""
BOT_TOKKEN = os.environ.get('BOT_TOKEN')
OOO_CHANNEL_ID = os.environ.get('OOO_CHANNEL_ID')
payload = {"token": BOT_TOKKEN, "channel": OOO_CHANNEL_ID}
headers = {'content-type': 'application/x-www-form-urlencoded'}
while True:
response = requests.get('https://slack.com/api/conversations.history', params=payload, headers=headers)
allDict = json.loads(response.text)
for messageItem in allDict["messages"]:
if "user" in messageItem and messageItem["user"] == userId:
if "thread_ts" in messageItem:
threadPayload = {"token": BOT_TOKKEN, "channel": OOO_CHANNEL_ID, "ts": messageItem["thread_ts"]}
threadResponse = requests.get('https://slack.com/api/conversations.replies', params=threadPayload, headers=headers)
threadDict = json.loads(threadResponse.text)
for threadItem in threadDict["messages"]:
if "user" in threadItem and threadItem["user"] == userId:
oooHistoryString = PrintItem(threadItem, oooHistoryString)
else:
oooHistoryString = PrintItem(messageItem, oooHistoryString)
if allDict["has_more"] == True:
payload['cursor'] = allDict["response_metadata"]["next_cursor"]
else:
break
attachmentsDict = {}
attachmentsDict['title'] = "Here comes your OoO history."
attachmentsDict['text'] = oooHistoryString
SendMessageToSlack(attachmentsDict, responseUrl)
def PrintItem(messageItem, oooHistoryString):
oooHistoryString += messageItem['text']
oooHistoryString += "\n\n"
return oooHistoryString
def SendMessageToSlack(attachment, url):
payload = {}
payload['text'] = 'Get OoO History'
payload['attachments'] = []
payload['attachments'].append(attachment)
payload['response_type'] = 'ephemeral'
payloadResult = json.dumps(payload);
headers = {'Content-type': 'application/json', 'charset': 'UTF-8'}
result = requests.post(url, data=payloadResult, headers=headers)
print(result.text)
def QueingJob(request):
print ("QueingJob")
q = Queue(connection=conn)
result = q.enqueue(GetChanellHistory, request.form['response_url'], request.form['user_id'])
print(result)
@app.route('/', methods=['POST'])
def OOOMe():
if not IsRequestValid(request):
abort(400)
print ("OOOME")
# GetChanellHistory(request.form['response_url'], request.form['user_id'])
QueingJob(request)
return jsonify(response_type='ephemeral', text="Check %s's ooo history:fast_parrot:" % request.form['user_name'])
# ooo channel id C4DRJAA0Y