-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_main.py
195 lines (142 loc) · 4.96 KB
/
dev_main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import json
import logging
import os
import sqlalchemy
from sqlalchemy import engine
from flask import Flask, request
from google.cloud.sql.connector import connector
from slack_bolt import App
from slack_bolt.adapter.flask import SlackRequestHandler
from slack_bolt.oauth.oauth_settings import OAuthSettings
from slack_sdk.oauth import installation_store
from slack_sdk.oauth.installation_store.sqlalchemy import SQLAlchemyInstallationStore
from slack_sdk.oauth.state_store.sqlalchemy import SQLAlchemyOAuthStateStore
from utils import BlockTemplates, SongRecommender, SQLAlchemyVumblebotStore
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
logger = logging.getLogger().setLevel(logging.INFO)
db_user = os.environ["DB_USER"]
db_pass = os.environ["DB_PASS"]
db_name = os.environ["DB_NAME"]
db_host = os.environ["DB_HOST"]
db_driver = os.environ.get("DB_DRIVER", "pymysql")
db_socket_dir = os.environ.get("DB_SOCKET_DIR", "/cloudsql")
instance_connection_name = os.environ["INSTANCE_CONNECTION_NAME"]
def getconn():
return connector.connect(
instance_connection_name, db_driver, user=db_user, password=db_pass, db=db_name
)
db_config = {
"pool_size": 5,
"max_overflow": 2,
"pool_timeout": 30,
"pool_recycle": 1800,
}
# local
sql_engine = sqlalchemy.create_engine("mysql+pymysql://", creator=getconn)
oauth_state_store = SQLAlchemyOAuthStateStore(
expiration_seconds=120, engine=sql_engine, logger=logger
)
installation_store = SQLAlchemyInstallationStore(
client_id=os.environ["SLACK_CLIENT_ID"], engine=sql_engine, logger=logger
)
vumblebot_store = SQLAlchemyVumblebotStore(engine=sql_engine, logger=logger)
try:
sql_engine.execute("select count(*) from slack_bots")
except Exception as e:
installation_store.metadata.create_all(sql_engine)
oauth_state_store.metadata.create_all(sql_engine)
oauth_settings = OAuthSettings(
client_id=os.environ["SLACK_CLIENT_ID"],
client_secret=os.environ["SLACK_CLIENT_SECRET"],
state_store=oauth_state_store,
)
# process_before_response must be True when running on FaaS
app = App(
logger=logger,
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
installation_store=installation_store,
oauth_settings=oauth_settings,
process_before_response=True,
)
song_recommender = SongRecommender()
def handle_challenge(request):
slack_request = json.loads(request.get("body"))
response = {"statusCode": 200, "body": slack_request.get("challenge")}
return response
@app.middleware
def log_request(logger, body, next):
return next()
@app.command("/onboarding")
def handle_onboarding(ack, body, say, logger):
ack()
blocks = BlockTemplates.onboarding()
say(
text="text",
blocks=blocks,
unfurl_links=False, # 링크 펼치지 않기
unfurl_media=False, # 미디어 펼치지 않기
)
@app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
client.views_publish(
user_id=event.get("user"),
view=BlockTemplates.app_home_opened(),
)
except Exception as e:
logger.error(f"Error publishing home tab: {e}")
@app.action("reco_static_select")
def handle_reco_static_select(ack, body, client, logger):
"""Type: 0(Click)"""
ack()
vumblebot_store.save_user_feedback(body, 0)
@app.action("like")
def handle_like(ack, body, client, logger):
"""Type: 1(Like)"""
ack()
client.reactions_add(
channel=body["channel"]["id"],
timestamp=body["message"]["thread_ts"],
name="thumbsup",
)
vumblebot_store.save_user_feedback(body, 1)
@app.action("dislike")
def handle_dislike(ack, body, client, logger):
"""Type: 2(Dislike)"""
ack()
client.reactions_add(
channel=body["channel"]["id"],
timestamp=body["message"]["thread_ts"],
name="bow",
)
vumblebot_store.save_user_feedback(body, 2)
@app.event("app_mention")
def handle_app_mentions(ack, body, say, logger):
ack()
is_thread = True if "thread_ts" in body.get("event", "") else False
if is_thread:
return
user = song_recommender.recommend_song(body)
blocks = BlockTemplates.app_mention(user.emotion, user.score, user.contents)
say(
text="text",
blocks=blocks,
channel=user.channel,
thread_ts=user.event_ts,
unfurl_links=False, # 링크 펼치지 않기
unfurl_media=False, # 미디어 펼치지 않기
)
vumblebot_store.save_user_input(user)
flask_app = Flask(__name__)
bolt_handler = SlackRequestHandler(app)
@flask_app.route("/slack/install", methods=["GET"])
def install():
return bolt_handler.handle(request)
@flask_app.route("/slack/oauth_redirect", methods=["GET"])
def oauth_redirect():
return bolt_handler.handle(request)
@flask_app.route("/slack/events", methods=["POST"])
def handler():
return bolt_handler.handle(request)
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))