BotError : Failed to send activity: bot returned an error. httpStatusCode: 500
Bot Info
SDK Platform: .NET SDK Version: 3.12.2.4 Emulator Version: 3.5.35 Active Channels: Directline v3
Issue Description
calls made to https://directline.botframework.com/v3/directline/conversations/conversationId/activities is providing error code 502(Bad gateway)

Code Example
BotChat.App({
botConnection: botapp.botConnection,
user: botapp.user,
bot: bot,
resize: 'detect'
}, document.getElementById("divbot"));
Reproduction Steps
trying to test the service from rest client (postman)
- Generating Token from "https://directline.botframework.com/v3/directline/tokens/generate"
- Passing the generated token in "https://directline.botframework.com/v3/directline/conversations"
- Passing the generated conversationId from step2 in "https://directline.botframework.com/v3/directline/conversations/conversationId/activities" The output result is

Expected Behavior
It should give 200 ok
Actual Results

community need your expert advice on this. In real tight schedule to deliver this chunk. Need your support.
@pankajdmishra Please post the code example, not images of the code.
I am facing similar error. Just hosted the sample bot framework code on azure and worked on direct line api. Doesn't work.
@shahsaurabh0605 What exactly "doesn't work"? Please be more specific, and explain the errors or issues you've run into so we can assist you.
Issue is clearly mentioned by @pankajdmishra . I follow the 3 steps:
- Generating Token from "https://directline.botframework.com/v3/directline/tokens/generate"
- Passing the generated token in "https://directline.botframework.com/v3/directline/conversations"
- Passing the generated conversationId from step2 in "https://directline.botframework.com/v3/directline/conversations/conversationId/activities"
And get the similar error on postman:

- Generate a Token from https://directline.botframework.com/v3/directline/tokens/generate

- Passing the generated token in https://directline.botframework.com/v3/directline/conversations

- Passing the generated conversationId from step2 in https://directline.botframework.com/v3/directline/conversations/conversationId/activities

I've seen the BotError before, but only if the bot is not actually running at the Messaging Endpoint:

I have hosted the bot on azure, say with the name "x-bot". Then is "https://x-bot.azurewebsites.net/api/messages" the correct messaging endpoint setup?
@shahsaurabh0605 Yes, that would most likely be the correct endpoint
Also if you'll see on portal upon checking the channels for direct line its showing errors PFB

@EricDahlvang need your expert advice here....
@pankajdmishra I'm sorry, but I'm not really understanding what you are attempting to do. If you are experiencing errors in your bots code, you can try debugging the bot locally: https://blog.botframework.com/2017/10/19/debug-channel-locally-using-ngrok/
Also, please try starting with one of the simple samples: https://github.com/Microsoft/BotBuilder-Samples Once you have a working example, you can build on it.
Hi @shahsaurabh0605 Were you able to resolve this in any case?
@pankajdmishra yes. Actual problem was not setting the correct endpoint. I hosted the bot on localhost and with ngrok got the correct endpoint. Refer here:- https://blogs.msdn.microsoft.com/jamiedalton/2016/07/29/ms-bot-framework-ngrok/
Thanks a lot @EricDahlvang
@pankajdmishra The code you've provided in HandleUserMessage should function, if your bot is configured properly. I just tested it, and it worked.
As I had suggested previously, please try debugging locally: https://blog.botframework.com/2017/10/19/debug-channel-locally-using-ngrok/
Also, please try starting with one of the simple samples: https://github.com/Microsoft/BotBuilder-Samples. Once you have a working example, you can build on it.
Hi @EricDahlvang
Thanks alot. I tried debugging my code using ngrok. Steps i performed using ngrok are as below
- I executed ngrok cmd
- runned following command (ngrok http 3979 -host-header="localhost:3979")
- got a forwarding uri. I changed my endpoint on azure portal with this forwarding URI pfb.

- I runned my code locally in debug mode.
- Using webchat control from, azure portal i posted few queries and it worked fine. I got expected response when i used ngrok for debugging.
there was no failure. Now the question is, was there a issue with my endpoint? below screen shots shows configuration of my bot.
This is the service URL for my bot app.
this is my BotId
Endpoint that i previously had before ngrok was https://sofeachatbot-stage.azurewebsites.net/api/messages
was this endpoint correct? If not than how to rectify which is the correct endpoint to configure?
Thanks in advance.
Hi,
I resolved this issue. Very stupid mistake done by me. While creating the bot i changed Password from WebAppBot --> Settings, "manage" PFB
And didn't updated the App service of the bot with the new password. In App service of your bot App Service --> Application settings change the password as well. PFB
Else this will always through 502 error.
Thanks alot @EricDahlvang and @shahsaurabh0605 for all your support.
Appreciated 👍
Closing this issue
@tinuCoder @EricDahlvang Even after updating the MicrosoftAppPassword in Application Settings of App service, I am still having this issue.
In postman,
{
"error": {
"code": "BotError",
"message": "Failed to send activity: bot returned an error"
},
"httpStatusCode": 417
}
And, the Status in postman is : 502 Bad Gateway
1. Generate a Token from https://directline.botframework.com/v3/directline/tokens/generate
1. Passing the generated token in https://directline.botframework.com/v3/directline/conversations
1. Passing the generated conversationId from step2 in https://directline.botframework.com/v3/directline/conversations/conversationId/activities
Hello @EricDahlvang i am following the exact steps that you pointed out.
- running locally developed with v4 sdk
- Bot connector service configured with the ngrok fwrd url
- have a directline channel configured
- successfully getting the token and conversationid, with the exception that conversation id is always appended with a -c. example :
{
"conversationId": "JQfgEvc7if86Qt1CwuV414-c",
"token": "token that was returned",
"expires_in": 1800,
"streamUrl": "wss://directline.botframework.com/v3/directline/conversations/JQfgEvc7if86Qt1CwuV414-c/stream?watermark=-&t=GaM9ExIOdJk.dAA.SgBRAGYAZwBFAHYAYwA3AGkAZgA4ADYAUQB0ADEAQwB3AHUAVgA0ADEANAAtAGMA.eIj5E3vp1AE.GEB8PJfGae4.5rT75D232c9Ht5wDOpuNT5LlEUmvhuLXkqnxUnLRCsU",
"referenceGrammarId": "88a91a98-2ddc-8a5d-ab4d-f76f2b5c876c"
}
- When i execute following https://directline.botframework.com/v3/directline/conversations/JQfgEvc7if86Qt1CwuV414-c/activities i always get internal server error
{
"error": {
"code": "ServiceError"
}
}
Following is my request object
{
"type": "message",
"from": {
"id": "user1"
},
"text": "hello"
}
Please help me identify the issue.
Thanks, Prerak
For anyone else facing the same issue, here is the advice. The issue is, only the token which was received against 'start conversation' can be used to 'send an activity'. refer this SO answer and read Q3
Thanks.


