【问题标题】:Conditional SMS response with Django/Twilio使用 Django/Twilio 的条件短信响应
【发布时间】:2013-08-01 06:31:59
【问题描述】:

我正在尝试根据不同的参数(呼叫者 ID、文本正文)调整 SMS 响应,错误是“HTTP 检索失败” 我试过为不同的调用者使用 Flask 教程:

def hello_monkey():
    """Respond and greet the caller by name."""

    from_number = request.values.get('From', None)
    if from_number in callers:
        message = callers[from_number] + ", thanks for the message!"
    else:
        message = "Monkey, thanks for the message!"

但我意识到 Django 可能会有所不同,所以我尝试了这些和其他几个:

def hello_monkey(request, From)
    #with    
    from = From

def hello_monkey(request):
    #with
    number = request.POST["Body"]

谢谢 编辑:忘记link

【问题讨论】:

  • Flask 教程有用吗?
  • 奇怪的是,我无法弄清楚烧瓶数据库的内容,所以我选择了 Django。我确信 Flask 可以工作。

标签: python django twilio


【解决方案1】:

实际上在 Django 中它会非常相似,您应该阅读更多关于 request 的信息。由于不知道请求是 POST 还是 GET,所以可以使用HttpRequest.REQUEST

callers = {
    "+14158675309": "Curious George",
    "+14158675310": "Boots",
    "+14158675311": "Virgil",
}

def hello_monkey(request):
    """Respond and greet the caller by name."""

    from_number = request.REQUEST.get('From', None)
    if from_number in callers:
        message = callers[from_number] + ", thanks for the message!"
    else:
        message = "Monkey, thanks for the message!"

    # .... your code ....

希望对你有帮助!

【讨论】:

  • 非常感谢!如果有人想知道,如果您将 'From' 替换为 'Body',此 REQUEST.get() 也可以使用
  • 不客气。基本上request.REQUEST 是一个字典,所以你可以正常使用get() docs.python.org/2/library/stdtypes.html#dict.get。如果您有任何其他问题,请联系我 ;)
  • 这取决于 Twilio 后端,因为在示例中视图应该同时接收 GET 和 POST,所以我不会打赌。使用 REQUEST 是您的安全选择。
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
相关资源
最近更新 更多