| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #!/usr/bin/env python
- #coding=utf-8
- import json
- from aliyunsdkcore.client import AcsClient
- from aliyunsdkcore.request import CommonRequest
- from tencentcloud.common import credential
- from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
- from tencentcloud.sms.v20210111 import sms_client, models
- from tencentcloud.common.profile.client_profile import ClientProfile
- from tencentcloud.common.profile.http_profile import HttpProfile
- ############aliyun#######################
- client = AcsClient('LTAI5t8bioQxGXB1jtVugJcU', 'OdGWSBRjkJxaPjgmE38eQ8nzkI6nRk', 'cn-hangzhou')
- def send_verify_code(phone,code):
- """发送手机验证码
- """
- request = CommonRequest()
- request.set_accept_format('json')
- request.set_domain('dysmsapi.aliyuncs.com')
- request.set_method('POST')
- request.set_protocol_type('https') # https | http
- request.set_version('2017-05-25')
- request.set_action_name('SendSms')
- request.add_query_param('RegionId', "cn-hangzhou")
- request.add_query_param('PhoneNumbers', phone)
- request.add_query_param('SignName', "顽主杯")
- #request.add_query_param('TemplateCode', "SMS_243482769")
- request.add_query_param('TemplateCode', "SMS_243487831")
- request.add_query_param('TemplateParam', json.dumps({"code":code}))
- response = client.do_action(request)
- print(response)
- return True,u"success"
- def send_signup_success(phone,name):
- """报名成功
- """
- request = CommonRequest()
- request.set_accept_format('json')
- request.set_domain('dysmsapi.aliyuncs.com')
- request.set_method('POST')
- request.set_protocol_type('https') # https | http
- request.set_version('2017-05-25')
- request.set_action_name('SendSms')
- request.add_query_param('RegionId', "cn-hangzhou")
- request.add_query_param('PhoneNumbers', phone)
- request.add_query_param('SignName', "顽主杯")
- request.add_query_param('TemplateCode', "SMS_243950634")
- request.add_query_param('TemplateParam', json.dumps({"name":name}))
- response = client.do_action(request)
- print(response)
- return True,u"success"
- #################tencent######################
- SECRET_ID = "AKIDmOnEZjMm05xg4JNErW2KOI2FIC5nIl6H"
- SECRET_KEY = "5yhHpEQREsKZeo04xe7NQJ0UkstiJhsp"
- APPID = "1400799769"
- def get_tencent_clinet():
- # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey。
- # 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
- # 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
- # SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi
- # cred = credential.Credential("secretId", "secretKey")
- cred = credential.Credential(SECRET_ID,SECRET_KEY)
- # 实例化一个http选项,可选的,没有特殊需求可以跳过。
- httpProfile = HttpProfile()
- # 如果需要指定proxy访问接口,可以按照如下方式初始化hp(无需要直接忽略)
- # httpProfile = HttpProfile(proxy="http://用户名:密码@代理IP:代理端口")
- httpProfile.reqMethod = "POST" # post请求(默认为post请求)
- httpProfile.reqTimeout = 10 # 请求超时时间,单位为秒(默认60秒)
- httpProfile.endpoint = "sms.tencentcloudapi.com" # 指定接入地域域名(默认就近接入)
- # 非必要步骤:
- # 实例化一个客户端配置对象,可以指定超时时间等配置
- clientProfile = ClientProfile()
- clientProfile.signMethod = "TC3-HMAC-SHA256" # 指定签名算法
- clientProfile.language = "en-US"
- clientProfile.httpProfile = httpProfile
- client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
- return client
- def send_verify_code(phone,code):
- """发送手机验证码
- """
- templateId = "2394364"
- client = get_tencent_clinet()
- req = models.SendSmsRequest()
- req.SmsSdkAppId = APPID
- # 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名
- # 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
- req.SignName = "顽主杯"
- # 模板 ID: 必须填写已审核通过的模板 ID
- # 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
- req.TemplateId = templateId
- # 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,,若无模板参数,则设置为空
- req.TemplateParamSet = [code]
- # 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
- # 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号
- req.PhoneNumberSet = [phone]
- # 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回
- req.SessionContext = ""
- # 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手]
- req.ExtendCode = ""
- # 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。
- req.SenderId = ""
- resp = client.SendSms(req)
- # 输出json格式的字符串回包
- print(resp.to_json_string(indent=2))
- return True,u"success"
- def send_signup_success(phone,name):
- """报名成功
- """
- templateId = "2048076"
- client = get_tencent_clinet()
- req = models.SendSmsRequest()
- req.SmsSdkAppId = APPID
- # 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名
- # 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
- req.SignName = "顽主杯"
- # 模板 ID: 必须填写已审核通过的模板 ID
- # 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
- req.TemplateId = TemplateId
- # 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,,若无模板参数,则设置为空
- req.TemplateParamSet = [name]
- # 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
- # 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号
- req.PhoneNumberSet = [phone]
- # 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回
- req.SessionContext = ""
- # 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手]
- req.ExtendCode = ""
- # 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。
- req.SenderId = ""
- resp = client.SendSms(req)
- # 输出json格式的字符串回包
- print(resp.to_json_string(indent=2))
- return True,u"success"
- if __name__ == "__main__":
- phone = "15982456282"
- #phone = "13883187629"
- code = "1234"
- send_verify_code(phone,code)
- #send_signup_success(phone,u"肖")
|