twillio_alerts/sms_alerts.py

32 wiersze
815 B
Python
Executable File

#!/usr/bin/python3
from twilio.rest import Client
import sys
import email
# Find these values at https://twilio.com/user/account
account_sid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
client = Client(account_sid, auth_token)
raw_msg = ""
for line in sys.stdin:
raw_msg = raw_msg + line
msg = email.message_from_string(raw_msg)
text = msg['Subject'] + "\n\n"
if msg.is_multipart():
for payload in msg.get_payload():
text = text + payload.get_payload()
else:
text = text + msg.get_payload()
text = text[:160] if len(text) > 160 else text
#print(text)
message = client.api.account.messages.create(to = "+357XXXXXXXX",
from_ = " +177XXXXXXXX",
body = text)