-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocksms.py
66 lines (48 loc) · 1.46 KB
/
rocksms.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
#! python3
# Copyright 2017 Michael Neeley
# builtin libs
import os
import logging
# installed libs
# pip install flask
from flask import Flask, request
# pip install twilio
from twilio.twiml.messaging_response import MessagingResponse
# package libs
import contacts
VERSION = "1.4.1"
app = Flask(__name__)
# test Flask server is running
@app.route("/")
def hello():
return ("Hello from Rock SMS Server v" + VERSION + "!")
@app.route('/sms', methods=['POST'])
def sms():
# handle request
app.logger.info(request.form)
body = request.form['Body']
# handle response
response = MessagingResponse()
#TODO: send error message
# if response is not good, show details
#if str(response) != "200 OK":
# app.logger.error(str(response))
# response.message("Error occurred. The team has been notified!")
# return str(response)
reply = contacts.search(body)
app.logger.info(reply)
response.message(reply)
return str(response)
#TODO: add a usage help menu and use short codes to choose apps
#TODO: add a feature that reads the staff news when you call in
if __name__ == '__main__':
#TODO: move this to external class
# add log file handlers
fh = logging.RotatingFileHandler("rocksms.log", maxBytes = 10000, backupCount = 1)
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
app.logger.addHandler(fh)
# add email handlers
#TODO: add email for ERROR messages
app.run()