-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_bot.py
158 lines (131 loc) · 4.61 KB
/
app_bot.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
from memory import get_chat_log, append_interaction_to_chat_log
import logging
logging.basicConfig(level=logging.DEBUG)
from gpt import ask_chatgpt, ask
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from pathlib import Path
from dotenv import load_dotenv
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])
@app.command("/hello-socket-mode")
def hello_command(ack, body):
user_id = body["user_id"]
ack(f"Hi <@{user_id}>!")
#When the app is mentioned in a channel, respond with a message
@app.event("app_mention")
def event_test(event, say):
#say(f"Hi there, <@{event['user']}>!")
say(f"AI: ({ask(event['text'])})")
#print(event['text'])
#say(text=ask(text))
#When the app is DM'd, respond with a message
@app.event("message")
def handle_message_events(event, say):
#logger.info(body)
#say(f"{ask_chatgpt(event['text'])}")
#incoming_msg = request.values['Body']
#chat_log = session.get('chat_log')
#answer = ask(incoming_msg, chat_log)
#session['chat_log'] = append_interaction_to_chat_log(incoming_msg, answer,
# chat_log)
#1. Getting data from incoming message
time_stamp = event['event_ts']
incoming_text = event['text']
user = event['user']
channel = event['channel']
#2. Getting chat log from database to memory
chat_log = get_chat_log("message", time_stamp, incoming_text, user, channel)
print("BEFORE chat_log:- ", chat_log)
#3. Getting answer from GPT
answer = ask_chatgpt(incoming_text, chat_log)
say(f"{answer}")
#4. Updating chat log in database
chat_log = append_interaction_to_chat_log(incoming_text, answer, chat_log, user, channel)
print("AFTER chat_log:- ", chat_log)
def ack_shortcut(ack):
ack()
def open_modal(body, client):
client.views_open(
trigger_id=body["trigger_id"],
view={
"type": "modal",
"callback_id": "socket_modal_submission",
"submit": {
"type": "plain_text",
"text": "Submit",
},
"close": {
"type": "plain_text",
"text": "Cancel",
},
"title": {
"type": "plain_text",
"text": "Socket Modal",
},
"blocks": [
{
"type": "input",
"block_id": "q1",
"label": {
"type": "plain_text",
"text": "Write anything here!",
},
"element": {
"action_id": "feedback",
"type": "plain_text_input",
},
},
{
"type": "input",
"block_id": "q2",
"label": {
"type": "plain_text",
"text": "Can you tell us your favorites?",
},
"element": {
"type": "external_select",
"action_id": "favorite-animal",
"min_query_length": 0,
"placeholder": {
"type": "plain_text",
"text": "Select your favorites",
},
},
},
],
},
)
app.shortcut("socket-mode")(ack=ack_shortcut, lazy=[open_modal])
all_options = [
{
"text": {"type": "plain_text", "text": ":cat: Cat"},
"value": "cat",
},
{
"text": {"type": "plain_text", "text": ":dog: Dog"},
"value": "dog",
},
{
"text": {"type": "plain_text", "text": ":bear: Bear"},
"value": "bear",
},
]
@app.options("favorite-animal")
def external_data_source_handler(ack, body):
keyword = body.get("value")
if keyword is not None and len(keyword) > 0:
options = [o for o in all_options if keyword in o["text"]["text"]]
ack(options=options)
else:
ack(options=all_options)
@app.view("socket_modal_submission")
def submission(ack):
ack()
if __name__ == "__main__":
# export SLACK_APP_TOKEN=xapp-***
# export SLACK_BOT_TOKEN=xoxb-***
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()