3
votes

I have been batteling to send a direct message from a c# console applicaton to my skype bot that is hosted in Azure, I keep on getting the error: Operation returned an invalid status code 'Unauthorized', but I have provided the following credentials:

Web.Config File

  <appSettings>
    <add key="BotId" value="myBotId" />
    <add key="MicrosoftAppId" value="AppId" />
    <add key="MicrosoftAppPassword" value="Password" />
  </appSettings>

I made sure of the above! So can't be that.

My console application code:

var userAccount = new ChannelAccount(name: "User", id: "default-user");
var botAccount = new ChannelAccount(name: "Bot", id: "id");
var connector = new ConnectorClient(new Uri("https://f2d92691.ngrok.io"));

IMessageActivity message = Activity.CreateMessageActivity();

if (!string.IsNullOrEmpty(conversationId) && !string.IsNullOrEmpty(channelId))
{
    message.ChannelId = channelId;
}
else
{
    conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
}

message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId);
message.Text = messageText;
message.Locale = "en-Us";

await connector.Conversations.SendToConversationAsync((Activity)message);

This is the code that I used to work with the bot emulator.

Has anyone done this before and please note that I can send message using this example: https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-proactive-messages

But this is code that is being used in the bot itself, I wan't to use it outside of the bot.

Thanks

1
did the user chat with the bot at least once before sending this message ? - Anita George
Yes, I have the ChannelAccount id and name for the recipient and From address.. - Arno van Zyl
Found the solution, just add this line if you are using code to send message outside the bot.MicrosoftAppCredentials.TrustServiceUrl(URL); - Arno van Zyl

1 Answers

3
votes

Found the solution, just add this line if you are using code to send message outside the bot. MicrosoftAppCredentials.TrustServiceUrl(URL);