thinkstudeo/laravel-textlocal

Textlocal API的Laravel封装

1.0.0 2019-04-22 06:23 UTC

This package is auto-updated.

Last update: 2024-09-22 19:03:48 UTC


README

Latest Version on Packagist Software License Total Downloads

这是一个用于事务性以及促销账户的textlocal.in API请求的便捷封装。目前尚不支持特定于经销商账户的API。

短信消息可用于事务性消息,例如:

  • 发送用于双因素认证的OTP
  • 确认收到付款或下单等

事务性消息需要在任何时间发送,不受任何DND(勿扰)限制。为此,需要向Textlocal注册事务性账户或请求将默认账户转换为事务性账户。

另一方面,短信消息也可用于促销活动,例如:

  • 宣布产品发布或折扣销售
  • 宣布公司或产品获得奖项
  • 发送优惠券代码等

促销消息必须遵守DND(勿扰)限制。Textlocal默认创建的是促销账户。

内容

安装

$ composer require thinkstudeo/textlocal

配置

该包使得同时使用Textlocal的事务性账户和促销账户变得容易。

将以下内容添加到您的config/services.php文件中:

'textlocal' => [
    'transactional' => [
		'apiKey' => env('TEXTLOCAL_TRANSACTIONAL_KEY')
    ],
    'promotional' => [
        'apiKey' => env('TEXTLOCAL_PROMOTIONAL_KEY')
    ]
]

别忘了将密钥添加到您的.env文件中

如果您只有一个账户,即促销账户或事务性账户,请确保正确输入API密钥。例如,对于仅有的Textlocal促销账户,声明TEXTLOCAL_PROMOTIONAL_KEY=您的促销账户API密钥

TEXTLOCAL_TRANSACTIONAL_KEY= <your textlocal transactional account api key here>
TEXTLOCAL_PROMOTIONAL_KEY= <your textlocal promotional account api key here>

使用方法

该包包含两个门面

  • Sms 用于发送消息
  • Account 用于与Textlocal账户交互

为了区分不同账户类型的API调用,请使用transactional()promotional()来设置API调用的账户。

消息 - 事务性账户

