说明
Amazon支持SMS/MMS功能,根据官方提供的API接口是需要SDK来支持的。如下是SDK API相关介绍:
发布到移动电话 - Amazon Simple Notification Service
获取SDK
git clone https://github.com/aws/aws-sdk-cpp.git
设置region、keyId及accessKey
setenv("AWS_DEFAULT_REGION", region, 1);
setenv("AWS_ACCESS_KEY_ID", keyId, 1);
setenv("AWS_SECRET_ACCESS_KEY", accessKey, 1);
SDK调用
static int amazonSendMsg(const char *to, const char *body)
{
int retval = -1;
Aws::SDKOptions options;
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
#if defined(__arm__) || defined(__aarch64__)
openlog("smstool", LOG_ODELAY, LOG_USER); //启用syslog功能
#endif
/*! 使用Lambada配置SDK log级别 */
options.loggingOptions.logger_create_fn = []() {
return Aws::MakeShared<LogObject>("Client", Aws::Utils::Logging::LogLevel::Trace);
};
Aws::InitAPI(options);
#if defined(__arm__) || defined(__aarch64__)
Aws::Client::ClientConfiguration clientConfig;
clientConfig.caFile = CA_FILE; //导入证书
Aws::SNS::SNSClient sns(clientConfig);
#else
Aws::SNS::SNSClient sns;
#endif
Aws::String message = body;
Aws::String phone_number = to;
Aws::SNS::Model::PublishRequest psms_req;
psms_req.SetMessage(message);
psms_req.SetPhoneNumber(phone_number);
auto psms_out = sns.Publish(psms_req);
if (psms_out.IsSuccess()) {
retval = 0;
logOut("Message published successfully %s", psms_out.GetResult().GetMessageId());
} else {
logOut("Error while publishing message %s", psms_out.GetError().GetMessage());
}
Aws::ShutdownAPI(options);
#if defined(__arm__) || defined(__aarch64__)
closelog();
#endif
return retval;
}