Skip to content

Commit 3c690e7

Browse files
Added Whatsapp Template Messages
1 parent d5a076e commit 3c690e7

File tree

9 files changed

+198
-8
lines changed

9 files changed

+198
-8
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMText/Message.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ class Message:
1313
maximumNumberOfMessageParts = 8
1414
hybridAppKey = ''
1515
allowedChannels = ['SMS']
16+
template = None
1617
richContent = None
1718
SENDER_FALLBACK = 'cm.com'
1819
MESSAGEPARTS_MINIMUM = 1
1920
MESSAGEPARTS_MAXIMUM = 8
2021
RECIPIENTS_MAXIMUM = 1000
2122

2223
# init function of class Message
23-
def __init__(self, body='', type=MessageBodyTypes.AUTO, from_=None, to=[], reference=None, allowedChannels=None, media=None):
24+
def __init__(self, body='', type=MessageBodyTypes.AUTO, from_=None, to=[], reference=None, allowedChannels=None, media=None, template=None):
2425
self.body = body
2526
self.type = type
2627
if from_ is not None:
@@ -37,6 +38,10 @@ def __init__(self, body='', type=MessageBodyTypes.AUTO, from_=None, to=[], refer
3738
if media is not None:
3839
self.richContent = media
3940

41+
# if template is not None
42+
if template is not None:
43+
self.template = template
44+
4045
self.reference = reference
4146
self.AddRecipients(recipients=to)
4247

CMText/TextClient.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from CMText.Gateways import Gateways
22
from CMText.Message import Message
3+
from CMText.WhatsappTemplate import WhatsappTemplate
34
from CMText.version import __version__
45
import json
56
import requests
67

7-
8-
98
class TextClient:
109
gateway = ''
1110
apikey = ''
@@ -32,6 +31,10 @@ def AddMessage(self, message, from_='', to=[], reference=None, allowedChannels=N
3231
def AddRichMessage(self, message, media, from_='', to=[], reference=None, allowedChannels=None):
3332
self.messages.append(Message(message, media=media, from_=from_, to=to, reference=reference, allowedChannels=allowedChannels))
3433

34+
# Add a Whatsapp Template message to the list
35+
def AddWhatsappTemplateMessage(self, template, from_='', to=[], reference=None, media=None):
36+
self.messages.append(Message(media=media, from_=from_, to=to, reference=reference, allowedChannels=['Whatsapp'], template=template))
37+
3538
# Send all messages in the list
3639
def send(self):
3740
if len(self.messages) == 0:
@@ -51,7 +54,7 @@ def send(self):
5154
'X-CM-SDK': 'text-sdk-python-' + self.VERSION
5255
}
5356

54-
# Send the message
57+
# Send the message(s)
5558
try:
5659
response = requests.post("https://gw.cmtelecom.com/v1.0/message", data=data, headers=headers)
5760
except Exception as e:
@@ -68,7 +71,6 @@ def encodeData(self, messages):
6871
data = {"messages": {"authentication":{"producttoken": self.apikey}}}
6972
data['messages']['msg'] = []
7073

71-
7274
# For each message do this
7375
for message in messages:
7476
# List all recipients
@@ -86,8 +88,29 @@ def encodeData(self, messages):
8688
}
8789
}
8890

