Search code examples
pythonubuntuflaskbotframeworkfirewall

Microsoft Bot Emulator showing "sending failed. Retry". VSCode shows KeyError: 'HTTP_CONTEXT_TYPE'


I am new to Microsoft Bot Framework, I am learning it through a youtube video https://youtu.be/ynG6Muox81o and making my bot on python3 on Ubuntu.

The Microsoft Bot Emulator says "sending failed. Retry".

Microsoft Bot Emulator

Visual Studio Code shows KeyError: 'HTTP_CONTEXT_TYPE'.

Visual Studio Code

I did everything right, my bot is connected to http://localhost:3978/, and bot emulator is connected to http://localhost:3978/api/messages/. Many people on stackoverflow who had the same issue faced it due to windows firewall, but mine is ubuntu and i did check if it was enabled but its not.

app.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)

echobot.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)

I cant seem to understand the issue and how to fix it. it would be really helpfull if someone could help me fix this as i am really interested in building bots.

I have uploaded my Bot Project on gitlab https://gitlab.com/pdmnbhrawal/myechobot01.


Solution

  • This is a simple typo. You've written "context-type" instead of "content-type." The YouTube tutorial you linked to tells you to write a line like this:

            if "application/json" in request.headers["content-type"]:
    

    In order to avoid this kind of typo, you can download the source code that the video links to directly instead of trying to rewrite it yourself.

    You can troubleshoot this kind of error yourself by noticing in the stack trace that it tells you what line of code is throwing the error. When it says KeyError: 'HTTP_CONTEXT_TYPE' that should indicate to you that you've entered the wrong key and you should check for a typo. You can also try using a debugger and stepping through your code.

    There are a few other problems with the bot that you may notice. It doesn't handle conversation update activities correctly because it doesn't account for the incoming activity having no text. It also doesn't return an HTTP response from the api/messages endpoint. You might want to fix those problems later, but your bot will run despite them.