我正在制作一个程序,它从服务器上擦除总线信息,并通过Facebook Messenger发送给用户。它工作正常,但我正在尝试添加功能,将很长的时间表分成单独的消息。要做到这一点,我必须制作一个if语句来检测真正的长时间表,拆分它们并从我的主文件中调用send_message
函数,app.py
以下是app
中函数的一部分,其中包含我需要提取的变量:
for messaging_event in entry["messaging"]:
if messaging_event.get("message"): # someone sent us a message
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID
message_text = messaging_event["message"]["text"] # the message's text
tobesent = messaging_event["message"]["text"]
send_message(sender_id, fetch.fetchtime(tobesent))
这里是fetch
中的if语句,它检测长消息,拆分它们并从另一个文件send_message
调用app.py
函数:
if len(info["results"]) > 5:
for i, chunk in enumerate(chunks(info, 5), 1):
app.send_message((USER ID SHOULD BE HERE, 'Listing part: {}\n \n{}'.format(i, chunk)))
我正在尝试从send_message
调用app.py
函数,但它需要两个参数sender_id
和消息文本。如何从此函数中获取sender_id
变量并在fetch
中使用它?我已经尝试返回它并调用该函数,但它对我不起作用。
编辑:错误
Traceback (most recent call last):
File "bus.py", line 7, in <module>
print fetch.fetchtime(stopnum)
File "/home/ryan/fb-messenger-bot-master/fetch.py", line 17, in fetchtime
send_message((webhook(),'Listing part: {}\n \n{}'.format(i, chunk)))
File "/home/ryan/fb-messenger-bot-master/app.py", line 30, in webhook
data = request.get_json()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 343, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 302, in _get_current_object
return self.__local()
File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 37, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.