- Notifications
You must be signed in to change notification settings - Fork6
PHP SDK to send messages with CM.com
License
cmdotcom/text-sdk-php
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A software development kit to provide ways to interact with CM.com's Text service. API used:
- php 7.1 through 8.4 (inclusive)
Using your uniqueApiKey
(or product token) which authorizes you on the CM platform. Always keep this key secret!
The product token can be found in theChannels application on the platform, under theGateway
section.
$client =new \CMText\TextClient('your-api-key');
By callingSendMessage
and providing message text, sender name, recipient phone number(s) and a reference (optional).
$result =$client->SendMessage('Message_Text','CM.com', ['Recipient_PhoneNumber' ],'Your_Reference');
SendMessage
andsend
return an object of typeTextClientResult
, example:
{"statusMessage":"Created 1 message(s)","statusCode":201,"details": [ {"reference":"Example_Reference","status":"Accepted","to":"Example_PhoneNumber","parts":1,"details":null }, {"reference":"Example_Reference2","status":"Rejected","to":"Example_PhoneNumber2","parts":0,"details":"A body without content was found" } ]}
For all possibly returned status codes, please reference theTextClientStatusCodes
class.
By using theMessage
class it is possible to create messages with media for channels such as WhatsApp and RCS
$client =newTextClient('your-api-key');$message =newMessage('Message Text','Sender_name', ['Recipient_PhoneNumber']);$message ->WithChannels([Channels::WHATSAPP]) ->WithHybridAppKey('your-secret-hybrid-app-key') ->WithRichMessage(newMediaMessage('cm.com','https://avatars3.githubusercontent.com/u/8234794?s=200&v=4','image/png' ) ) ->WithSuggestions([newReplySuggestion('Opt In','OK'),newReplySuggestion('Opt Out','STOP'), ]);$result =$client->send( [$message] );
By using theMessage
class it is possible to create template messages. Please note that this is WhatsApp only and your template needs to be approved before sending.For more info please check our documentation:https://www.cm.com/en-en/app/docs/api/business-messaging-api/1.0/index#whatsapp-template-message
$client =newTextClient('your-api-key');$message =newMessage('Message Text','Sender_name', ['Recipient_PhoneNumber']);$message ->WithChannels([Channels::WHATSAPP]) ->WithTemplate(newTemplateMessage(newWhatsappTemplate('namespace','elementname',newLanguage('en'), [newComponentBody([newComponentParameterText('firstname') ]) ] ) ) );$result =$client->send( [$message] );
It is also possible to send a rich template with an image!
$client =newTextClient('your-api-key');$message =newMessage('Message Text','Sender_name', ['Recipient_PhoneNumber']);$message ->WithChannels([Channels::WHATSAPP]) ->WithTemplate(newTemplateMessage(newWhatsappTemplate('template-name','the-namespace-of-template',newLanguage('en'), [newComponentHeader([newComponentParameterImage(newMediaContent('image name','https://image.location','image/png' ) ) ]),newComponentBody([newComponentParameterText('firstname') ]) ] ) ) );$result =$client->send( [$message] );
It is now possible to send an apple pay request only possible in Apple Business Chat
$client =newTextClient('your-api-key');$message =newMessage('Message Text','Sender_name', ['Recipient_PhoneNumber']);$message ->WithChannels([Channels::IMESSAGE]) ->WithPayment(newPaymentMessage(newApplePayConfiguration('merchant-name','product-description','unique-order-guid',1,'currency-code','recipient-email','recipient-country-code','language-country-code',true,true, [newLineItem('product-name','final-or-pending',1 ) ] ) ) );$result =$client->send( [$message] );
It is now possible to send list messages and reply buttons without using templatesonly supported in WhatsApp
$client =newTextClient('your-api-key');$message =newMessage('Message Text','Sender_name', ['Recipient_PhoneNumber']);$message ->WithChannels([Channels::WHATSAPP]) ->WithRichMessage(newWhatsAppInteractiveMessage(newWhatsAppInteractiveContent( WhatsAppInteractiveContentTypes::LIST,newWhatsAppInteractiveHeader( WhatsAppInteractiveHeaderTypes::TEXT,'List message example' ),newWhatsAppInteractiveBody('checkout our list message demo'),newWhatsAppInteractiveListAction('Descriptive list title', [newWhatsAppInteractiveSection('Select an option', [newWhatsAppInteractiveSectionRow('unique title 1',rand(),'description text' ),newWhatsAppInteractiveSectionRow('unique title 2',rand() )] )] ),newWhatsAppInteractiveFooter('footer text') ) ) );$result =$client->send( [$message] );
Only with Reply buttons you can send media like image,video or documentsee following example.
$client =newTextClient('your-api-key');$message =newMessage('Message Text','Sender_name', ['Recipient_PhoneNumber']);$message ->WithChannels([Channels::WHATSAPP]) ->WithRichMessage(newWhatsAppInteractiveMessage(newWhatsAppInteractiveContent( WhatsAppInteractiveContentTypes::BUTTON,newWhatsAppInteractiveHeader( WhatsAppInteractiveHeaderTypes::IMAGE,null,newMediaContent('media name','media.url','mime/type' ) ),newWhatsAppInteractiveBody('checkout our list message demo'),newWhatsAppInteractiveButtonAction( [newWhatsAppInteractiveReplyButton('button 1 reply-text',rand() ),newWhatsAppInteractiveReplyButton('button 2 title',rand() )] ),newWhatsAppInteractiveFooter('footer text') ) ) );$result =$client->send( [$message] );
About
PHP SDK to send messages with CM.com