Python 通过Textlocal API发送模板消息

Python 通过Textlocal API发送模板消息,python,Python,我得到的答复是: {“错误”:[{“代码”:204,“消息”:“无效消息内容”}],“状态”:“失败”} 当我尝试使用此模板提交邮件时,我已向Textlocal注册: 感谢您在***注册。请输入验证码%%| OTP^{“inputtype”:“text”,“maxlength”:“6”}%%以完成注册 如何使用此模板发送邮件?我目前的代码是: def sending_sms(number,otp): url = 'http://api.textlocal.in/send/' ms

我得到的答复是:

{“错误”:[{“代码”:204,“消息”:“无效消息内容”}],“状态”:“失败”}

当我尝试使用此模板提交邮件时,我已向Textlocal注册:

感谢您在***注册。请输入验证码%%| OTP^{“inputtype”:“text”,“maxlength”:“6”}%%以完成注册

如何使用此模板发送邮件?我目前的代码是:

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将根据所有已批准的模板检查您的邮件,并传递 仅当消息与您帐户中的任何已批准模板完全匹配时,才发送消息

  • 从数据库/应用程序中获取动态参数并传递最终结果 API中的消息内容
    • 例如:如果您批准的模板是:
      感谢您注册Textlocal。您的验证代码是XXXX
      需要传递的消息内容是:
      感谢您注册Textlocal。您的验证码是1123(代码由您的应用程序生成)
  • 在代码中,必须为动态参数分配一个变量(理想情况下是一个长字符串或数组),该变量根据手机号码从数据库/应用程序中分配一个特定值。
    • 例如:在PHP中,
      $message=rawurlencode('感谢您注册Textlocal。您的验证码是$otp')
      其中,
      $otp
      是代码中动态参数的标识符
  • 注意事项:

  • 模板名称、占位符文本仅供参考。它们不应添加到API中
  • Textlocal API中的“Message”参数应仅包含完整的消息内容 已替换动态参数。(有关参数详细信息,请参阅)
  • 所有特殊字符都需要URL编码(参考: 用于编码值)
  • 如果模板具有换行符–请在API中将换行符提要替换为%n

  • 根据@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."