Python 通过Textlocal API发送模板消息
我得到的答复是: {“错误”:[{“代码”:204,“消息”:“无效消息内容”}],“状态”:“失败”} 当我尝试使用此模板提交邮件时,我已向Textlocal注册: 感谢您在***注册。请输入验证码%%| OTP^{“inputtype”:“text”,“maxlength”:“6”}%%以完成注册 如何使用此模板发送邮件?我目前的代码是:Python 通过Textlocal API发送模板消息,python,Python,我得到的答复是: {“错误”:[{“代码”:204,“消息”:“无效消息内容”}],“状态”:“失败”} 当我尝试使用此模板提交邮件时,我已向Textlocal注册: 感谢您在***注册。请输入验证码%%| OTP^{“inputtype”:“text”,“maxlength”:“6”}%%以完成注册 如何使用此模板发送邮件?我目前的代码是: def sending_sms(number,otp): url = 'http://api.textlocal.in/send/' ms
def sending_sms(number,otp):
url = 'http://api.textlocal.in/send/'
msg = 'Thank you for registering with RTM. Please enter the verification code %%|OTP^{"inputtype" : "text", "maxlength" : "6"}%% to complete the registration.'
post_fields = ({"username":"admin@example.in","password":"P@ssword","numbers":number,"message":msg})
request = Request(url, urlencode(post_fields).encode())
print request
json = urlopen(request).read().decode()
print json
return json
您正在将模板作为邮件发送。您需要发送一个已填写的模板(即替换实际的OTP代码,而不是
%%| OTP…
)
从文档中:
如何通过API发送模板?
Textlocal将根据所有已批准的模板检查您的邮件,并传递
仅当消息与您帐户中的任何已批准模板完全匹配时,才发送消息
- 例如:如果您批准的模板是:
需要传递的消息内容是:感谢您注册Textlocal。您的验证代码是XXXX
感谢您注册Textlocal。您的验证码是1123(代码由您的应用程序生成)
- 例如:在PHP中,
其中,$message=rawurlencode('感谢您注册Textlocal。您的验证码是$otp')
是代码中动态参数的标识符$otp
根据@Grisha Levit的回答,是的。您只需在消息变量本身中发送OTP,而不是%%|OTP^{“inputtype”:“text”,“maxlength”:“6”}%% 解决这个问题 改变这个
msg = 'Thank you for registering with RTM. Please enter the verification code %%|OTP^{"inputtype" : "text", "maxlength" : "6"}%% to complete the registration.'
对此
otp = 123456
msg = f"Thank you for registering with RTM. Please enter the verification code {otp} to complete the registration."