-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
127 lines (105 loc) · 3.62 KB
/
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
try:
import unzip_requirements
except ImportError:
pass
import logging
import os
import random
import re
import boto3
from slack_bolt import App
logging.basicConfig(level=logging.DEBUG)
# Slack App Config
app = App(
signing_secret=os.getenv("SLACK_SIGNING_SECRET"), # Required for Event Subscription
token=os.getenv("SLACK_BOT_TOKEN"),
process_before_response=True, # Required for Lambda
)
# Connect to DynamoDB
dynamodb = boto3.resource("dynamodb")
DYNAMODB_TABLE_NAME = "tmykbot-slack-dev-tab"
table = dynamodb.Table(os.getenv("DYNAMODB_TABLE_NAME", DYNAMODB_TABLE_NAME))
@app.message(re.compile("([a-zA-Z0-9-]+)\+\+"))
def plus_plus_handler(message, say, context):
for matched_text in context["matches"]:
try:
response = table.get_item(Key={"alias": matched_text})
if (item := response.get("Item")) is None: # Key does not exist
total_count = 1
else: # Key exists
total_count = item.get("count", 0) + 1
table.put_item(
Item={
"alias": matched_text,
"count": total_count,
}
)
except Exception as e:
print(e)
say("Error:" + e)
continue
patterns = (
f"{matched_text}がプラプラされたよ. ",
f"{matched_text} いいね!",
f"{matched_text} ナイス!",
)
reply = random.choice(patterns) + f" (++された回数: {total_count})"
say(reply)
def word_not_found(text, say):
patterns = (
f"ごめんね. 「{text}」はわからないよ.",
f"「{text}」はわからないよ.",
f"恐れ入りますが「{text}」に関して当方ではわかりかねます.",
)
reply = random.choice(patterns)
say(reply)
@app.message(re.compile("!word ([^ ]+)"))
def get_word_handler(message, say, context):
for matched_text in context["matches"]:
try:
response = table.get_item(Key={"alias": "word_" + matched_text})
if (item := response.get("Item")) is None: # Key does not exist
word_not_found(text=matched_text, say=say)
continue
# Key exists
if (desc := item.get("desc")) is None:
word_not_found(text=matched_text, say=say)
continue
# Word found
reply = f"{matched_text}: {desc}"
say(reply)
except Exception as e:
print(e)
say("Error:" + e)
continue
@app.message(re.compile("!add_word ([^, ]+,.+)"))
def add_word_handler(message, say, context):
for matched_text in context["matches"]:
try:
# print(f"{matched_text=}")
divided = matched_text.split(",")
keyword = divided[0]
desc = ",".join(divided[1:])
# TODO: ReadしないとCountが消える
table.put_item(
Item={
"alias":"word_" + keyword,
"desc": desc,
}
)
say("覚えたよ!")
except Exception as e:
print(e)
say("Error:" + e)
continue
if __name__ == "__main__":
app.start()
# Required for Lambda
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
# Logging for AWS Lambda
SlackRequestHandler.clear_all_log_handlers()
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
# AWS Lambda support
def lambda_handler(event, context):
slack_handler = SlackRequestHandler(app)
return slack_handler.handle(event, context)