-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathreceive_notifications.py
More file actions
215 lines (171 loc) · 7.01 KB
/
Copy pathreceive_notifications.py
File metadata and controls
215 lines (171 loc) · 7.01 KB
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
from datetime import datetime
from urllib.parse import unquote
from app.celery.queue_utils import get_message_group_id_for_queue
import iso8601
from flask import Blueprint, abort, current_app, jsonify, request
from gds_metrics.metrics import Counter
from app.celery import service_callback_tasks
from app.config import QueueNames
from app.constants import INBOUND_SMS_TYPE
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.dao.services_dao import dao_fetch_service_by_inbound_number
from app.errors import register_errors
from app.models import InboundSms
from app.utils import try_parse_and_format_phone_number
receive_notifications_blueprint = Blueprint("receive_notifications", __name__)
register_errors(receive_notifications_blueprint)
INBOUND_SMS_COUNTER = Counter("inbound_sms", "Total number of inbound SMS received", ["provider"])
@receive_notifications_blueprint.route("/notifications/sms/receive/mmg", methods=["POST"])
def receive_mmg_sms():
"""
{
'MSISDN': '447123456789'
'Number': '40604',
'Message': 'some+uri+encoded+message%3A',
'ID': 'SOME-MMG-SPECIFIC-ID',
'DateRecieved': '2017-05-21+11%3A56%3A11'
}
"""
post_data = request.get_json()
auth = request.authorization
if not auth:
current_app.logger.warning("Inbound sms (MMG) no auth header")
abort(401)
elif (
auth.username not in current_app.config["MMG_INBOUND_SMS_USERNAME"]
or auth.password not in current_app.config["MMG_INBOUND_SMS_AUTH"]
):
current_app.logger.warning(
"Inbound sms (MMG) incorrect username (%s) or password",
auth.username,
extra={"username": auth.username},
)
abort(403)
inbound_number = strip_leading_forty_four(post_data["Number"])
service = fetch_potential_service(inbound_number, "mmg")
if not service:
# since this is an issue with our service <-> number mapping, or no inbound_sms service permission
# we should still tell MMG that we received it successfully
return "RECEIVED", 200
INBOUND_SMS_COUNTER.labels("mmg").inc()
inbound = create_inbound_sms_object(
service,
content=format_mmg_message(post_data["Message"]),
from_number=post_data["MSISDN"],
provider_ref=post_data["ID"],
date_received=post_data.get("DateRecieved"),
provider_name="mmg",
)
queue_name = QueueNames.CALLBACKS
message_group_kwargs = get_message_group_id_for_queue(
queue_name=queue_name,
service_id=str(service.id),
notification_type=INBOUND_SMS_TYPE,
)
service_callback_tasks.send_inbound_sms_to_service.apply_async(
[str(inbound.id), str(service.id)], queue=queue_name, **message_group_kwargs
)
current_app.logger.info(
"Service %s received inbound SMS with reference %s from MMG",
service.id,
inbound.provider_reference,
extra={"service_id": service.id, "inbound_sms_provider_reference": inbound.provider_reference},
)
return jsonify({"status": "ok"}), 200
@receive_notifications_blueprint.route("/notifications/sms/receive/firetext", methods=["POST"])
def receive_firetext_sms():
post_data = request.form
auth = request.authorization
if not auth:
current_app.logger.warning("Inbound sms (Firetext) no auth header")
abort(401)
elif auth.username != "notify" or auth.password not in current_app.config["FIRETEXT_INBOUND_SMS_AUTH"]:
current_app.logger.warning(
"Inbound sms (Firetext) incorrect username (%s) or password",
auth.username,
extra={"username": auth.username},
)
abort(403)
inbound_number = strip_leading_forty_four(post_data["destination"])
service = fetch_potential_service(inbound_number, "firetext")
if not service:
return jsonify({"status": "ok"}), 200
inbound = create_inbound_sms_object(
service=service,
content=post_data["message"],
from_number=post_data["source"],
provider_ref=None,
date_received=post_data["time"],
provider_name="firetext",
)
INBOUND_SMS_COUNTER.labels("firetext").inc()
queue_name = QueueNames.CALLBACKS
message_group_kwargs = get_message_group_id_for_queue(
queue_name=queue_name,
service_id=str(service.id),
notification_type=INBOUND_SMS_TYPE,
)
service_callback_tasks.send_inbound_sms_to_service.apply_async(
[str(inbound.id), str(service.id)], queue=queue_name, **message_group_kwargs
)
current_app.logger.info(
"%s received inbound SMS with reference %s from Firetext",
service.id,
inbound.provider_reference,
extra={"service_id": service.id, "inbound_sms_provider_reference": inbound.provider_reference},
)
return jsonify({"status": "ok"}), 200
def format_mmg_message(message):
return unescape_string(unquote(message.replace("+", " ")))
def unescape_string(string):
return string.encode("raw_unicode_escape").decode("unicode_escape")
def format_mmg_datetime(date):
"""
We expect datetimes in format 2017-05-21+11%3A56%3A11 - ie, spaces replaced with pluses, and URI encoded
and in UTC
"""
try:
orig_date = format_mmg_message(date)
parsed_datetime = iso8601.parse_date(orig_date).replace(tzinfo=None)
return parsed_datetime
except iso8601.ParseError:
return datetime.utcnow()
def create_inbound_sms_object(service, content, from_number, provider_ref, date_received, provider_name):
user_number = try_parse_and_format_phone_number(
from_number, log_msg=f'Invalid from_number received for service "{service.id}"'
)
provider_date = date_received
if provider_date:
provider_date = format_mmg_datetime(provider_date)
inbound = InboundSms(
service=service,
notify_number=service.get_inbound_number(),
user_number=user_number,
provider_date=provider_date,
provider_reference=provider_ref,
content=content,
provider=provider_name,
)
dao_create_inbound_sms(inbound)
return inbound
def fetch_potential_service(inbound_number, provider_name):
service = dao_fetch_service_by_inbound_number(inbound_number)
if not service:
current_app.logger.warning(
"Inbound number %r from %s not associated with a service",
inbound_number,
provider_name,
extra={"inbound_number": inbound_number, "provider_name": provider_name},
)
return False
if not has_inbound_sms_permissions(service.permissions):
current_app.logger.error("Service %s does not allow inbound SMS", service.id, extra={"service_id": service.id})
return False
return service
def has_inbound_sms_permissions(permissions):
str_permissions = [p.permission for p in permissions]
return INBOUND_SMS_TYPE in str_permissions
def strip_leading_forty_four(number):
if number.startswith("44"):
return number.replace("44", "0", 1)
return number