すべてのプロダクト
Search
ドキュメントセンター

:PublishMessage

最終更新日:Mar 22, 2020

トピックにMailEndpointまたはSmsEndpointが含まれている場合、トピックメッセージの属性を追加指定します。

次のコードは、python sdkで属性を設定する方法を示しています。

  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