Odoo发送短信

转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10825951.html

 

一:阿里云短信服务注册

1:开通短信业务:实名认证的个人用户是免费开通;企业用户需要提供三证(营业执照、组织机构代码证、税务登记证)

 

2:创建用户,生成AccessKey,记得下载并保存起来。只有在创建的时候可以下载!

 

3:为用户授权

 

4:创建签名和短信模版,一般两小时内就可以审核通过

 

二:SDK下载

下载地址:https://help.aliyun.com/document_detail/55359.html

 

三:要用到的文件

1)api_sdk/aliyun-python-sdk-core/aliyunsdkcore

2)api_sdk/aliyun-python-sdk-dysmsapi/aliyunsdkdysmsapi

把aliyunsdkcore和aliyunsdkdysmsapi拷贝到项目中,然后在同级目录下,新建 aliyun_sms.py文件,在里面定义短信发送和短信查询的方法:

# -*- coding: utf-8 -*-
import sys
from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest
from aliyunsdkcore.client import AcsClient
import uuid
import json
"""
短信业务调用接口
"""

reload(sys)
sys.setdefaultencoding('utf8')

#不要更改
REGION = "cn-hangzhou"

# ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
ACCESS_KEY_ID = "你创建的RAM用户的ACCESS_KEY_ID"
ACCESS_KEY_SECRET = "你创建的RAM用户的ACCESS_KEY_SECRET"

acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)


class AliSMS(object):
    #发送短信
    def send_sms(self, business_id, phone_numbers, sign_name, template_code, template_param=None):
        smsRequest = SendSmsRequest.SendSmsRequest()
        #1:设置短信模板编码
        smsRequest.set_TemplateCode(template_code)

        #2:传递短信模板变量参数 {"code":"值"}
        if template_param is not None:
            smsRequest.set_TemplateParam(template_param)

        #3:设置业务请求流水号,必填。
        smsRequest.set_OutId(business_id)

        #4:设置短信签名
        smsRequest.set_SignName(sign_name);

        #5:设置短信发送的号码列表(一个就是单发,数组就是群发)
        smsRequest.set_PhoneNumbers(phone_numbers)

        #6:调用短信发送接口,获得返回json响应数据
        smsResponse = json.loads(acs_client.do_action_with_exception(smsRequest))

        return smsResponse

    #查询短信
    def query_send_detail(self, biz_id, phone_number, page_size, current_page, send_date):
        queryRequest = QuerySendDetailsRequest.QuerySendDetailsRequest()

        # 查询的手机号码
        queryRequest.set_PhoneNumber(phone_number)
        # 流水号
        # queryRequest.set_BizId(biz_id)
        # 发送日期 支持30天内记录查询,格式yyyyMMdd
        queryRequest.set_SendDate(send_date)
        # 必填-当前页码从1开始计数
        queryRequest.set_CurrentPage(current_page)
        # 必填-页大小
        queryRequest.set_PageSize(page_size)

        # 调用短信记录查询接口,返回json
        queryResponse = json.loads(acs_client.do_action_with_exception(queryRequest))

        return queryResponse

 

四:调用示例

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

from Ali_sms import *

class TestSMS(object):
    ali_sms = AliSMS()

    if __name__ == '__main__':
__business_id = uuid.uuid1()
params = "{\"code\":\"2222\"}"
response = ali_sms.send_sms(__business_id, "电话", "签名Name", "模版ID", params)
BizId = response["BizId"]
detail = ali_sms.query_send_detail(BizId, "电话", 10, 1,datetime.datetime.now().strftime('%Y%m%d'))

 

五:短信发送错误码和回执状态码

错误码解读:https://help.aliyun.com/knowledge_detail/57717.html

状态码:https://help.aliyun.com/document_detail/55323.html

posted @ 2019-05-07 15:52  keenyeh  阅读(1347)  评论(0编辑  收藏  举报