-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
146 lines (116 loc) · 3.73 KB
/
app.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import logging
import asyncpg
import asyncio
import datetime
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
# app init
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
# log level
if os.environ.get("LOG_LEVEL") == "INFO":
logging.basicConfig(level=logging.INFO)
elif os.environ.get("LOG_LEVEL") == "DEBUG":
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
# ---common function---
async def db_insert(channel_id,post_at_time,permalink,post_user_id):
dsn = os.environ["PSQL_DSN"]
conn = await asyncpg.connect(dsn)
# Note: table is using by "production" and "develop" in my DB.
# So this can switch two tables.
table = os.environ["PSQL_TABLE"]
sql = "INSERT INTO :table (channel_id,post_at,permalink,post_user_id) VALUES($1,$2,$3,$4)"
sql = sql.replace(":table",table)
# using transaction
async with conn.transaction():
await conn.execute(sql, channel_id,post_at_time,permalink,post_user_id)
await conn.close()
# ---event function---
@app.event({
"type": "message",
"subtype": (None,"thread_broadcast")
})
def handle_message_events(say, logger, context, message):
logger.debug(message)
channel_id = message["channel"]
post_at_ts = message["ts"]
# In some cases, 'user' is null
if 'user' in message:
post_user_id = message["user"]
else:
post_user_id = ""
# get permalink api
res = app.client.chat_getPermalink(
channel=channel_id,
message_ts=post_at_ts
)
permalink = res["permalink"]
# "ts" to unixtime. And unixtime to datetime.
post_at_unix = int(post_at_ts.split('.')[0])
post_at_time = datetime.datetime.fromtimestamp(post_at_unix)
# post to timeline
app.client.chat_postMessage(
channel=os.environ["TIMELINE_CHANNEL_ID"],
text=permalink
)
# insert to DB
asyncio.run(db_insert(channel_id,post_at_time,permalink,post_user_id))
# files post
#@app.event("file_shared")
@app.event({
"type": "message",
"subtype": "file_share"
})
def handle_file_created(say, logger, context, message):
logger.debug(message)
# event -> file_shared
'''
file_id = body['event']['file_id']
fid_res = app.client.files_info(
file = file_id
)
public_data = fid_res.get('file').get('shares').get('public')
if public_data is None:
return
if len(public_data) != 1:
return
for channel_id in public_data.keys():
post_at_ts = public_data[channel_id].get('ts')
if post_at_ts is None:
return
post_user_id = fid_res.get('file').get('user')
if post_user_id is None:
post_user_id = ""
'''
channel_id = message["channel"]
post_at_ts = message["ts"]
# In some cases, 'user' is null
if 'user' in message:
post_user_id = message["user"]
else:
post_user_id = ""
# get permalink api
res = app.client.chat_getPermalink(
channel=channel_id,
message_ts=post_at_ts
)
permalink = res["permalink"]
# "ts" to unixtime. And unixtime to datetime.
post_at_unix = int(post_at_ts.split('.')[0])
post_at_time = datetime.datetime.fromtimestamp(post_at_unix)
# post to timeline
app.client.chat_postMessage(
channel=os.environ["TIMELINE_CHANNEL_ID"],
text=permalink
)
# insert to DB
asyncio.run(db_insert(channel_id,post_at_time,permalink,post_user_id))
# other message
@app.event("message")
def handle_message_events_other(body, logger):
logger.debug(body)
# app start
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()