-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
316 lines (266 loc) · 9.17 KB
/
handlers.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
Implement business logic for processing messages based on their type or purpose.
"""
from datetime import datetime
import json
from utils.logging import configure_logging
from database import build_insert_query, execute_query
from config import (
MESSAGES_TABLE,
GROUPME_TABLE,
GROUPME_CALLBACK_TABLE,
SENT_MESSAGES_TABLE,
)
logger = configure_logging(__name__)
MESSAGE_HANDLERS = {}
def register_message_handler(message_type):
"""Decorator to register a handler for a specific message type."""
def decorator(func):
MESSAGE_HANDLERS[message_type] = func
return func
return decorator
@register_message_handler("postgres")
def handle_postgres_data(_message, _cursor):
"""
????
"""
logger.critical("Received message with type 'postgres'. No handler implemented.")
# def add_to_contacts(message, cursor):
# """
# Add a new contact to the contacts table.
# This function is called by the handle_contact_event function.
# """
# columns = ['"phone_number"', '"contact_name"']
# values = [message.get("phone_number"), message.get("contact_name")]
# query, values = build_insert_query("contacts", columns, values)
# cursor.execute(query, values)
# def handle_contact_event(message, cursor):
# """
# Handle the insertion of a new contact.
# """
# add_to_contacts(message, cursor)
@register_message_handler("twilio.sms.incoming")
def handle_twilio_sms(message, cursor):
"""
Handle insertion of incoming Twilio SMS messages.
Calls database.execute_query with the appropriate query and values.
"""
logger.debug("Handling twilio.sms.incoming message: %s", message)
# Prepare additional columns and values for From(LocationType) if they exist
location_columns = []
location_values = []
for loc_type in ["from_city", "from_state", "from_country", "from_zip"]:
if message.get(loc_type):
location_columns.append(f'"{loc_type}"')
location_values.append(message.get(loc_type))
# Prepare additional columns and values for media items
media_columns = []
media_values = []
for i in range(10):
# Keys in the Twilio request body
media_type_key = f"MediaContentType{i}"
media_url_key = f"MediaUrl{i}"
if message.get(media_type_key):
media_columns.append(f"media_content_type_{i}")
media_values.append(message.get(media_type_key))
if message.get(media_url_key):
media_columns.append(f"media_url_{i}")
media_values.append(message.get(media_url_key))
# Combine static columns with dynamic columns
columns = (
[
"message_sid",
"account_sid",
"messagingservice_sid",
"from_num",
"to_num",
"body",
"num_segments",
"num_media",
"api_version",
"sender_name",
"wbor_message_id",
]
+ location_columns
+ media_columns
)
values = (
[
message.get("MessageSid"),
message.get("AccountSid"),
message.get("MessagingServiceSid"),
message.get("From"),
message.get("To"),
message.get("Body"),
message.get("NumSegments"),
message.get("NumMedia"),
message.get("ApiVersion"),
message.get("SenderName"),
message.get("wbor_message_id"),
]
+ location_values
+ media_values
)
query, values = build_insert_query(MESSAGES_TABLE, columns, values)
logger.debug("Executing `twilio.sms.incoming` query: %s", query)
execute_query(cursor, query, values)
@register_message_handler("twilio.sms.outgoing")
def handle_outgoing_twilio_sms(message, cursor):
"""
Log outgoing Twilio SMS messages (sent by MGMT).
"""
logger.debug("Handling twilio.sms.outgoing message: %s", message)
timestamp_str = message.get("timestamp")
timestamp = datetime.fromisoformat(timestamp_str.replace("Z", "+00:00"))
columns = [
'"wbor_message_id"',
'"recipient_number"',
'"body"',
'"timestamp"',
]
values = [
message.get("wbor_message_id"),
message.get("recipient_number"),
message.get("body"),
timestamp,
]
query, values = build_insert_query(SENT_MESSAGES_TABLE, columns, values)
logger.debug("Executing `twilio.sms.outgoing` query: %s", query)
execute_query(cursor, query, values)
@register_message_handler("twilio.voice-intelligence")
def handle_twilio_voice_intelligence(message, cursor):
"""
Log Twilio Voice Intelligence messages.
TODO: store transcripts?
"""
logger.debug("Handling twilio.voice-intelligence message: %s", message)
# query, values = build_insert_query(TABLE, columns, values)
# logger.debug("Executing `twilio.voice-intelligence` query: %s", query)
# execute_query(cursor, query, values)
@register_message_handler("twilio.call-events")
def handle_twilio_call_events(message, cursor):
"""
Log Twilio call event messages.
TODO: store audio file?
"""
logger.debug("Handling twilio.call-events message: %s", message)
# query, values = build_insert_query(TABLE, columns, values)
# logger.debug("Executing `twilio.call-events` query: %s", query)
# execute_query(cursor, query, values)
@register_message_handler("groupme.msg")
def handle_message_event(message, cursor):
"""
Handle insertion of text message logs from GroupMe.
"""
logger.debug("Handling groupme.msg message: %s", message)
# Prepare the columns and values to insert
columns = [
'"text"',
'"bot_id"',
'"code"',
'"type"',
'"wbor_message_id"',
'"picture_url"',
'"source"',
]
values = [
message.get("text"),
message.get("bot_id"),
message.get("statuscode"),
message.get("type"),
message.get("wbor_message_id"),
message.get("picture_url"),
message.get("source"),
]
# Build and execute the SQL query
query, values = build_insert_query(GROUPME_TABLE, columns, values)
logger.debug("Executing `groupme.msg` query: %s", query)
execute_query(cursor, query, values)
@register_message_handler("groupme.img")
def handle_image_event(message, cursor):
"""
Handle insertion of image message logs from GroupMe.
"""
logger.debug("Handling groupme.img message: %s", message)
# Prepare the columns and values to insert
columns = [
'"raw_img"',
'"bot_id"',
'"code"',
'"type"',
'"wbor_message_id"',
'"picture_url"',
'"text"',
'"source"',
]
values = [
message.get("raw_img"), # Unlikely to be used based on current implementation
message.get("bot_id"),
message.get("statuscode"),
message.get("type"),
message.get("wbor_message_id"),
message.get("picture_url"),
message.get("text"),
message.get("source"),
]
query, values = build_insert_query(GROUPME_TABLE, columns, values)
logger.debug("Executing `groupme.img` query: %s", query)
execute_query(cursor, query, values)
@register_message_handler("groupme.callback")
def handle_callback_event(message, cursor):
"""
Handle insertion of callback logs from GroupMe.
"""
logger.debug("Handling groupme.callback message: %s", message)
# Convert the Unix timestamp to a datetime object
created_at = datetime.fromtimestamp(message.get("created_at"))
# Prepare the columns and values to insert
columns = [
'"attachments"',
'"avatar_url"',
'"created_at"',
'"group_id"',
'"id"',
'"name"',
'"sender_id"',
'"sender_type"',
'"source_guid"',
'"system"',
'"text"',
'"user_id"',
]
values = [
json.dumps(message.get("attachments", [])), # Convert list to JSON string
message.get("avatar_url"),
created_at,
message.get("group_id"),
message.get("id"), # Rename 'id' to 'message_id' for clarity
message.get("name"),
message.get("sender_id"),
message.get("sender_type"),
message.get("source_guid"),
message.get("system"),
message.get("text"),
message.get("user_id"),
]
query, values = build_insert_query(GROUPME_CALLBACK_TABLE, columns, values)
logger.debug("Executing `groupme.callback` query: %s", query)
execute_query(cursor, query, values)
# Example handler for generic messages
# @register_message_handler("generic_event")
# def handle_generic_event(message, cursor):
# """Handle insertion of generic event messages."""
# cursor.execute(
# query,
# (message.get("event_id"), message.get("event_name"), message.get("timestamp")),
# )
# @register_message_handler("rds")
# def handle_rds_data(message, cursor):
# """Handle Radio Data System (RDS) data messages."""
# columns = ["song_title", "artist", "timestamp"]
# values = [
# message.get("song_title"),
# message.get("artist"),
# message.get("timestamp"),
# ]
# execute_query(cursor, query, values)