I am using NodeJs API of Microsoft Bot Framework v4. And my dialogs are not hardcoded in the ActivityHandler, I just call from there. I am using Waterfall dialogs. So when I try to show Carousel card on messenger (which is HeroCard on Microsoft bot framework), it shows up successfully but when I click any button on cards, there is no response for next dialog.
I tried to handle on onMessage hook but, it just tries to validate the response and throw errors.
....
ListItems extends ComponentDialog {
constructor(userProfileState, conversionStateAccessor) {
super(LIST_ITEMS);
this.userState = userProfileState;
this.conversionState = conversionStateAccessor;
this.addDialog(new WaterfallDialog(LIST_ITEMS_DIALOG, [
this.sendItems.bind(this),
this.handleItems.bind(this)
]
));
this.initialDialogId = LIST_ITEMS_DIALOG;
}
async sendItems(step) {
.......
await step.context.sendActivity({ attachments: cards }); // this line is working
}
async handleItems(step) {
console.log(step.result) // the response is not in 'step.result.value' if i click a button on cards
}
Thanks for your help
---- I added more detail -----
I am using this template to create cards
const card = CardFactory.heroCard('title', 'subtitle', ['imagelink'], [
{ type: ActionTypes.PostBack,
title: 'product 1',
value: 'product_1'
},
{ type: ActionTypes.PostBack,
title: 'product 1',
value: 'product_1'
},
]);
await context.sendActivity({ attachments: [card] });
Cars can be created successfully but the problem is, after that, I send a prompt to let the user turn the main menu if the user wants.
so I send them like that
await context.sendActivity({ attachments: [card] });
await step.prompt(LIST_ITEMS_MENU_PROMPT, menuPromptPayload);
And if the user clicks a button on cards, an error is thrown because I think the framework waits for prompt's answers. Couldn't catch the card's button's payload/
You need to instruct your bot to wait for and return a response following the sendActivity in sendItems. Similarly, you will encounter an error in the handleItems if you don't tell the bot to continue on since there is nothing happening other than console logging.
async sendItems(step) {
.......
await step.context.sendActivity({ attachments: cards });
// Tells the bot to wait. Normally, on `sendActivity`, it continues on to the next step.
// "DialogTurnStatus" can be found in the `botbuilder-dialogs` package.
return { status: DialogTurnStatus.waiting };
}
async handleItems(step) {
console.log(step.result);
return step.next(); // Tells the bot to continue to the next step.
}
Hope of help!
Related
I am using the nodejs SDK for Bot Framework to develop a chatbot. I want to send a message to the user if they do not write in 5 minutes.
I do not find an example in bot-framework documentation and, in stackoverflow there are not solutions for a started bot (I do not need it to start the conversation). Where do I need to create the code? I have an index.js and a dialog file. How can I set the timer and restart it when the user send a message?
I'm using directline.
Thanks
There are two different ways you can approach this, one for directline only using events and one for all channels using setTimeout. The directline solution requires some code on your webchat client, but the latter requires you to save the conversation reference and start a new bot adapter. Both approaches could work.
Directline Only
You need to set up your webchat client to set up the timer and send an event to your bot if no activities are sent before the timer expires. You need to create a custom store to do this. Here is an example I used in the past:
const store = window.WebChat.createStore({}, function(dispatch) { return function(next) { return function(action) {
if (action.type === 'WEB_CHAT/SEND_MESSAGE') {
// Message sent by the user
clearTimeout(interval);
} else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY' && action.payload.activity.name !== "inactive") {
// Message sent by the bot
clearInterval(interval);
interval = setTimeout(function() {
// Notify bot the user has been inactive
dispatch.dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'inactive',
value: ''
}
});
}, 300000)
}
return next(action);
}}});
This will send an event to your bot with the name 'inactive'. Now you need to set up your bot to handle it. So in your this.onEvent handler you need to do something like this:
if (context.activity.name && context.activity.name === 'inactive') {
await context.sendActivity({
text: 'Are you still there? Is there anything else I can help you with?',
name: 'inactive'
});
}
All channels
As I'm typing this up, I'm realizing you should be able to emit the event from your bot itself and forego starting a new bot adapter instance. But I haven't tried that before, so I'm providing my existing solution. But you may wish to experiment with emitting an inactive event if the timeout is reached instead of the actions below.
That said, here is a solution you can use within your this.onMessage handler.
// Inactivity messages
// Reset the inactivity timer
clearTimeout(this.inactivityTimer);
this.inactivityTimer = setTimeout(async function(conversationReference) {
console.log('User is inactive');
try {
const adapter = new BotFrameworkAdapter({
appId: process.env.microsoftAppID,
appPassword: process.env.microsoftAppPassword
});
await adapter.continueConversation(conversationReference, async turnContext => {
await turnContext.sendActivity('Are you still there?');
});
} catch (error) {
//console.log('Bad Request. Please ensure your message contains the conversation reference and message text.');
console.log(error);
}
}, 300000, conversationData.conversationReference);
Note that you have to get and save the conversationReference if you go this route, so that you can call continueConversation if the timer expires. I typically do this in my this.onMessage handler as well just to make sure I always have a valid conversation reference. You can get it with the below code (I'm assuming you already have your conversation state and state accessor defined).
const conversationData = await this.dialogState.get(context, {});
conversationData.conversationReference = TurnContext.getConversationReference(context.activity);
Now as I mentioned in the first solution, I believe you should be able to send an inactivity event in your try block instead of initiating the bot adapter. If you try that and it works, please let me know so I can update this solution!
I'm using slackbots (https://github.com/mishk0/slack-bot-api) to create an interactive bot.
I can, as a user, send message in chat and having my bot giving me an answer depending on what i wrote. Like if I type : !weekend, my bot will answer if weekend is close or not by fetching an API.
It's working this way
bot.on('message', data => {
if (data.type !== 'message') {
return;
}
handleMessage(data.text);
});
function handleMessage(message) {
switch (message) {
case '!weekend':
case '!we':
fetchWeekendData();
break;
case '!apero':
case '!biere':
case '!pastis':
fetchAperoData();
break;
case '!meteo':
fetchWeatherData();
break;
case '!metro':
fetchMetroData('A');
fetchMetroData('R');
break;
case '!features':
getFeatures();
break;
}
}
function fetchWeekendData() {
axios.get('https://estcequecestbientotleweekend.fr/api',
).then(res => {
const weText = res.data.text;
postToChannel(`_Bientôt le week end ?_ : *${weText}*`);
});
}
function postToChannel(message) {
bot.postMessageToChannel(
'général',
message,
params
)
}
I have another function calling, this time, a weather api. The api respond with many infos and
a link to a png depending on the weather. A little weather icon.
Problem is, when i send back my message to chat, it send the image link and i just can't figure out how to get this link as an image in chat.
function fetchWeatherData() {
axios.get('http://api.weatherstack.com/current?access_key=myAPIkey&query=Paris&units=m',
).then(res => {
const location = res.data.location.name;
const icon = res.data.current.weather_icons;
const temperature = res.data.current.temperature;
const feelslike = res.data.current.feelslike;
const humidity = res.data.current.humidity;
postToChannel(`Météo à *${location}* : ${icon} \n>_Température_ : *${temperature}* °C _Ressenti_ : *${feelslike}* °C\n\n>_Humidité_ : *${humidity}* %`)
});
}
If anyone has already used slack-bot-api and posted images with his bot, I'd like to know how you did this cause I'm running out of idea, slack api docs is showing JSON attachments for images but I don't know how to use it with this package.
EDIT : Alright it's solved, this way
function postToChannelWithImage() {
bot.postMessageToChannel(
'général',
'yikes itis working',
params = {
"icon_url": "https://avatars.slack-edge.com/2020-02-04/935676215584_38623245747b942874b5_192.jpg",
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://a.slack-edge.com/80588/img/blocks/bkb_template_images/beagle.png"
}
]
}
)
}
I added the array and it worked perfectly, I just have to add other params into the same array to keep my config the way it is.
Thanks a lot to #Erik Kalkoken !!
This library seams to just forward parameters to the respective Slack API method, which is chat.postMessage.
To post an image with Slack you need to include an image URL either within an attachment (outmoded) or blocks.
e.g. to attach an image wit attachments you would add this array via params:
{
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://a.slack-edge.com/80588/img/blocks/bkb_template_images/beagle.png"
}
]
}
Issue :
how to show typing acitivity for every user activity ?
Platform : node js 101.5
Framework : Bor Framework 4.5
You can set Web Chat's sendTyping prop to true and then echo the typing events sent to bot back to the conversation.
Web Chat
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
sendTyping: true
},
document.getElementById('webchat')
);
BotFramework SDK v4 (Node)
this.onUnrecognizedActivityType(async context => {
const { activity: { type } } = context;
if (type === ActivityTypes.Typing) {
await context.sendActivity({ type: ActivityTypes.Typing });
}
});
Screen Capture
Currently you have to capture typing events from the user in the onUnrecognizedActivityType message handler; however, there is an open GitHub Issue to add an onTyping event handler.
Hope this helps.
I am trying to send a postBack text message to my bot but I don't know the right syntax.
Here is my code:
if (postback.payload == "WHAT_IS_MENTAL_HEALTH") {
await turnContext.sendActivity("TO-DO: Forward on 'What Is Mental Health?' to Bot Handler");
ActionTypes.postBack("What Is Mental Health?");
}
I'm trying to forward on the text "What Is Mental Health?" to my bot so it will pull back the QnA Maker response for that question.
The steps for this are as follows:
User clicks a button on a Facebook Messenger Generic Template Card (e.g. “What Is Mental Health?” Button)
The button sends a postBack payload to the bot (e.g. “WHAT_IS_MENTAL_HEALTH”)
I am detecting the postBack payload from Facebook Messenger (e.g if (postBack.payload == “WHAT_IS_MENTAL_HEALTH”))
Once that particular postBack payload is detected I then want to send an additional postBack to my bot as text (e.g. “What Is Mental Health?”) so it can be interpreted by my QnA and respond with the correct answer which has been programmed into my QnA Knowledge Base.
Facebook Events and the Bot Framework
When Facebook sends an event to your bot, it sends an Activity with an Event ActivityType. For some events, the event data is in the Activity.Value property. For other events, like a PostBack from a Quick Reply, the activity will be a Message and the data will be in Activity.ChannelData. For example, your bot might receive a postBack event as an activity like this:
{
channelId: 'facebook',
[...]
type: 'message',
channelData: {
recipient: {...},
sender: {...},
message: {
[...],
quick_reply: {
[...],
payload: '<your payload>'
}
}
}
}
Handling Facebook Events
This answer is going to pull heavily from the Facebook Events Sample. I highly recommend looking at that for additional help.
Capture Messages and Events
First, you want to capture the facebook messages and events with onMessage() and onEvent():
this.onMessage(async (turnContext) => {
console.log('Processing a Message Activity.');
// Show choices if the Facebook Payload from ChannelData is not handled
if (!await this.processFacebookPayload(turnContext, turnContext.activity.channelData)) {
if (turnContext.activity.channelId !== 'facebook') {
await turnContext.sendActivity('This sample is intended to be used with a Facebook bot.');
}
await this.showChoices(turnContext);
}
});
this.onEvent(async (turnContext) => {
console.log('Processing an Event Activity.');
// Analyze Facebook payload from EventActivity.Value
await this.processFacebookPayload(turnContext, turnContext.activity.value);
});
Process the Messages/Events
Facebook can send many types of events. You may want to use an if or switch statement to handle each type:
async processFacebookPayload(turnContext, data) {
// At this point we know we are on Facebook channel, and can consume the Facebook custom payload present in channelData.
const facebookPayload = data;
if (facebookPayload) {
if (facebookPayload.postback) {
// Postback
await this.onFacebookPostback(turnContext, facebookPayload.postback);
return true;
} else if (facebookPayload.optin) {
// Optin
await this.onFacebookOptin(turnContext, facebookPayload.optin);
return true;
[...]
}
return false;
}
Specifically, Handle a PostBack
The sample just does:
async onFacebookPostback(turnContext, postback) {
console.log('Postback message received.');
// TODO: Your postBack handling logic here...
// Answer the postback and show choices
await turnContext.sendActivity('Are you sure?');
await this.showChoices(turnContext);
}
As you want to route the question to QnA Maker, you might (using the QnA Maker Sample as guidance):
async onFacebookPostback(turnContext, postback) {
// qnaMaker.getAnswers doesn't accept string input, so we need to adjust our turnContext
// to match what it expects, which is a string in Activity.Text
turnContext.activity.text = postback.payload;
const qnaResults = await this.qnaMaker.getAnswers(turnContext);
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
await turnContext.sendActivity(qnaResults[0].answer);
// If no answers were returned from QnA Maker, reply with help.
} else {
await turnContext.sendActivity('No QnA Maker answers were found.');
}
}
I want to open widgets.getsitecontrol.com/ javascript page that I have implemented on my website. Whenever I type 'Help' inside my bot, the widget should open. Is it possible to open it? Thanks. I am using node js version. If it is possible, please provide me an approach to solve this issue.
I'm not sure exactly how your widget functions, but when the user sends a 'help' message to the bot, you can send a back channel event to WebChat to trigger opening the widget. Take a look at the code snippets below.
Bot Code - NodeJs
When the bot receives a 'help' message from the user, the bot can send an event by sending an activity with the type set to 'event'. We can also give the outgoing activity a name attribute so we can send mutltiple types of events to WebChat. In this case, we are going to name the out going activity 'helpEvent'.
async onTurn(turnContext) {
if(turnContext.activity.type === ActivityTypes.Message) {
if (turnContext.activity.text.toLowerCase() === 'help') {
// Send Back Channel Help Event
await turnContext.sendActivity({ type: 'event', name: 'helpEvent'});
}
...
}
}
WebChat Custom Middleware
In WebChat, we are going to create a custom middleware to check incoming activities. When we encounter an activity that has a name and type that we recognize, trigger your event on the webpage. In the example below, I just alerted the use that they asked for help, but here is where you launch your widget.
const store = window.WebChat.createStore(
{},
({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
const { name, type } = action.payload.activity;
if (type === 'event' && name === 'helpEvent') {
// Activate Widget
alert("You asked for help.");
}
}
return next(action);
}
);
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
store,
}, document.getElementById('webchat'));
For more details on back channel events and creating a custom middleware in WebChat, checkout this sample in the WebChat Repo.
Hope this helps!