如何将TwilioPython程序连接到本地服务器?

2024-06-16 11:17:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我看到了这个惊人的hackathon项目,我想测试一下。你知道吗

以下是供您参考的链接:https://github.com/ionambrinoc/oxhack

我知道如何运行android应用程序。只要在androideclipse上运行它,它就如我所期望的那样工作了。它将消息发送到twilio后端。我从github得到了这个项目,我想尝试一下,但是他们的twilio id过期了。所以我有了一个新的twilio账号,我试着用我的账号替换他们的账号。我假设应用程序SID和应用程序密钥是一样的。(是吗?)你知道吗

我试图在终端上用python hello.py运行我的代码,但是我得到了这个巨大的错误。你知道吗

TypeError TypeError: expected string or buffer ... The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error... You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: dump() shows all variables in the frame dump(obj) dumps all that's known about the object

我也有一些关于我的代码的问题。我把问题放在python代码的注释中。问题是大写的

from flask import Flask, session, redirect, url_for, escape, request
from twilio.rest import TwilioRestClient
import twilio.twiml
import requests
import json
import httplib

account_sid = "SOME_ACCOUNT_SID"
auth_token  = "SOME_AUTH_TOKEN"
client = TwilioRestClient(account_sid, auth_token)
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST']) #WHAT DOES THIS MEAN?
def index():
    phonenumber = request.values.get('From', None)
    body = request.values.get('Body', None)
    print(body)
    action = json.loads(body)
    current = action['current']
    desired = action['desired']
    lang = action['lang']
    payload={'current':current,'desired':desired,'lang':lang}
    url='http://school/index.php' #The dev used this link. WHAT DOES IT MEAN?
    r = requests.post(url,data=payload)
    print(payload)
    print(r.text)
    response=r.text
    message = client.messages.create(body=response,
                            to=phonenumber,
                            from_="+1408MYTWILIO")
    return message.sid
# IS THE APP SID SAME AS APP SECRET KEY?
app.secret_key = 'MY_APP_SECRET_KEY'
if __name__ == '__main__':
    app.run(debug=True)

如果您能回答这些问题中的任何一个,我们将非常感谢您的帮助。因为我还在学习,我真的很想看看这个hackathon项目是如何运作的。你知道吗


Tags: the项目代码inimportapp应用程序lang
2条回答

Twilio开发者福音传道者。你知道吗

我可以解释那里发生的一些事情,但恐怕不是全部。你知道吗

首先:

@app.route('/', methods=['GET', 'POST']) #WHAT DOES THIS MEAN?

这是python中的一个decorator函数。在这个应用程序的上下文中,它应用于下面的方法def index():。这意味着当Flask应用程序接收到对'/'(根路径)的GETPOST请求时,它将执行index方法。你知道吗

# IS THE APP SID SAME AS APP SECRET KEY?
app.secret_key = 'MY_APP_SECRET_KEY'

应用程序的密钥不是从Twilio获得的任何凭据。密钥应该是一个长的随机字符串。它与Flask中的会话一起使用,密钥用于对会话进行加密签名。在Flask documentation on settings中有更多关于这方面的信息。你知道吗

但是,以下行:

payload={'current':current,'desired':desired,'lang':lang}
url='http://school/index.php' #The dev used this link. WHAT DOES IT MEAN?
r = requests.post(url,data=payload)

包括奇怪的http://school/index.php,我真的无法解释。我只知道黑客用Esri API在两点之间寻找方向。看起来这一部分是在试图做到这一点。我不知道这个团队的网址后面是什么,看起来好像是某种代理。为了再次运行这个程序,您可能需要查看Esri API documentation并找出如何获取两地之间路由的文本描述。如果您需要这方面的帮助,我建议您向James Milner,Esri开发人员福音传道者大喊一声。你知道吗

希望这有帮助!你知道吗

似乎它们正在传递原始位置(current=action['current'])、目标目的地(desired=action['desired'])和语言(lang=action['lang'])。你知道吗

这几乎肯定会被传递给中的ArcGIS routing REST service索引.php. 基本上,它是和X,Y对以及WGS84坐标中的另一个X,Y对作为输入。WGS84坐标只是一种表达传统GPS经纬度的奇特方式。但是要特别注意这个顺序,经度(X)然后纬度(Y),因为这会让很多人感到困惑。你知道吗

如果你想的话,在不调用任何外部脚本(例如。索引.php). 如果使用的是请求,则端点URL为:

http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve?你知道吗

如果需要以下参数作为请求参数:

  • directionsLanguage=语言
  • returnDirections=真
  • 停止=x1,y1,x2,y2
  • token=在此处插入您的\u ARCGIS \u标记\u
  • f=json

从当前(x1,y1)和所需(x2,y2)得到的停止坐标。令牌将来自您的ArcGIS Developer account。f=json只是将返回格式设置为json而不是HTML。你知道吗

您的代码最终可能会如下所示:

payload= {'stops':str(current[0],current[1],desired[0],desired[1]),
          'directionsLanguage':lang, 'returnDirections': "true", 
          'f':'json','token': VAR_WITH_ARCGIS_TOKEN } 
url= 'http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve?' 

你需要把你的答案和你的答案连接起来。您可以通过从响应体获取方向数组,然后遍历features数组,连接attributes对象的文本属性来访问这些属性。你知道吗

希望这能澄清。如果有问题,请留言

相关问题 更多 >