This topic describes how to use the SDK for Python to publish messages to a topic. To push messages to email addresses or mobile numbers, you must specify additional parameters. For more information about how to specify the parameters, see the following sample code:

#you can get $accountid from
#you can get $accid and $acckey from
#you can generate $endpoint: http://$, eg.
my_account = Account("$endpoint", "$accid", "$acckey")
topic_name = "TestTopic"
my_topic = my_account.get_topic(topic_name)

#attributes for Mail
direct_mail = DirectMailInfo(account_name="", subject="TestMailSubject", address_type=0, is_html=0, reply_to_address=0)

#attributes for SMS
direct_sms = DirectSMSInfo(free_sign_name="SignName", template_code="TemplateCode", single=False)
direct_sms.add_receiver(receiver="$phone1", params={"name": "Tom"})
direct_sms.add_receiver(receiver="$phone2", params={"name": "David"})

#init TopicMessage
msg_body = "I am test message."
msg = TopicMessage(msg_body, "msg_tag", direct_mail, direct_sms)
    re_msg = my_topic.publish_message(msg)
    print "Publish Message Succeed. MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id)
except MNSExceptionBase,e:
    if e.type == "TopicNotExist":
        print "Topic not exist, please create it."
    print "Publish Message Fail. Exception:%s" % e