使用阿里雲訊息服務mns傳送簡訊

2021-08-05 19:58:16 字數 4222 閱讀 2806

'''step 1. 獲取主題引用

'''# 從獲取$youraccountid

# 從獲取$youraccessid和$youraccesskey

# 從http://$youraccountid.mns.cn-hangzhou.aliyuncs.com獲取$yourmnsendpoint, eg.

my_account = account("$yourmnsendpoint", "$youraccessid", "$youraccesskey")

my_topic = my_account.get_topic("$yourtopicname")

'''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="$yoursignname", template_code="$yoursmstemplatecode", single=false)

# 3.2 指定接收簡訊的手機號並指定傳送給該接收人的簡訊中的引數值(在簡訊模板中定義的)

direct_sms_attr1.add_receiver(receiver="$yourreceiverphonenumber1", params=)

direct_sms_attr1.add_receiver(receiver="$yourreceiverphonenumber2", params=)

'''step 4. 生成sms訊息屬性,single=true表示每個接收者引數一樣

'''direct_sms_attr2 = directsmsinfo(free_sign_name="$yoursignname", template_code="$yoursmstemplatecode", single=true)

direct_sms_attr2.add_receiver(receiver="$yourreceiverphonenumber1")

direct_sms_attr2.add_receiver(receiver="$yourreceiverphonenumber2")

direct_sms_attr2.set_params()

'''#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 mn***ceptionbase,e:

if e.type == "topicnotexist":

print

"topic not exist, please create it."

sys.exit(1)

print

"publish message fail. exception:%s" % e

# -*- coding: utf-8 -*-

import os

import random

from mns.account import account

from mns.topic import directsmsinfo, topicmessage

from mns.mns_exception import mn***ceptionbase

from mns.mns_tool import mnslogger

'''step 1. 獲取主題引用

'''# mnslogger.get_logger的引數:

# log_name: 預設為 mns_python_sdk

# log_file: 預設為 mns所安裝路徑下的mns_python_sdk.log檔案, 如果不是root使用者, 一般情況下是沒有許可權寫該檔案.

# log_level: 預設為 info

log_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mns_python_sdk.log')

logger = mnslogger.get_logger(log_file=log_file)

my_account = account("", "******", "******", logger=logger)

my_topic = my_account.get_topic("sms.topic-cn-beijing")

'''step 2. 設定sms訊息體(必須)

注:目前暫時不支援訊息內容為空,需要指定訊息內容,不為空即可。

'''msg_body2 = "sms-message2."

'''step 4. 生成sms訊息屬性,single=true表示每個接收者引數一樣

'''direct_sms_attr2 = directsmsinfo(free_sign_name="******", template_code="******", single=true)

direct_sms_attr2.add_receiver(receiver="*********xx")

direct_sms_attr2.add_receiver(receiver="yyyyyyyyyyy")

direct_sms_attr2.add_receiver(receiver="zzzzzzzzzzz")

'''#step 5. 生成sms訊息物件

'''msg2 = topicmessage(msg_body2, direct_sms = direct_sms_attr2)

try:

'''step 6. 發布sms訊息

'''re_msg = my_topic.publish_message(msg2)

print

"publish message succeed. messagebody:%s messageid:%s" % (msg_body2, re_msg.message_id)

except mn***ceptionbase,e:

if e.type == "topicnotexist":

print

"topic not exist, please create it."

sys.exit(1)

print

"publish message fail. exception:%s" % e

阿里雲訊息服務使用教程

課程介紹 阿里雲訊息服務 message service,原mqs 是阿里雲商用的訊息中介軟體服務。與傳統的訊息中介軟體不同,訊息服務一開始就是基於阿里雲自主研發的飛天分布式系統來設計和實現,具有大規模,高可靠 高併發訪問和超強訊息堆積能力的特點。訊息服務api採用http restful標準,接入...

阿里雲訊息服務使用教程

課程介紹 阿里雲訊息服務 message service,原mqs 是阿里雲商用的訊息中介軟體服務。與傳統的訊息中介軟體不同,訊息服務一開始就是基於阿里雲自主研發的飛天分布式系統來設計和實現,具有大規模,高可靠 高併發訪問和超強訊息堆積能力的特點。訊息服務api採用http restful標準,接入...

阿里雲訊息推送服務

移動推送 提供從雲端到移動終端的優質推送服務,支援android和ios平台的通知 訊息的推送功能 推送內容及模式 推送方式 按裝置號推送,在客戶端呼叫getdeviceld獲得裝置號,再通過控制台 openapi介面推送 名詞解釋 accesskeyld和accesskeysecret 阿里雲使用...