我正在设置一个Facebook Messenger机器人,该机器人可以在我的笔记本电脑上远程执行代码(即截屏,显示消息)。当我从Messenger发出“ msg hello”消息时,机器人会使用Tkinter显示带有“ hello”的消息框。
程序返回错误:
RuntimeError: main thread is not in main loop
我尝试使用以下命令运行Tkinter:
root = tkinter.Tk()
root.mainloop()
但是程序只是冻结在那里(就像while True
循环一样
这是我的主烧瓶应用程序的代码:
from flask import Flask, request
from pymessenger.bot import Bot
from functions.message_box import msg_box
app = Flask(__name__)
ACCESS_TOKEN = 'my token' # I removed these tokens on purpose
VERIFY_TOKEN = 'my token'
bot = Bot(ACCESS_TOKEN)
@app.route("/", methods=['GET', 'POST'])
def receive_message():
if request.method == 'GET':
# Assuming the request is from facebook's verfication
token_sent = request.args.get("hub.verify_token")
return verify_fb_token(token_sent)
else:
# get whatever message a user sent the bot
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
#Facebook Messenger ID for user so we know where to send response back to
recipient_id = message['sender']['id']
# get code from the message
text = message['message'].get('text')
code = text[:text.find(' ')] # find the first word in the message
if code == 'msg':
msg = text[text.find(' ') + 1:] # get the message except the code
msg_box(msg)
send_message(recipient_id, f'Sent: {msg}')
return "Message Processed"
def verify_fb_token(token_sent):
if token_sent == VERIFY_TOKEN:
return request.args.get("hub.challenge")
return 'Invalid verification token'
#uses PyMessenger to send response to user
def send_message(recipient_id, response):
#sends user the text message provided via input response parameter
bot.send_text_message(recipient_id, response)
return "success"
if __name__ == "__main__":
app.run()
这是来自message_box.py的代码(用于在窗口上显示消息框):
'''
Purpose: Display msg box on screen
'''
import tkinter
from tkinter import messagebox
# hide main window
root = tkinter.Tk()
# root.mainloop()
root.withdraw()
def msg_box(msg):
messagebox.showinfo("Information", msg)
return 1
我收到的完整错误:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2019-09-05 15:52:47,663] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "app.py", line 59, in receive_message
msg_box(msg)
File "C:\Users\kevin\Desktop\python\laptopRemoteController\functions\message_box.py", line 13, in msg_box
messagebox.showinfo("Information", msg)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\messagebox.py", line 83, in showinfo
return _show(title, message, INFO, OK, **options)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\messagebox.py", line 72, in _show
res = Message(**options).show()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\commondialog.py", line 39, in show
w = Frame(self.master)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2744, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
(widgetName, self._w) + extra + self._options(cnf))
RuntimeError: main thread is not in main loop
127.0.0.1 - - [05/Sep/2019 15:52:47] "POST / HTTP/1.1" 500 -
[2019-09-05 15:52:49,220] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
如果有错误,请帮助我纠正它们。此外,如果还有其他方法,请教我如何操作。
感谢您的时间和耐心。