-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht2t-sms.py
61 lines (47 loc) · 1.73 KB
/
t2t-sms.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
try:
import os
import requests
from flask import Flask, request, abort
from waitress import serve
from functools import wraps
from twilio.request_validator import RequestValidator
except ImportError as err:
print(f"Failed to import the required modules: {err}")
TWILIO_AUTH_TOKEN = os.environ['TWILIO_AUTH_TOKEN']
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
TELEGRAM_CHAT_ID = os.environ['TELEGRAM_CHAT_ID']
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}"
webhook = Flask(__name__)
def validate_twilio_request(f):
@wraps(f)
def decorated_function(*args, **kwargs):
validator = RequestValidator(TWILIO_AUTH_TOKEN)
request_valid = validator.validate(
request.url,
request.form,
request.headers.get('X-TWILIO-SIGNATURE', ''))
if request_valid:
return f(*args, **kwargs)
else:
return abort(403)
return decorated_function
@webhook.route("/", methods=["GET"])
def index_webhook():
return webhook.send_static_file('index.html')
@webhook.route("/sms", methods=["POST"])
@validate_twilio_request
def sms_reply():
message = request.form["Body"]
sender = request.form["From"]
send_message(f"From: {sender}\n{message}", TELEGRAM_CHAT_ID)
return 'SUCCESS'
def send_message(message, chat_id):
url = f"{TELEGRAM_API_URL}/sendMessage?text={message}&chat_id={chat_id}"
requests.get(url)
def send_initial_message(chat_id):
message = "Bot started"
url = f"{TELEGRAM_API_URL}/sendMessage?text={message}&chat_id={chat_id}"
requests.get(url)
if __name__ == "__main__":
send_initial_message(TELEGRAM_CHAT_ID)
serve(webhook, host='0.0.0.0', port=8080)