93 lines
3.3 KiB
C#
93 lines
3.3 KiB
C#
using Aliyun.MNS;
|
||
using Aliyun.MNS.Model;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.CRMServices.SMS.InterfaceModel;
|
||
|
||
namespace WX.CRM.CRMServices.SMS.InterfaceExec
|
||
{
|
||
internal class AliYunSms : IMsgSend
|
||
{
|
||
AliYunSmsConfig _config;
|
||
public AliYunSms(string _account = "", string _password = "")
|
||
{
|
||
_config = new AliYunSmsConfig();
|
||
|
||
}
|
||
public InterfaceModel.ReturnResult Execute(string[] mobiles, string msg)
|
||
{
|
||
return Send(mobiles, msg);
|
||
}
|
||
|
||
public decimal queryAmt()
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
InterfaceModel.ReturnResult Send(string[] mobiles, string msg)
|
||
{
|
||
string mobilesStr = string.Join(",", mobiles);
|
||
ReturnResult result = new ReturnResult();
|
||
result.SendStr = mobilesStr;
|
||
|
||
Dictionary<string, string> ndic = new Dictionary<string, string>();
|
||
|
||
/**
|
||
* Step 1. 初始化Client
|
||
*/
|
||
IMNS client = new Aliyun.MNS.MNSClient(_config.appkey, _config.appSecret, _config.endpoint);
|
||
/**
|
||
* Step 2. 获取主题引用
|
||
*/
|
||
Topic topic = client.GetNativeTopic(_config.topicName);
|
||
/**
|
||
* Step 3. 生成SMS消息属性
|
||
*/
|
||
MessageAttributes messageAttributes = new MessageAttributes();
|
||
BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
|
||
// 3.1 设置发送短信的签名:SMSSignName
|
||
batchSmsAttributes.FreeSignName = _config.sinName;
|
||
// 3.2 设置发送短信的模板SMSTemplateCode
|
||
batchSmsAttributes.TemplateCode = _config.msgTempleteCode;
|
||
Dictionary<string, string> param = new Dictionary<string, string>();
|
||
// 3.3 (如果短信模板中定义了参数)设置短信模板中的参数,发送短信时,会进行替换
|
||
param.Add("password", msg);
|
||
// 3.4 设置短信接收者手机号码
|
||
foreach (string phone in mobiles)
|
||
{
|
||
batchSmsAttributes.AddReceiver(phone, param);
|
||
}
|
||
|
||
messageAttributes.BatchSmsAttributes = batchSmsAttributes;
|
||
PublishMessageRequest request = new PublishMessageRequest();
|
||
request.MessageAttributes = messageAttributes;
|
||
/**
|
||
* Step 4. 设置SMS消息体(必须)
|
||
*
|
||
* 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
|
||
*/
|
||
request.MessageBody = "m";
|
||
try
|
||
{
|
||
/**
|
||
* Step 5. 发布SMS消息
|
||
*/
|
||
PublishMessageResponse resp = topic.PublishMessage(request);
|
||
//Console.WriteLine(resp.MessageId);
|
||
//LogHelper.Info("MessageId:" + resp.MessageId);
|
||
//LogHelper.Info("HttpStatusCode:" + resp.HttpStatusCode.ToString());
|
||
if (!string.IsNullOrEmpty(resp.MessageId))
|
||
result.submitStatus = SubmitStatus.success;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result.submitStatus = SubmitStatus.fail;
|
||
LogHelper.Error(ex.ToString());
|
||
}
|
||
return result;
|
||
}
|
||
|
||
}
|
||
}
|