AliyunVoice.php
5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 工具类:AliyunVoice 阿里云语音
+-----------------------------------------------------------------------------------------------------------------------
* PHP version 7
*
* @category App\Util
* @package App\Util
* @author Richer <yangzi1028@163.com>
* @date 2021年9月1日15:44:32
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Util;
use Illuminate\Http\Request;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Dyvmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\SingleCallByVoiceRequest;
/**
* Class MasSms
*
* @category App\Sms
* @package App\Sms
* @author Richer <yangzi1028@163.com>
* @date 2021年8月19日10:04:55
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
/**
* Class AliyunVoice
*
* @category App\Util
* @package App\Util
* @author Richer <yangzi1028@163.com>
* @date 2021年9月1日15:44:32
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class AliyunVoice
{
protected $message = '短信发送成功!';
/**
* 项目常量配置,
*/
private $accessKeyId = "LTAI5t7UJ2LZoRSGP65iUDEE"; // vms@1348923735754837.onaliyun.com
private $accessKeySecret = "你的阿里云secret";
private $dyvmsApiClient = "";
/**
* 初始化操作: 读取 config, 设定配置参数
* 如果没有配置, 则使用默认
*/
public function __construct()
{
$this->accessKeyId = config('mas.ACCESS_KEY_ID', 'LTAI5t7UJ2LZoRSGP65iUDEE');
$this->accessKeySecret = config('mas.ACCESS_KEY_SECRET', '6MfTOAs4gL5ig1I9qb3wgllj44fDyp');
$config = new Config([
// 您的AccessKey ID
"accessKeyId" => $this->accessKeyId ,
// 您的AccessKey Secret
"accessKeySecret" => $this->accessKeySecret
]);
// 访问的域名
$config->endpoint = "dyvmsapi.aliyuncs.com";
$this->dyvmsApiClient = new Dyvmsapi($config);
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dyvmsapi Client
*/
public static function createClient($accessKeyId, $accessKeySecret)
{
$config = new Config([
// 您的AccessKey ID
"accessKeyId" => $accessKeyId,
// 您的AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// 访问的域名
$config->endpoint = "dyvmsapi.aliyuncs.com";
return new Dyvmsapi($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args)
{
$client = self::createClient("accessKeyId", "accessKeySecret");
$singleCallByVoiceRequest = new SingleCallByVoiceRequest([
"calledNumber" => "15956278880",
//"voiceCode" => "100000051350368"
"voiceCode" => "100000052430019"
]);
// 复制代码运行请自行打印 API 的返回值
$client->singleCallByVoice($singleCallByVoiceRequest);
}
/**
* @param Request $request
*/
public function send(Request $request)
{
$input = $request->all();
$params = array ();
// *** 需用户填写部分 ***
// fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
$accessKeyId = "LTAI5t7UJ2LZoRSGP65iUDEE"; // vms@1348923735754837.onaliyun.com
$accessKeySecret = "你的阿里云secret";
// fixme 必填: 被叫显号
$params["CalledShowNumber"] = "这里填写被叫显示的号码";
// fixme 必填: 语音接收号码
$params["CalledNumber"] = $input['phone'];
// fixme 必填: Tts模板Code
$params["TtsCode"] = "这里填写模板你的code";
// fixme 选填: Tts模板中的变量替换JSON,假如Tts模板中存在变量,则此处必填
$params["TtsParam"] = array(
"code" => $input['code'] //这里填写你的模板变量,根据自己的模板需要填写
);
// fixme 选填: 音量
$params["Volume"] = 100;
// fixme 选填: 播放次数
$params["PlayTimes"] = 3;
// fixme 选填: 音量, 取值范围 0~200
$params["Volume"] = 100;
// fixme 选填: 预留给调用方使用的ID, 最终会通过在回执消息中将此ID带回给调用方
$params["OutId"] = "";
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
if (!empty($params["TtsParam"]) && is_array($params["TtsParam"])) {
$params["TtsParam"] = json_encode($params["TtsParam"], JSON_UNESCAPED_UNICODE);
}
// 初始化SignatureHelper实例用于设置参数,签名以及发送请求
$helper = new SignatureHelper();
// 此处可能会抛出异常,注意catch
$result = $helper->request(
$accessKeyId,
$accessKeySecret,
"dyvmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SingleCallByTts",
"Version" => "2017-05-25",
))
// fixme 选填: 启用https
// ,true
);
if ($result->Code == 'OK') {
echo "语音发送成功";
} else {
echo "语音发送失败";
}
}
}