89-
# If message is rich
90-
if message.richContent is not None:
91+
# If message is template
92+
if message.template is not None:
93+
temp["richContent"] = {
94+
"conversation": [{
95+
"template":
96+
{
97+
"whatsapp":
98+
{
99+
"namespace": message.template.namespace,
100+
"element_name": message.template.element_name,
101+
"language":
102+
{
103+
"policy": message.template.language_policy,
104+
"code": message.template.language_code
105+
},
106+
"components": message.template.components
107+
}
108+
}
109+
}]
110+
}
111+
112+
# If message is rich and no template
113+
if message.template is None and message.richContent is not None:
91114
temp["richContent"] = {
92115
"conversation": [{
93116
"text": message.body

CMText/WhatsappTemplate.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from CMText.version import __version__
2+
3+
class WhatsappTemplate:
4+
namespace = ''
5+
element_name = ''
6+
language_policy = ''
7+
language_code = ''
8+
components = []
9+
media = None
10+
11+
# init function of class Message
12+
def __init__(self, namespace, element_name, components=[], media=None, language_policy="deterministic", language_code="en"):
13+
self.namespace = namespace
14+
self.element_name = element_name
15+
self.components = components
16+
self.media = media
17+
self.language_policy = language_policy
18+
self.language_code = language_code
19+

CMText/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#project Version
2-
__version__ = '1.0.5'
2+
__version__ = '1.1.0'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from CMText.TextClient import TextClient
2+
from CMText.WhatsappTemplate import WhatsappTemplate
3+
4+
# Your api-Key
5+
key = 'Your-Key'
6+
7+
# Recipients
8+
to = ['00123456789','00986837265']
9+
10+
# Template
11+
template_namespace = "Your_Template_Namespace"
12+
template_element_name = "Your_Template_Name"
13+
template = WhatsappTemplate(template_namespace, template_element_name)
14+
15+
# Instantiate client with your own api-key
16+
client = TextClient(apikey=key)
17+
18+
# Add message to queue
19+
client.AddWhatsappTemplateMessage(from_='pythonSDK', to=to, template=template)
20+
21+
# Send message
22+
response = client.send()
23+
24+
# Response is an object of type: https://www.w3schools.com/python/ref_requests_response.asp
25+
print(response.text)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from CMText.TextClient import TextClient
2+
from CMText.WhatsappTemplate import WhatsappTemplate
3+
4+
# Your api-Key
5+
key = 'Your-Key'
6+
7+
# Recipients
8+
to = ['00123456789','00986837265']
9+
10+
# Template
11+
template_namespace = "Your_Template_Namespace"
12+
template_element_name = "Your_Template_Name"
13+
template_parameters = [
14+
{
15+
"type": "header",
16+
"parameters": [
17+
{
18+
"type": "image",
19+
"media": {
20+
"mediaName": "template_media_name",
21+
"mediaUri": "https://www.cm.com/cdn/cm/cm.png",
22+
"mimeType": "image/png"
23+
}
24+
}
25+
]
26+
},
27+
{
28+
"type": "body",
29+
"parameters": [
30+
{
31+
"type": "text",
32+
"text": "test string"
33+
},
34+
{
35+
"type": "text",
36+
"text": "test string"
37+
}
38+
]
39+
}]
40+
41+
template = WhatsappTemplate(template_namespace, template_element_name, template_parameters)
42+
43+
# Instantiate client with your own api-key
44+
client = TextClient(apikey=key)
45+
46+
# Add message to queue
47+
client.AddWhatsappTemplateMessage(from_='pythonSDK', to=to, template=template)
48+
49+
# Send message
50+
response = client.send()
51+
52+
# Response is an object of type: https://www.w3schools.com/python/ref_requests_response.asp
53+
print(response.text)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from CMText.TextClient import TextClient
2+
from CMText.WhatsappTemplate import WhatsappTemplate
3+
4+
# Your api-Key
5+
key = 'Your-Key'
6+
7+
# Recipients
8+
to = ['00123456789','00986837265']
9+
10+
# Template
11+
template_namespace = "Your_Template_Namespace"
12+
template_element_name = "Your_Template_Name"
13+
template_parameters = [{
14+
"type": "body",
15+
"parameters": [
16+
{
17+
"type": "text",
18+
"text": "Name"
19+
},
20+
{
21+
"type": "text",
22+
"text": "test"
23+
},
24+
{
25+
"type": "text",
26+
"text": "Order2"
27+
},
28+
{
29+
"type": "text",
30+
"text": "cm.com"
31+
}
32+
]
33+
}]
34+
35+
template = WhatsappTemplate(template_namespace, template_element_name, template_parameters)
36+
37+
# Instantiate client with your own api-key
38+
client = TextClient(apikey=key)
39+
40+
# Add message to queue
41+
client.AddWhatsappTemplateMessage(from_='pythonSDK', to=to, template=template)
42+
43+
# Send message
44+
response = client.send()
45+
46+
# Response is an object of type: https://www.w3schools.com/python/ref_requests_response.asp
47+
print(response.text)

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ By calling `AddRichMessage` and providing `Media`, message text, sender name, re
5555
response = client.send()
5656
```
5757

58+
## Sending a Whatsapp Template message
59+
By calling `AddWhatsappTemplateMessage` and providing `Template`, sender name, recipient phone number(s) you can queue multiple Whatsapp Template messages. Send them by calling `send`.
60+
61+
```cs
62+
template_namespace = "Your-Template-Namespace"
63+
template_element_name = "Replace with Template Name"
64+
template = WhatsappTemplate(template_namespace, template_element_name)
65+
66+
client = TextClient(apikey=key)
67+
client.AddWhatsappTemplateMessage(from_='pythonSDK', to=to, template=template)
68+
response = client.send()
69+
```
70+
71+
See Examples folder for more examples.
72+
5873
## Get the result
5974
Sending a message by calling `send` returns the response body. Response is of type: https://requests.readthedocs.io/en/master/user/quickstart/#response-content
6075
```cs

0 commit comments

Comments
 (0)