sách gpt4 ai đã đi

python - Microsoft Bot Emulator 显示 "sending failed. Retry"。 VSCode 显示 KeyError : 'HTTP_CONTEXT_TYPE'

In lại 作者:行者123 更新时间:2023-12-04 18:33:13 30 4
mua khóa gpt4 Nike

我是 Microsoft Bot Framework 的新手,我正在通过 youtube 视频 https://youtu.be/ynG6Muox81o 学习它并在 Ubuntu 上使用 python3 制作我的机器人。
Microsoft Bot Emulator说“发送失败。重试”。
Microsoft Bot Emulator
Visual Studio Code显示 KeyError:'HTTP_CONTEXT_TYPE'。
Visual Studio Code
我做对了,我的机器人连接到 http://localhost:3978/,而机器人模拟器连接到 http://localhost:3978/api/messages/。
由于windows防火墙,stackoverflow上的许多人都遇到了同样的问题,但我的是ubuntu,我确实检查了它是否已启用,但没有。
应用程序.py



from flask import Flask, request, Response
from botbuilder.schema import Activity
from botbuilder.core import BotFrameworkAdapter,BotFrameworkAdapterSettings

import asyncio

from echobot import EchoBot

ứng dụng = Flask(__tên__)
loop = asyncio.get_event_loop()

botadaptersettings=BotFrameworkAdapterSettings("","")
botadapter = BotFrameworkAdapter(botadaptersettings)

ebot = EchoBot()

#POST is the message
@app.route("/api/messages",methods=["POST"])
def messages():
#checking if HTTP file format is JSON or not
if "application/json" in request.headers["context-type"]:
#reading the JSON message
jsonmessage = request.json
khác:
#unsupported media type 415
return Response(status=415)

activity = Activity().deserialize(jsonmessage)

async def turn_call(turn_context):
await ebot.on_turn(turn_context)

task = loop.create_task(botadapter.process_activity(activity,"",turn_call))
loop.run_until_complete(task)


nếu __name__ == '__main__':
app.run('localhost',3978)
回声机器人.py


from botbuilder.core import TurnContext

class EchoBot:
async def on_turn(self,turn_context:TurnContext):
await turn_context.send_activity(turn_context.activity.text)

我似乎无法理解这个问题以及如何解决它。如果有人可以帮助我解决这个问题,那将非常有帮助,因为我对构建机器人非常感兴趣。
我已经在 gitlab https://gitlab.com/pdmnbhrawal/myechobot01 上上传了我的 Bot 项目.

1 Câu trả lời

这是一个简单的错字。你写的是“context-type”而不是“content-type”。你链接到的 YouTube 教程告诉你写这样一行:

        if "application/json" in request.headers["content-type"]:
为了避免这种错别字,您可以直接下载视频链接的源代码,而不是尝试自己重写。
您可以自己解决此类错误,方法是在堆栈跟踪中注意到它告诉您是哪一行代码引发了错误。当它说 KeyError: 'HTTP_CONTEXT_TYPE'这应该向您表明您输入了错误的 key ,并且您应该检查是否有错字。您还可以尝试使用调试器并单步执行您的代码。
您可能会注意到该机器人还有一些其他问题。它不能正确处理对话更新事件,因为它没有考虑到没有文本的传入事件。它也不会从 api/messages 端点返回 HTTP 响应。您可能希望稍后解决这些问题,但尽管存在这些问题,您的机器人仍会运行。

关于python - Microsoft Bot Emulator 显示 "sending failed. Retry"。 VSCode 显示 KeyError : 'HTTP_CONTEXT_TYPE' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66116377/

30 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com