您必须有Textlocal的事务性账户,才能在24x7无DND限制的情况下发送事务性短信。此外,别忘了让Textlocal批准您的事务性消息模板。最后,您必须从Textlocal仪表板生成API密钥(API KEY

然后您可以使用门面如下:

向单个用户发送消息

Sms::transactional()
    ->to('911234523451')
    ->from('SENDER')   //Your registered sender
    ->send('My message');  //as per your approved template

一次性向多个用户发送消息

Sms::transactional()
    ->to('911234523451,919898456456')
    ->from('SENDER')   //Your registered sender
    ->send('My message');  //as per your approved template

或者,您也可以提供数字数组

Sms::transactional()
    ->to(['911234523451', '919898456456'])
    ->from('SENDER')   //Your registered sender
    ->send('My message');  //as per your approved template

如果您想安排在未来的某个时间发送消息,您可以通过Carbon支持的任何格式提供datetime字符串,或者您也可以提供Unix时间戳。

$schedule = "2019-01-01 09:30:00"; 

Sms::transactional()
    ->to('911234523451,919898456456')
    ->at($schedule) 
    ->from('SENDER')   //Your registered sender
    ->send('My message');  //as per your approved template

取消已安排的消息

//Get a list of all the scheduled messages
$response = Account::transactional()->scheduledMessages();
$scheduledMessages = $response->scheduled;

//Identify the scheduled message you want to cancel
$msg = $scheduledMessages[0];

//Cancel the message
Account::transactional()->cancel($msg->id);

消息 - 促销账户

向单个用户发送消息

Sms::promotional()
    ->to('911234523451')
    ->from('TXTLCL')   //or Your registered sender
    ->send('My message');  

一次性向多个用户发送消息

Sms::promotional()
    ->to('911234523451,919898456456')
    ->from('TXTLCL')   //or Your registered sender
    ->send('My message');  

或者,您也可以提供数字数组

Sms::promotional()
    ->to(['911234523451', '919898456456'])
    ->from('TXTLCL')   //or Your registered sender
    ->send('My message');  

如果您想安排在未来的某个时间发送消息,您可以通过Carbon支持的任何格式提供datetime字符串,或者您也可以提供Unix时间戳。

$schedule = "2019-01-01 09:30:00";

Sms::promotional()
    ->to('911234523451,919898456456')
    ->at($schedule) //unix timestamp
    ->from('TXTLCL')   //or Your registered sender
    ->send('My message');  //as per your approved template

取消已安排的消息

//Get a list of all the scheduled messages
$response = Account::promotional()->scheduledMessages();
$scheduledMessages = $response->scheduled;

//Identify the scheduled message you want to cancel
$msg = $scheduledMessages[0];

//Cancel the message
Account::promotional()->cancel($msg->id);

消息状态

获取已发送消息的状态详情

//Capture the message id and or batch id when you send the message
$response = Sms::promotional()->to(['911234523451', '919898456456'])->from('TXTLCL')->send('My Message');
$msgId = $response->messages[1]->id;
$batchId = $response->batch_id;

//Get the status
Account::promotional()->messageStatus($msgId);
//Or
Account::promotional()->batchStatus($batchId);

收件箱中的收到的消息

从特定的收件箱检索收到的消息

//Get a list of all inboxes in your account
$inboxList = Account::promotional()->inboxes();

//Identify the inbox you want to fetch the messages from, and get its id
$inboxId = $inboxList[0]->id;

//Fetch the messages from the inbox
$messages = Account::promotional()->messages($inboxId);

账户中的模板

获取与账户关联的所有模板

$templates = Account::transactional()->templates();

发送者名称

从您的账户获取所有已批准的发送者名称

$senders = Account::transactional()->senders();

账户余额

获取当前账户余额(剩余信用额度)

$balance = Account::promotional()->balance();

检查关键词的可用性

$availability = Account::promotional()->checkKeyword('KEYWRD');

检查是否存在联系人组

$exists = Account::promotional()->groupExists('Customers')

创建新的联系人组

$groupId = Account::promotional()->createGroup('Tech')->group->id;

获取所有组列表

$groupList = Account::promotional()->groups();

删除联系人组

//Note the id of the group when you create a new one
$customersId = Account::promotional()->createGroup('Customers')->group->id;
//Or get a list of all groups
$groupList = Account::promotional()->groups();
//Get the id of the group you want to delete
$groupId = $groupList[0]->id;

//Delete the group
$status = Account::promotional()->deleteGroup($customerId);
//Or
$status = Account::promotional()->deleteGroup($groupId);
//Or just give the name of the group to the command
$status = Account::promotional()->deleteGroup('Customers');

联系人组所有成员列表

$groupId = '66455';
$members = Account::promotional()->members($groupId);

向现有联系人组添加号码

$groupId = '66455';
$numbers = '919033100026,919879612326';
$response = Account::promotional()->addNumbers($numbers, $groupId);

向现有组添加联系人

$groupId = '66455';

$members = [
	['number' => '911234567894', 'first_name' => 'John', 'last_name' => 'Doe'],
	['number' => '913355667798', 'first_name' => 'Jane', 'last_name' => 'Mclane']
];

$response = Account::promotional()->addMembers($members);

从组中移除联系人

$response = Account::promotional()->removeMember('913355667798', $groupId);

获取所有已选择退出的列表

$response = Account::promotional()->optOuts();

历史记录

//Single message histr=ory
$response = Account::promotional()->history('single');

//Group message history
$response = Account::promotional()->history('group');

//Api message history
$response = Account::promotional()->history('api');

调查

获取所有活跃调查的列表

$response = Account::promotional()->surveys();

获取特定调查的详细信息

//Get a list of active surveys
$surveys = Account::promotional()->surveys();
//Identify the survey for which you want the details
$surveyId = $surveys->survey_ids[0]->id;
//Fetch the details
$response = Account::promotional()->surveyDetails($surveyId);

获取特定调查的结果

//Get a list of active surveys
$surveys = Account::promotional()->surveys();
//Identify the survey for which you want the results
$surveyId = $surveys->survey_ids[0]->id;
//Fetch the results
$response = Account::promotional()->surveyResults($surveyId);

变更日志

请参阅变更日志以获取更多关于最近更改的信息。

测试

请注意,所有测试都是集成测试,这意味着它们将实际调用textlocal API并消耗信用额度。

$ composer test

安全性

如果您发现任何安全相关的问题,请通过neerav@thinkstudeo.com 发送电子邮件,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅贡献指南

致谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件