-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextClient.py
109 lines (89 loc) · 3.56 KB
/
TextClient.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from Gateways import Gateways
from Message import *
# from version import *
from tempwa import *
import json
import requests
class TextClient:
gateway = ''
apikey = ''
messages = []
MESSAGES_MAXIMUM = 100
# VERSION = __version__
# Initialize Client with defaul Gateway Gateways.Global
def __init__(self, apikey, gateway=Gateways.Global):
self.apikey = apikey
self.gateway = gateway
# Send 1 message to one or multiple locations
def SendSingleMessage(self, message, from_, to=[], reference=None, allowedChannels=None):
self.messages = []
self.messages.append(Message(message, from_=from_, to=to, reference=reference, allowedChannels=allowedChannels))
self.send()
# Add a message to the list
def AddMessage(self, message, from_='', to=[], reference=None, allowedChannels=None):
self.messages.append(Message(message, from_=from_, to=to, reference=reference, allowedChannels=allowedChannels))
# Add a rich message to the list
def AddRichMessage(self, message, media=None, from_='', to=[], reference=None, allowedChannels=None,temp=None):
self.messages.append(Message(message, media=media, from_=from_, to=to, reference=reference, allowedChannels=allowedChannels,template=temp))
# Send all messages in the list
def send(self):
if len(self.messages) == 0:
print('No messages in the queue')
return
if len(self.messages) > self.MESSAGES_MAXIMUM:
print('Messages exceeds MESSAGES_MAXIMUM')
return
# Set data for post
data = self.encodeData(self.messages)
# Set headers for post
headers = {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": str(len(data)),
'X-CM-SDK': 'text-sdk-python-' + '1.0.5'
}
# Send the message
try:
response = requests.post("https://gw.cmtelecom.com/v1.0/message", data=data, headers=headers)
except Exception as e:
print(e)
# Clear messages
self.messages = []
# Return response
return response
# Method to encode Data, Gateway accepts this format
def encodeData(self, messages):
# Set productToken
data = {"messages": {"authentication":{"producttoken": self.apikey}}}
data['messages']['msg'] = []
# For each message do this
for message in messages:
# List all recipients
to = []
for toItem in message.to:
to = to + [{'number': toItem}]
# Create message container
temp = {"allowedChannels": message.allowedChannels,
"from": message.from_,
"to": to,
"body": {
"type": message.type,
"content": message.body
}
}
#if the message has the template
if message.template is not None:
temp["richContent"] = {"conversation": [message.template]}
# or If message is rich
elif message.richContent is not None:
temp["richContent"] = {
"conversation": [{
"text": message.body
},
{
"media": message.richContent,
}]
}
data['messages']['msg'] = data['messages']['msg'] + [temp]
# Json encode the data
data = json.dumps(data)
return data