Python SDK
1. 准备工作
1) 获取API密钥信息
在调用API之前,您需要获取用于生成X-Signature
签名的关键信息。您需要提供AccessKeyId
和AccessKeySecret
,这些信息可以从控制台账户中获取。有关如何获取这些信息的详细步骤,请参考:如何获取AccessKeyId和AccessKeySecret。
2) 申请短信模板
2. SDK
1) 使用pip安装。
pip install uspeedo-sdk-python
3. 示例
请查看github链接了解详情: uspeedo-sdk-python
import logging
import os
from uspeedo.client import Client
from uspeedo.core.exc import USpeedoException
logger = logging.getLogger("uspeedo")
logger.setLevel(logging.DEBUG)
def get_asms_client():
client = Client(
{
"public_key": os.getenv("USPEEDO_PUBLIC_KEY"),
"private_key": os.getenv("USPEEDO_PRIVATE_KEY"),
"base_url": "https://api.uspeedo.com/api/"
}
)
return client
def invoke_send_message():
client = get_asms_client()
req = {
"TaskContent": [{
"TemplateId": "UTAXXXXXXXXXXX",
"SenderId": "uspeedo",
"Target": [{
"Phone": "(1)11111111",
"TemplateParams": ["1111"]
}]
}]
}
try:
response_json = client.asms().send_batch_usms_message(req)
print(response_json)
except USpeedoException as e:
print(e)
def invoke_template_api():
client = get_asms_client()
try:
create_template_req = {
"Template": "Your verification code is {1}",
"Purpose": 1,
"Remark": "this is a test template",
"TemplateName": "test template"
}
response_json = client.asms().create_usms_template(create_template_req)
print(response_json)
except USpeedoException as e:
print(e)
try:
query_template_req = {
"AccountId": "60000011",
"TemplateId": ["UTA230227EL4IW1", "UTA230227JVIB02"],
}
response_json = client.asms().query_usms_template(query_template_req)
print(response_json)
except USpeedoException as e:
print(e)
try:
update_template_req = {
"TemplateId": "UTAXXXXXXXXXXX",
"Template": "Your verification code is {1},thanks"
}
response_json = client.asms().update_usms_template(update_template_req)
print(response_json)
except USpeedoException as e:
print(e)
try:
delete_template_req = {
"TemplateIds": ["UTAXXXXXXXXXXX"],
}
response_json = client.asms().delete_usms_template(delete_template_req)
print(response_json)
except USpeedoException as e:
print(e)
def main():
# invoke_send_message()
invoke_template_api()
if __name__ == '__main__':
main()