cmdotcom/text-sdk-php
Composer 安装命令:
composer require cmdotcom/text-sdk-php
包简介
PHP SDK to send messages with CM.com
README 文档
README
CM Text SDK
A software development kit to provide ways to interact with CM.com's Text service. API used:
Requirements
- php 7.1 through 8.5 (inclusive)
Usage
Instantiate the client
Using your unique ApiKey (or product token) which authorizes you on the CM platform. Always keep this key secret!
The product token can be found in the Channels application on the platform, under the Gateway section.
$client = new \CMText\TextClient('your-api-key');
Send a message
By calling SendMessage 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');
Get the result
SendMessage and send return an object of type TextClientResult, 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"
}
]
}
Status codes
For all possibly returned status codes, please reference the TextClientStatusCodes class.
Sending a rich message
By using the Message class it is possible to create messages with media for channels such as WhatsApp and RCS
$client = new TextClient('your-api-key'); $message = new Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']); $message ->WithChannels([Channels::WHATSAPP]) ->WithHybridAppKey('your-secret-hybrid-app-key') ->WithRichMessage( new MediaMessage( 'cm.com', 'https://avatars3.githubusercontent.com/u/8234794?s=200&v=4', 'image/png' ) ) ->WithSuggestions([ new ReplySuggestion('Opt In', 'OK'), new ReplySuggestion('Opt Out', 'STOP'), ]); $result = $client->send( [$message] );
Sending a WhatsApp template message
By using the Message 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 = new TextClient('your-api-key'); $message = new Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']); $message ->WithChannels([Channels::WHATSAPP]) ->WithTemplate( new TemplateMessage( new WhatsappTemplate( 'namespace', 'elementname', new Language('en'), [ new ComponentBody([ new ComponentParameterText('firstname') ]) ] ) ) ); $result = $client->send( [$message] );
Sending a rich WhatsApp template message
It is also possible to send a rich template with an image!
$client = new TextClient('your-api-key'); $message = new Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']); $message ->WithChannels([Channels::WHATSAPP]) ->WithTemplate( new TemplateMessage( new WhatsappTemplate( 'template-name', 'the-namespace-of-template', new Language('en'), [ new ComponentHeader([ new ComponentParameterImage( new MediaContent( 'image name', 'https://image.location', 'image/png' ) ) ]), new ComponentBody([ new ComponentParameterText('firstname') ]) ] ) ) ); $result = $client->send( [$message] );
Sending an Apple Pay Request
It is now possible to send an apple pay request only possible in Apple Business Chat
$client = new TextClient('your-api-key'); $message = new Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']); $message ->WithChannels([Channels::IMESSAGE]) ->WithPayment( new PaymentMessage( new ApplePayConfiguration( 'merchant-name', 'product-description', 'unique-order-guid', 1, 'currency-code', 'recipient-email', 'recipient-country-code', 'language-country-code', true, true, [ new LineItem( 'product-name', 'final-or-pending', 1 ) ] ) ) ); $result = $client->send( [$message] );
Sending WhatsApp interactive messages
It is now possible to send list messages and reply buttons without using templates only supported in WhatsApp
$client = new TextClient('your-api-key'); $message = new Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']); $message ->WithChannels([Channels::WHATSAPP]) ->WithRichMessage( new WhatsAppInteractiveMessage( new WhatsAppInteractiveContent( WhatsAppInteractiveContentTypes::LIST, new WhatsAppInteractiveHeader( WhatsAppInteractiveHeaderTypes::TEXT, 'List message example' ), new WhatsAppInteractiveBody('checkout our list message demo'), new WhatsAppInteractiveListAction( 'Descriptive list title', [new WhatsAppInteractiveSection( 'Select an option', [new WhatsAppInteractiveSectionRow( 'unique title 1', rand(), 'description text' ),new WhatsAppInteractiveSectionRow( 'unique title 2', rand() )] )] ), new WhatsAppInteractiveFooter('footer text') ) ) ); $result = $client->send( [$message] );
Only with Reply buttons you can send media like image,video or document see following example.
$client = new TextClient('your-api-key'); $message = new Message('Message Text', 'Sender_name', ['Recipient_PhoneNumber']); $message ->WithChannels([Channels::WHATSAPP]) ->WithRichMessage( new WhatsAppInteractiveMessage( new WhatsAppInteractiveContent( WhatsAppInteractiveContentTypes::BUTTON, new WhatsAppInteractiveHeader( WhatsAppInteractiveHeaderTypes::IMAGE, null, new MediaContent( 'media name', 'media.url', 'mime/type' ) ), new WhatsAppInteractiveBody('checkout our list message demo'), new WhatsAppInteractiveButtonAction( [new WhatsAppInteractiveReplyButton( 'button 1 reply-text', rand() ),new WhatsAppInteractiveReplyButton( 'button 2 title', rand() )] ), new WhatsAppInteractiveFooter('footer text') ) ) ); $result = $client->send( [$message] );
cmdotcom/text-sdk-php 适用场景与选型建议
cmdotcom/text-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 732.49k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2018 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sms」 「twitter」 「line」 「instagram」 「whatsapp」 「wechat」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cmdotcom/text-sdk-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cmdotcom/text-sdk-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cmdotcom/text-sdk-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Handles basic OAuth/OAuth2 authentication along with classes for common services
Simple Twitter API wrapper
Simple Sharing generates social media share links within CP entry pages, allowing you to quickly & easily share entries.
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Extensible library for building notifications and sending them via different delivery channels.
A PHP console tool to generate Markdown documentation from your docblocks and types hints using templates.
统计信息
- 总下载量: 732.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 33
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-27