目标:我试图处理来自Trello webhooks的帖子请求,然后发送一个包含不和谐工会相关数据的嵌入
目前的进展:我已经处理了POST请求,我知道如何发送嵌入等。然而,当我最终需要同时实现这两个时,我意识到仅仅py trelloHandler.py
是不可能的,它们都开始运行了。我做了一些研究,发现了someone asking a similar question。对大多数人来说,这将是有帮助的,尽管我对python非常陌生(我喜欢学习项目),也不确定如何实现线程。我确实找到了一个guide over on realpython.com,但我没能理解它
我的问题(TL;DR):如何运行一个HTTP服务器,该服务器将在discord.py bot(更具体地说,使用线程)的相同程序中侦听post请求
我的代码(“bot_token_here”替换为我的discord token):
import discord
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
from discord.ext import commands
client = commands.Bot(command_prefix = "~")
class requestHandler(BaseHTTPRequestHandler):
def do_POST(self):
# Interpret and process the data
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
data = json.loads(post_body)
# Action and Models data
action = data['action']
actionData = action['data']
model = data['model']
# Board and card data
board = action['data']['board']
card = action['data']['card']
# Member data
member = action['memberCreator']
username = member['username']
# Keep at end of do_POST
self.send_response(204)
self.send_header('content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self.send_response(200)
self.end_headers()
@client.event
async def on_ready():
print("Bot is online, and ready to go! (Listening to {} servers!)".format(len(list(client.guilds))))
def main():
PORT = 9090
server_address = ('localhost', PORT)
server = HTTPServer(server_address, requestHandler)
server.serve_forever()
client.run("bot_token_here")
if __name__ == '__main__':
main()
目前没有回答
相关问题 更多 >
编程相关推荐