-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd.py
52 lines (38 loc) · 1.57 KB
/
httpd.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
import utils
import logging
import config
from aiohttp import web
from twilio.twiml.voice_response import VoiceResponse
from twilio.twiml.messaging_response import MessagingResponse
logger = logging.getLogger(__name__)
app = web.Application()
routes = web.RouteTableDef()
@routes.post(config.VOICE_WEBHOOK_PATH)
async def record(request):
post_data = await request.post()
data = {**post_data, **request.query}
sender = data.get('From', '<unknown number>')
me = data.get('To', '<unknown number>')
logger.warning(f'Recorded from {sender} to {me}.')
await utils.send_to_admin_channel(f'Recorded voice from {sender} to {me}.')
response = VoiceResponse()
response.record()
response.hangup()
return web.Response(text=str(response), content_type='text/xml')
@routes.post(config.SMS_WEBHOOK_PATH)
async def sms(request):
post_data = await request.post()
data = {**post_data, **request.query}
sender = data.get('From', '<unknown number>')
me = data.get('To', '<unknown number>')
body = data.get('Body', '<unknown message>')
logger.warning(f'Received SMS from {sender} to {me}: \n{body}')
await utils.send_to_admin_channel(f'Received SMS from {sender} to {me}: \n{body}')
return web.Response(text=str(str(MessagingResponse())), content_type='text/xml')
async def main():
logger.info('Bot, SMS and Audio webhook server started')
app.add_routes(routes)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host=config.SMS_WEBHOOK_LISTEN, port=config.SMS_WEBHOOK_PORT)
await site.start()