作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Microsoft Bot Framework 的新手,我正在通过 youtube 视频 https://youtu.be/ynG6Muox81o 学习它并在 Ubuntu 上使用 python3 制作我的机器人。
Microsoft Bot Emulator说“发送失败。重试”。
Visual Studio Code显示 KeyError:'HTTP_CONTEXT_TYPE'。
我做对了,我的机器人连接到 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
app = Flask(__name__)
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
else:
#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)
if __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)
我似乎无法理解这个问题以及如何解决它。如果有人可以帮助我解决这个问题,那将非常有帮助,因为我对构建机器人非常感兴趣。
最佳答案
这是一个简单的错字。你写的是“context-type”而不是“content-type”。你链接到的 YouTube 教程告诉你写这样一行:
if "application/json" in request.headers["content-type"]:
为了避免这种错别字,您可以直接下载视频链接的源代码,而不是尝试自己重写。
KeyError: 'HTTP_CONTEXT_TYPE'
这应该向您表明您输入了错误的 key ,并且您应该检查是否有错字。您还可以尝试使用调试器并单步执行您的代码。
关于python - Microsoft Bot Emulator 显示 "sending failed. Retry"。 VSCode 显示 KeyError : 'HTTP_CONTEXT_TYPE' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66116377/
我是 Microsoft Bot Framework 的新手,我正在通过 youtube 视频 https://youtu.be/ynG6Muox81o 学习它并在 Ubuntu 上使用 python
我是一名优秀的程序员,十分优秀!