全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网 钉钉智能硬件
消息服务

主题——发布消息

更新时间:2017-06-07 13:26:11

本文档介绍使用PythonSDK发布主题消息的示例代码,当需要将消息推送到邮箱或者以短信的方式推送到指定的手机,需要在发布消息设置额外的属性,具体设置方式参考代码。

  1. #you can get $accountid from https://account.console.aliyun.com/#/secure
  2. #you can get $accid and $acckey from https://ak-console.aliyun.com/#/accesskey
  3. #you can generate $endpoint: http://$accountid.mns.cn-hangzhou.aliyuncs.com, eg. http://1234567890123456.mns.cn-hangzhou.aliyuncs.com
  4. my_account = Account("$endpoint", "$accid", "$acckey")
  5. topic_name = "TestTopic"
  6. my_topic = my_account.get_topic(topic_name)
  7. #attributes for Mail
  8. direct_mail = DirectMailInfo(account_name="direct_mail_account_name@aliyun-inc.com", subject="TestMailSubject", address_type=0, is_html=0, reply_to_address=0)
  9. #attributes for SMS
  10. direct_sms = DirectSMSInfo(free_sign_name="SignName", template_code="TemplateCode", single=False)
  11. direct_sms.add_receiver(receiver="$phone1", params={"name": "Tom"})
  12. direct_sms.add_receiver(receiver="$phone2", params={"name": "David"})
  13. #init TopicMessage
  14. msg_body = "I am test message."
  15. msg = TopicMessage(msg_body, "msg_tag", direct_mail, direct_sms)
  16. try:
  17. re_msg = my_topic.publish_message(msg)
  18. print "Publish Message Succeed. MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id)
  19. except MNSExceptionBase,e:
  20. if e.type == "TopicNotExist":
  21. print "Topic not exist, please create it."
  22. sys.exit(1)
  23. print "Publish Message Fail. Exception:%s" % e
本文导读目录