作者:E4b9a6, 创建:2020-07-17, 字数:3593, 已阅:34, 最后更新:2020-07-17
记录下这个艰难的过程顺便吐槽下阿里云糟糕的文档,并不全面,仅仅指出其中难点,如有问题还需要仔细看糟糕的文档.
首先参考阿里云短信发送API文档
按照文档我们需要申请下述资料
申请完毕之后就可以开始了
请注意,大部分linux发行版目前都是2.7版本python,也有部分3.0以上版本,目前暂不支持3.0版本(2017年12月)
下载阿里云的SDK,下载完成后解压
切换到解压后的目录运行 python setup.py install进行依赖安装,安装完成后就可以开始写你自己的消息通知例子
Tips-1:代码中包含$字眼的需要自行替换为你的服务资料
Tips-2:step3跟step4是一样的,写法不同(我也不知道阿里云API文档为什么要这么写)
# -*- encoding:utf-8 -*-
import sys
import time
from mns.account import Account
from mns.queue import *
from mns.topic import *
from mns.subscription import *
import ConfigParser
import config
def send_sms(serverAddr,errorInfo):
'''
Step 1. 获取主题引用
'''
# 从https://account.console.aliyun.com/#/secure获取$YourAccountid
# 从https://ak-console.aliyun.com/#/accesskey获取$YourAccessId和$YourAccessKey
# 从http://$YourAccountId.mns.cn-hangzhou.aliyuncs.com获取$YourMNSEndpoint, eg. http://1234567890123456.mns.cn-hangzhou.aliyuncs.com
my_account = Account("$YourAccountid", "$YourAccessId", "$YourMNSEndpoint")
my_topic = my_account.get_topic("$yourtopic")
'''
Step 2. 设置SMS消息体(必须)
注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可 '''
msg_body1 = "sms-message1."
#msg_body2 = "sms-message2."
'''
Step 3. 生成SMS消息属性,single=False表示每个接收者参数不一样,
'''
# 3.1 设置SMSSignName和SMSTempateCode
direct_sms_attr1 = DirectSMSInfo(free_sign_name="$you_rsign_name", template_code="$yourtemplate_code", single=False)
# 3.2 指定接收短信的手机号并指定发送给该接收人的短信中的参数值(在短信模板中定义的)
# 此处的循环可不写,如有多人,建议写配置文件
for phone in config.PHONES:
print("准备发送短信到"+ str(phone) + ",消息内容为:ServerName:" + str(serverAddr) + "错误信息:" + str(errorInfo) )
direct_sms_attr1.add_receiver(receiver=str(phone), params={"servername": str(serverAddr),"errordesc":str(errorInfo)})
#direct_sms_attr1.add_receiver(receiver=str(phone), params={"servername": "啊哈哈哈","errordesc":"测试"})
#direct_sms_attr1.add_receiver(receiver="$YourReceiverPhoneNumber2", params={"$YourSMSTemplateParamKey1": "$Value2"})
'''
Step 4. 生成SMS消息属性,single=True表示每个接收者参数一样
'''
# direct_sms_attr2 = DirectSMSInfo(free_sign_name="$your_sign_name", template_code="$your_template_code", single=True)
# for phone in config.PHONES:
# print("准备发送短信到"+ str(phone) + ",消息内容为:ServerName:" + str(serverAddr) + "错误信息:" + str(errorInfo) )
# direct_sms_attr2.add_receiver(receiver=str(phone))
# #direct_sms_attr2.add_receiver(receiver="$YourReceiverPhoneNumber2")
# direct_sms_attr2.set_params({"servername": str(serverAddr),"errordesc":str(errorInfo)})
'''
#Step 5. 生成SMS消息对象
'''
msg1 = TopicMessage(msg_body1, direct_sms = direct_sms_attr1)
#msg2 = TopicMessage(msg_body2, direct_sms = direct_sms_attr2)
try:
'''
Step 6. 发布SMS消息
'''
re_msg = my_topic.publish_message(msg1)
print "Publish Message Succeed. MessageBody:%s MessageID:%s" % (msg_body1, re_msg.message_id)
#re_msg = my_topic.publish_message(msg2)
#print "Publish Message Succeed. MessageBody:%s MessageID:%s" % (msg_body2, re_msg.message_id)
except MNSExceptionBase,e:
if e.type == "TopicNotExist":
print "Topic not exist, please create it."
sys.exit(1)
print "Publish Message Fail. Exception:%s" % e
如需要更多例子,可参考sdk中的sample.py,更垃圾完善