11from CMText .Gateways import Gateways
22from CMText .Message import Message
3+ from CMText .WhatsappTemplate import WhatsappTemplate
34from CMText .version import __version__
45import json
56import requests
67
7-
8-
98class 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
0 commit comments