I am having difficulty with a chatbot that I developed which works fine locally but after it was deployed to dev.botframework.com it does not appear to work.
My code is below and it breaks at the line...
await Conversation.SendAsync(activity, () => new MyBot.AppServices.ServiceLUIS()); where it states
{"Authorization for Microsoft App ID a8641a16-932c-49a5-af8b-a58ab2ce251f failed with status code Unauthorized and reason phrase 'Unauthorized'"}.
I have tried the instructions at Troubleshooting Bot Framework Authentication with the following results:
Step 1: Connect without password on localhost - Worked fine!
Step 2: Verify AppID and Password are Correct - They are!
Step 3: Enable security and run on localhost - This does not work :( However the endpoint is correct as are the MicrosoftAppID and Password
Step 4: Connect to your bot using the Bot Framework Developer Portal - This also works! However when using the web chat feature within the portal I also get an internal server error
Where it is breaking is on a call to my LUIS service so to me it seems like my Bot doesn't have authorization to call my LUIS model. However I can't seem to find anything in Azure where my bot is published and LUIS Model resides where I would allow LUIS model to authorize access to my BOT. Also don't see anything in Luis.ai or Bot Framework Portal.
Any ideas on how best to resolve would really be helpful!
namespace MyBot
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
try
{
if (activity.Type == ActivityTypes.Message)
{
**await Conversation.SendAsync(activity, () => new MyBot.AppServices.ServiceLUIS());**
}
else
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var reply = HandleSystemMessage(activity);
if (reply != null)
await connector.Conversations.ReplyToActivityAsync(reply);
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
catch (Exception e)
{
return null;
}
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
Solved! Ended up creating another new key and password for the bot and republishing and it worked! –
Related
I'm implementing the InAppBilling plugin in my Xamarin Forms 5 app for auto-renewing subscriptions.
I have the "subscriptions" set up on Google Play and they're active. When I ask for a list of subscription items, I get the list fine but when I try make a purchase, I get the following error that indicates the item is not available.
I'm running this on a real device connected to my laptop via USB. Any idea what I'm doing wrong?
Here's my purchase subscription method which is directly from documentation here:
public async Task<bool> Subscribe(string productId)
{
var billing = CrossInAppBilling.Current;
try
{
var connected = await billing.ConnectAsync();
if (!connected)
return false;
//check purchases
var purchase = await billing.PurchaseAsync(productId, ItemType.Subscription);
//possibility that a null came through.
if (purchase == null)
{
//did not purchase
return false;
}
else
{
//purchased!
if (Device.RuntimePlatform == Device.Android)
{
// Must call AcknowledgePurchaseAsync else the purchase will be refunded
//await billing.AcknowledgePurchaseAsync(purchase.PurchaseToken);
}
return true;
}
}
catch (InAppBillingPurchaseException purchaseEx)
{
//Billing Exception handle this based on the type
throw new Exception("Error: " + purchaseEx);
}
catch (Exception ex)
{
//Something else has gone wrong, log it
throw new Exception();
}
finally
{
await billing.DisconnectAsync();
}
}
As I mentioned before, I see the subscription items available and active on Google Play. I also made sure, I'm getting them from "Subscriptions" and NOT "In-App Products". I'm using the ID that I copy and paste from the "Product ID" column on Google Play Console -- see below:
Any idea what the issue here may be?
I'm working on bot framework virtual assistant template. For getting QnA responses, bot uses following method
protected virtual QnAMakerDialog TryCreateQnADialog(string knowledgebaseId, CognitiveModelSet cognitiveModels, Metadata[] metadata)
{
if (!cognitiveModels.QnAConfiguration.TryGetValue(knowledgebaseId, out QnAMakerEndpoint qnaEndpoint)
|| qnaEndpoint == null)
{
throw new Exception($"Could not find QnA Maker knowledge base configuration with id: {knowledgebaseId}.");
}
// QnAMaker dialog already present on the stack?
if (Dialogs.Find(knowledgebaseId) == null)
{
return new QnAMakerDialog(
//dialogId: knowledgebaseId,
knowledgeBaseId: qnaEndpoint.KnowledgeBaseId,
endpointKey: qnaEndpoint.EndpointKey,
hostName: qnaEndpoint.Host,
strictFilters: metadata,
noAnswer: _templateManager.GenerateActivityForLocale("UnsupportedMessage"),
activeLearningCardTitle: _templateManager.GenerateActivityForLocale("QnaMakerAdaptiveLearningCardTitle").Text,
cardNoMatchText: _templateManager.GenerateActivityForLocale("QnaMakerNoMatchText").Text)
{
Id = knowledgebaseId,
LogPersonalInformation = true
};
}
else
{
return null;
}
}
var qnaDialog = TryCreateQnADialog(knowledgebaseId, localizedServices, metadata);
if (qnaDialog != null)
{
Dialogs.Add(qnaDialog);
}
return await stepContext.BeginDialogAsync(knowledgebaseId, cancellationToken: cancellationToken);
I want to check the result text in the QnA response and perform a set of operations before displaying the QnA response in chat window. Where can I get the response value for this QnA dialog ?
I have already gone through this question - how to get the qna response inside QnAMakerDialogbase: QnAMakerDialog, but I did not understand how can I override DisplayQnAResultAsync.
How to implement a welcome activity when the bot first starts - NLP is from Google Dialogflow.
I have designed the chatbot -intent, entities and NLP from Google Dialogflow and I have integrated successfully with the botframework webchat in a html file on referring this url.
The bot design and also the bot response is good to go. My most expected is am not getting the Bot response first here.
The welcome intent from Google Dialogflow has to get trigger from the following code as per the link given above.
But I am unable to get the bot trigger first here.
How to trigger the event of Google Dialogflow from the code.
I am expecting same like this
Note: Also referred this url
When a user joins WebChat, a conversation update activity will be sent to the bot. Once the activity is received, you can check if a member was added and send the welcome message accordingly.
If you are using the Activity Handler that was released in v4.3, you can simply add an onMembersAdded handler and send the welcome message from there.
class Bot extends ActivityHandler{
constructor() {
super();
this.onMembersAdded(async (context, next) => {
const { membersAdded } = context.activity;
for (let member of membersAdded) {
if (member.id !== context.activity.recipient.id) {
await context.sendActivity("Welcome Message!");
}
}
await next();
});
...
}
}
If you are not using the activity handler, in the bot's onTurn method, you can check if the incoming activity handler is a conversation update and if a member has been added.
async onTurn(turnContext) {
if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
if (turnContext.activity.membersAdded && turnContext.activity.membersAdded.length > 0) {
for (let member of turnContext.activity.membersAdded) {
if (member.id !== turnContext.activity.recipient.id) {
await turnContext.sendActivity("Welcome Message!");
}
}
}
} ...
}
For more details on sending welcome messages, please take a look at this sample.
Hope this helps!
I'm trying to create a chatbot where in order to avoid the user opening the chat window and not knowing the available options, I want to give some basic instructions when the user opens the chat window.
Is there any trigger available when the user opens a chat window? Maybe then I can check, and if there's not an ongoing conversation I could provide basic instructions.
I did some googling and found nothing about this. Is it possible to do something like this, and if not, is there a way to mitigate this problem, and provide the user with information regarding the chatbot capabilities and supported instructions?
Facebook does not allow bots to initiate a conversation, unlike Skype or other platforms.
There are still some tricks you can do :
Go on the Settings of your Facebook page, then Messaging and check "Show a Messenger Greeting" as below, and write your greeting sentence.
The result will look like this :
You can also set a "Get Started" button to trigger an event.
Here's the doc :
"https://developers.facebook.com/docs/messenger-platform/thread-settings/get-started-button"
You can monitor for two event types: ConversationUpdate and ContactRelationUpdate.
The first one (ConversationUpdate) is called when a user is added or removed from the conversation. So, there's a place where you can introduce available options. It will be each type the new conversation has started. So, it may become annoying, you may add a check - do not show it if the user has been using the bot for some time.
The second (ContactRelationUpdate) is called when a user adds or removes the bot to/from the contacts. In general, it is only called once per user action.
Here's the extract from the Bot-Frameworks examples:
For Node.Js
bot.on('conversationUpdate', function (message) {
// Check for group conversations
if (message.address.conversation.isGroup) {
// Send a hello message when bot is added
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Hello everyone!");
bot.send(reply);
}
});
}
// Send a goodbye message when bot is removed
if (message.membersRemoved) {
message.membersRemoved.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Goodbye");
bot.send(reply);
}
});
}
}
});
bot.on('contactRelationUpdate', function (message) {
if (message.action === 'add') {
var name = message.user ? message.user.name : null;
var reply = new builder.Message()
.address(message.address)
.text("Hello %s... Thanks for adding me. Say 'hello' to see some great demos.", name || 'there');
bot.send(reply);
} else {
// delete their data
}
});
For C#
private void HandleMessage(Activity message)
{
if (message.Type == ActivityTypes.ConversationUpdate)
{
if (activity.MembersAdded.Any(m => m.Id == activity.Recipient.Id))
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var response = activity.CreateReply();
response.Text = "Hi! I am Bot. Here's what you can do...";
await connector.Conversations.ReplyToActivityAsync(response);
}
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
if (Activity.AsContactRelationUpdateActivity().Action == ContactRelationUpdateActionTypes.Add)
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var response = activity.CreateReply();
response.Text = "Hi! I am Bot. Thanks for adding me. Here's what you can do...";
}
}
return null;
}
I think the acid answer is not.
But you can intercept the IConversationUpdateActivity type message to know if the user has added the bot to a conversation. In the C# project template you can find a code block that ask for this message type but do nothing.
How can I send a message to the user without the user sending me a message? Like for example CNN bot is sending messages every day in the morning by itself. How can I do that in the bot framework?
See this.
In fact, you do not strictly need to receive a message from the user first, but addressing manually can be error-prone (you have to know the user's and bot's channel account, the service URL, etc.)
And in turn (per #thegaram's message), that only works for some channels. For example, Skype requires that the user contact the bot before the bot can message the user.
Once contacted, you can store the user's channelAccount data once they contact you and use that to send them proactive messages. For example if the user has subscribed to hear sports scores for a particular team over time.
Any sort of unsolicited spam messages of course are prohibited by the policies of the Bot Framework (and most of the channels).
Yes you can do that. We called it Greeting from Bot. I have done it and sharing a sample code with you.
Write this code in your messageController or first dialog used in bot.
if (activity.Text == null)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity isActivityTyping = activity.CreateReply();
isActivityTyping.Type = ActivityTypes.Typing;
await connector.Conversations.ReplyToActivityAsync(isActivityTyping);
await Conversation.SendAsync(activity, () => new Dialogs.GreetDialog());
}
after this code you need to create a dialog GreetDialog. Below is the cs file code for your reference.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace GPP.Bot.Dialogs
{
[Serializable]
internal class GreetDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(Greeting);
}
private async Task Greeting(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
if (string.IsNullOrEmpty(message.Text))
{
// Hero Card
var cardMsg = context.MakeMessage();
var attachment = BotWelcomeCard("Hello, I am a bot. Right now I am on training and in a prototype state", "");
cardMsg.Attachments.Add(attachment);
await context.PostAsync(cardMsg);
context.Call<object>(new ActionDialog(), AfterGreetingDialogCompleted);
}
else
{
context.Call<object>(new ActionDialog(), AfterGreetingDialogCompleted);
}
}
private static Attachment BotWelcomeCard(string responseFromQNAMaker, string userQuery)
{
var heroCard = new HeroCard
{
Title = userQuery,
Subtitle = "",
Text = responseFromQNAMaker,
Images = new List<CardImage> { new CardImage("https://i2.wp.com/lawyerist.com/lawyerist/wp-content/uploads/2016/08/docubot.gif?fit=322%2C294&ssl=1") },
Buttons = new List<CardAction> { new CardAction(ActionTypes.ImBack, "Show Menu", value: "Show Bot Menu") }
};
return heroCard.ToAttachment();
}
private async Task AfterGreetingDialogCompleted(IDialogContext context, IAwaitable<object> result)
{
context.Done<object>(new object());
}
}
}
this is a working code. Do let me know in case you face ant issue.
~cheers :)