-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio_messaging.py
61 lines (57 loc) · 2.32 KB
/
twilio_messaging.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
# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client
import json
import mqtt_messaging
from time import sleep
with open('config.json') as config_file:
config = json.load(config_file)
MEDIA_SUPPORT_WHATSAPP = ['image/jpeg', 'application/pdf', 'audio/mpeg'] #Supports video/mp4 but needs larger size maybe
MEDIA_SUPPORT_MESSENGER = ['image/jpeg']
# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = config["twilio"]["account_sid"]
auth_token = config["twilio"]["auth_token"]
client = Client(account_sid, auth_token)
def send_messages(to_no, messages):
for message in messages:
send_message(to_no, message)
sleep(3)
def send_message(to_no, message):
from_no = ""
if to_no.startswith('whatsapp'):
from_no = config["twilio"]["whatsapp_no"]
elif to_no.startswith('messenger'):
from_no = config["twilio"]["messenger_no"]
elif to_no.startswith('sparrow'):
mqtt_messaging.send_message(to_no, message)
return
else:
from_no = config["twilio"]["number"]
message = client.messages.create(
body=message,
from_=from_no,
to=to_no
)
print(message.sid)
def send_message_with_media(sender, to_no, message, media, mime_type):
from_no = ""
if to_no.startswith('whatsapp'):
from_no = config["twilio"]["whatsapp_no"]
if mime_type not in MEDIA_SUPPORT_WHATSAPP:
send_message(to_no, message + "\nFollowing media was sent to you: "+ media)
return
elif to_no.startswith('messenger'):
from_no = config["twilio"]["messenger_no"]
if mime_type not in MEDIA_SUPPORT_MESSENGER:
send_message(to_no, message + "\nFollowing media was sent to you: "+ media)
return
else:
send_message(to_no, message + "\nFollowing media was sent to you: "+ media)
return
message = client.messages.create(
body=message,
from_=from_no,
to=to_no,
media_url=[media]
)
print(message.sid)