Alexa skill doesn't execute intent - aws-lambda

I have a skill, I'm trying to test it with the "test" function in alexa developer console. If I give the invocation name, it gets recognized to be the specific intent, but the response doesn't match. (It might be something glaringly obvious that I just can't notice anymore.)
I have a LaunchRequest type, it works with the invocation name.
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
welcomeMessage = `updated welcome`;
return handlerInput.responseBuilder
.speak(welcomeMessage)
.reprompt(helpMessage)
.getResponse();
},
};
(welcomeMessage is declared outside, this was just testing if the issue was giving it new value)
However, when it comes to an intent based on user input (in this case TestIntent, the user input is "is the skill working"), it just doesn't work.
TestIntent's code is the same as LaunchRequest, except the intent type&name check
const request = handlerInput.requestEnvelope.request;
return (request.type === "IntentRequest" &&
request.intent.name === "TestIntent");
The alexa skill's json input recognizes the input as a TestIntent
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.601d2e89-71c1-417e-b878-790afc6f79f4",
"timestamp": "2019-08-12T07:01:38Z",
"locale": "en-US",
"intent": {
"name": "TestIntent",
"confirmationStatus": "NONE"
},
"dialogState": "STARTED"
}
But the response is just "I am sorry, but I do not know that. Can you repeat that?"

You need to create your custom intent with utterances.
Login to https://developer.amazon.com
Create your skill and add your utterances which will map to specific intent.
Sample:
{
"interactionModel": {
"languageModel": {
"invocationName": "mySkill",
"intents": [
{
"name": "TestIntent",
"slots": [
{
"name": "name",
"type": ""
}
],
"samples": [
"test me", // This would be your utterance to identify intent
"testing you" // You can have multiple
]
},
{
"name": "AMAZON.FallbackIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.NoIntent",
"samples": []
}
]
}
}
}
Below is the walkthrough to the developer account
1) Create your intent
2) Create your utterances
And then just build your modal. Your Skill needs to be linked to your lambda function.
Hope this help!
======UPDATE=====
Need to return card response
response = {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: title,
content: output
},
shouldEndSession: shouldEndSession
};
Using aw-sdk: (Sample)
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}

Related

Triggering an Action.Execute on an adaptive card sent by a chat bot on Teams mobile apps caused a "Something Went Wrong" error

Versions
Package: botbuilder#4.11.0
Nodejs: v16.19.0
Teams Android Client: 1416/1.0.0/2023012702/0115
I have a chatbot that functions as a notification bot that sends an adaptive card notification to our users.
On the card, the user can reply to the notification by typing in the Input.Text field and then clicking a send button that triggers an Action.Execute, our Nodejs bot will then process the activity by sending notifications to corresponding user and then updating the card with a new card.
This works perfectly on Web and Desktop app but in the Teams Android App the card will briefly shows an error message saying "Something went wrong" before our card is finally updated.
These is the card that I send:
{
"type": "AdaptiveCard",
"appId": process.env.MicrosoftBotAppId,
"body": [
{
"type": "TextBlock",
"text": message,
"wrap": true
},
{
"id": "history_title",
"type": "TextBlock",
"text": cardString.pastEvents,
"wrap": true
},
{
"type": "Container",
"spacing": "None",
"items": [
{
"id": "last_trail_name",
"type": "TextBlock",
"text": lastTrail ? cardString.action(lastTrail.userName, lastTrailAction) : "",
"spacing": "None",
// "size": "Small",
"wrap": true
},
{
"id": "last_trail_comment",
"type": "TextBlock",
"text": lastTrail ? `${lastTrail.comment.replace(/<\/?[^>]+(>|$)/g, "")}` : "",
"spacing": "None",
// "size": "Small",
"wrap": true
},
{
"id": "last_trail_date",
"type": "TextBlock",
"text": lastTrail ? `${lastTrailDateString}` : "",
"spacing": "None",
"isSubtle": true,
"size": "Small",
"wrap": true
}
]
},
{
"type": "Container",
"items": [
{
"id": "second_last_trail_name",
"type": "TextBlock",
"text": secondLastTrail ? cardString.action(secondLastTrail.userName, secondLastTrailAction) : "",
"spacing": "None",
// "size": "Small",
"wrap": true
},
{
"id": "second_last_trail_comment",
"type": "TextBlock",
"text": secondLastTrail ? `${secondLastTrail.comment.replace(/<\/?[^>]+(>|$)/g, "")}` : "",
"spacing": "None",
// "size": "Small",
"wrap": true
},
{
"id": "second_last_trail_date",
"type": "TextBlock",
"text": secondLastTrail ? `${secondLastTrailDateString}` : "",
"spacing": "None",
"isSubtle": true,
"size": "Small",
"wrap": true
}
]
},
{
"id": "comment",
"isRequired": true,
"type": "Input.Text",
"placeholder": cardString.commentPlaceholder,
"isMultiline": true
},
{
"id": "success-msg",
"type": "TextBlock",
"text": success ? cardString.commentSentAlert : "",
"spacing": "None",
"isSubtle": true,
"size": "Small",
"wrap": true,
"color": "good"
}
],
"actions": [
{
// "tooltip": isPremium ? cardString.send : cardString.premiumTooltip,
// "isEnabled": isPremium,
"type": "Action.Execute",
"verb": "comment",
"title": cardString.send,
"data": data,
},
{
"type": "Action.OpenUrl",
"title": cardString.goToTicketButton,
"url": deeplink
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.4"
}
And this is the bot that processed the action:
class Bot extends TeamsActivityHandler {
constructor() {
super();
this.onMembersAdded(async (turnContext: TurnContext, next: () => Promise<any>) => {
const gettingStartedUrl: string = 'https://www.teamswork.app/gettingstarted';
const membersAdded = turnContext.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== turnContext.activity.recipient.id) {
const welcomeMessage = "Welcome message"
// add conv references
if (await this.storeConversationReference(turnContext))
await turnContext.sendActivity(welcomeMessage); // Only send notification if conversation reference store (one time)
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
// Listener for activity e.g. comment activity on notification card
async onInvokeActivity(turnContext: TurnContext): Promise<InvokeResponse<any>> {
// Listen for activity invocation and check if the comment action on adaptiveCard has been called
const activity: Activity = turnContext.activity;
// context.log('activity: ', activity);
// turnContext.sendActivity("Hello World!");
let card: object;
if (activity.name === "adaptiveCard/action") {
// context.log("action activity");
const action = activity.value.action;
// context.log("action : ", action);
if (action.verb === "comment") {
const userName: string = activity.from.name;
const userId: string = activity.from.aadObjectId;
const data: IActionData = action.data;
// context.log('data: ', action.data);
const date: Date = new Date();
const tenantId: string = activity.conversation.tenantId;
const comment: string = data.comment;
const lang: string = data.lang || "en";
// assignee Ticket == user commennt
if ((data.ticket.assigneeId == userId) && (data.ticket.firstTimeResponse == "")) {
this.updateTicket(data.ticket)
}
// Check for ticketId
if (!!!data.ticket) {
context.log("No ticketId found, aborting");
// throw ("No ticketId found, aborting")
}
// Construct new audit trail / comment
let newComment: IAuditTrail = {
userName: userName,
userId: userId,
ticketId: data.ticket.id,
action: "commented",
comment: comment,
date: date,
}
// initialize parameters for card to be sent
const cardContent: ICardContent = {
userName: userName,
type: "comment",
action: "commented",
comment: comment,
isRequestor: false,
}
// Prepare response message
const encodedDeeplink: string = constructDeeplink(data.ticket, data.ticketingAppUrl, data.channelId, data.entityId);
const message: string = constructMessage(data.ticket, data.cardContent, encodedDeeplink, lang);
const isPremium: boolean = await getPremium(tenantId);
// const card = invokeSuccessResponse(encodedDeeplink, message, data);
// Save audit trail
const insertCommentEndpoint: string = process.env["InsertCommentEndpoint"];
try {
await cosmosService.insertItem(insertCommentEndpoint + encodeQueryParams({ instanceId: data.ticket.instanceId }), { comment: newComment });
} catch (error) {
context.log(error);
}
// Send request to SendCardNotification API
try {
// Notification recipient id array
let recipientIds: string[] = [];
if (data.ticket.requestorId)
recipientIds.push(data.ticket.requestorId)
if (data.ticket.assigneeId)
recipientIds.push(data.ticket.assigneeId)
// If ticket have custom fields get people from them
if (data.ticket.customFields) {
// Get instance so we can get customFieldSetting
const instanceContainer: Container = ticketDatabase.container(process.env["InstanceContainer"]);
const { resource: instance } = await instanceContainer.item(data.ticket.instanceId, tenantId).read<IInstance>();
// Create array for user who will receive the notification
let activePersonaCustomFields: ICustomField[] = [];
if (instance.customFieldsLeft)
activePersonaCustomFields.push(...instance.customFieldsLeft);
if (instance.customFieldsRight)
activePersonaCustomFields.push(...instance.customFieldsRight);
activePersonaCustomFields = activePersonaCustomFields.filter((value) => value.type.key === "6_peoplepicker");
activePersonaCustomFields = activePersonaCustomFields.filter((value) => value.isReceiveNotification);
if (activePersonaCustomFields.length > 0) {
for (const fields of activePersonaCustomFields) {
if (data.ticket.customFields[fields.id]?.length > 0) {
const personas: IPersonaProps[] = data.ticket.customFields[fields.id];
for (const element of personas) {
recipientIds.push(element.id)
}
}
}
}
}
// Remove message sender from recipient list
recipientIds = recipientIds.filter((id) => id !== userId);
// Get unique notification recipient array
const uniqueRecipientIds: string[] = [... new Set(recipientIds)];
context.log("Sending notifications");
for (const id of uniqueRecipientIds) {
await this.sendNotification(cardContent, id, tenantId, data);
}
card = NotificationCard(encodedDeeplink, message, data, true, isPremium, lang);
const cardAttachment: Attachment = CardFactory.adaptiveCard(card);
const activityPayload = MessageFactory.attachment(cardAttachment);
activityPayload.id = turnContext.activity.replyToId;
context.log("Updating cards");
await turnContext.updateActivity(activityPayload);
} catch (error) {
context.log(error)
}
}
}
const cardRes = {
statusCode: StatusCodes.OK,
type: 'application/vnd.microsoft.card.adaptive',
value: card
};
const res = {
status: StatusCodes.OK,
body: cardRes
};
return res;
}
}
It actually does not matter what I do on the card, even if I do nothing and then only return the InvokeResponse, the error still happens.
Expected behavior
The card is updated without any error message shown.
Screenshots

UpdateActivity throws 400 "Unknown activity type"

I do have a Bot that is reachable via MS Teams. The bot sends an Adaptive Card with some Text and a submit-action. When the user clicks on this submit-action, I want to proceed the input and then update the prior sent Adaptive card via calling context.updateActivity. According to documentation, I can use activity.Id = turnContext.Activity.ReplyToId; to specify the message I want to update. But the call of context.updateActivity results in a 400 HTTP error, the message is "Unknown activity type".
Some investigation:
This error occurs when I want to send another Adaptive Card and when I want to send plain text
I verified, that the id of sendActivity is the same as turnContext.Activity.ReplyToId
Any idea?
Here is my code:
Adaptive Card
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Some text",
"wrap": true
},
{
"type": "Input.ChoiceSet",
"id": "Feedback",
"value": "",
"style": "compact",
"placeholder": "Wie hilfreich war diese Antwort?",
"choices": [
{
"title": "⭐",
"value": "1"
},
{
"title": "⭐⭐",
"value": "2"
},
{
"title": "⭐⭐⭐",
"value": "3"
},
{
"title": "⭐⭐⭐⭐",
"value": "4"
},
{
"title": "⭐⭐⭐⭐⭐",
"value": "5"
}
]
}
],
"actions": [
{
"title": "Feedback absenden",
"type": "Action.Submit"
}
]
}
Sending the message:
private handleMessage = async (context: TurnContext, next: () => Promise<void>): Promise<void> => {
const adaptiveCard = AdaptiveCardFactory.createAdaptiveCardFromTemplateAndData(AnswerWithFeedbackCard);
const result = await context.sendActivity({ attachments: [adaptiveCard] });
console.error("send msg with id " + result?.id);
}
code to update the message:
private handleMessage = async (context: TurnContext, next: () => Promise<void>): Promise<void> => {
console.error("received msg with id " + context.activity.replyToId);
if (context.activity.value && !context.activity.text) {
const updatedCard = CardFactory.adaptiveCard(this.botConfig.updatedCard);
await context.updateActivity({ text: "updated :)", id: context.activity.replyToId});
//or
await context.updateActivity({ attachments: [updatedCard], id: context.activity.replyToId});
}
}
Got it!
const att = AdaptiveCardFactory.createAdaptiveCardFromTemplateAndData(AnswerWithAnsweredFeedbackCard, {
answer: "Mir geht's super, danke der Nachfrage!",
starsCount: context.activity.value.Feedback
});
const id = await context.updateActivity( { attachments: [ att ], id: context.activity.replyToId} );
This does not work, but this does:
const att = AdaptiveCardFactory.createAdaptiveCardFromTemplateAndData(AnswerWithAnsweredFeedbackCard, {
answer: "Mir geht's super, danke der Nachfrage!",
starsCount: context.activity.value.Feedback
});
const msg = MessageFactory.attachment( att )
msg.id = context.activity.replyToId;
const id = await context.updateActivity( msg )
So, you need to save the save the sent msg in a variable and set the id of this variable instead of using the "inplace"-implementation of updateActivity

How do I tell Alexa to prompt user for input

I need to tell alexa to prompt user for input then store that input in a variable to be used in my code.
InvocationName: send mail
Alexa: Tell me mail subject
User: Test email
Alexa: Okay, tell me message body.
User: This is just a sample test
Alexa, okay, tell me receiver email
User: test#gmail.com
Below is my intent schema:
{
"interactionModel": {
"languageModel": {
"invocationName": "send mail",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.FallbackIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "SendMailIntent",
"slots": [
{
"name": "ReceiverEmail",
"type": "AMAZON.SearchQuery"
}
],
"samples": [
"mail",
"send mail"
]
}
],
"types": []
},
"dialog": {
"intents": [
{
"name": "SendMailIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "ReceiverEmail",
"type": "AMAZON.SearchQuery",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.838288524310.965699312002"
}
}
]
}
],
"delegationStrategy": "ALWAYS"
},
"prompts": [
{
"id": "Elicit.Slot.838288524310.965699312002",
"variations": [
{
"type": "PlainText",
"value": "Enter subject"
}
]
}
]
}
}
and below is the code I have been able to come up with:
// sets up dependencies
const Alexa = require('ask-sdk-core');
const i18n = require('i18next');
const languageStrings = require('./languageStrings');
const SendMailHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// var code = this.event.request.intent.slots.code.value;
//console.log(code)
// checks request type
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'SendMailIntent');
},
handle(handlerInput) {
const speechText = 'Ok. Tell me the mail subject'
const response = handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText) // <--- Here is our reprompt
.getResponse();
console.log(response)
return response;
},
};
// Omitted default Alexa handlers
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
SendMailHandler,
)
.lambda();
You should use dialog management with 2 slots.
As I can see currently you only collect one slot (ReceiverEmail) with dialoge management.
But you need also to create a slot for the text you want to send.
Later in your code you need to check if the dialogue is in status COMPLETED.
See the example https://github.com/alexa/skill-sample-nodejs-petmatch/ or this video: https://www.youtube.com/watch?v=u99WMljnQXI

Adaptive Card rendered as image on FB shows "Title" text/element

I understand that adaptive cards are down-rendered as image on channels that does not support them. I just want to know how to either remove or set the "Title" element as shown on the fb channel:
The AdaptiveCard.Title element is deprecated and I did try setting that, it did not have any effect.
Here is my sample json:
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"id": "Title",
"horizontalAlignment": "Center",
"size": "Large",
"weight": "Bolder",
"text": "See results on our website!"
},
{
"type": "Image",
"horizontalAlignment": "Center",
"url": "mylogo.png",
"size": "Stretch"
},
{
"type": "TextBlock",
"id": "Subtitle",
"horizontalAlignment": "Center",
"size": "ExtraLarge",
"text": "This channel does not allow us to display your results. Click the button to view it on our website.",
"wrap": true
}
],
"actions": [
{
"type": "Action.OpenUrl",
"id": "OpenUrl",
"title": "Take me there!"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
Unfortunately, when the BotFramewrok renders the card into an image for Facebook Messenger it adds the title above the actions which is strange. The only way to get rid of it is to remove the action from the card, which defeats its purpose in this case. An alternative is to send a Facebook Button Template in the activity's channel data instead of the adaptive card when the user is on Facebook Messenger. For more details checkout the Facebook Documentation on Button Templates and the code snippet below.
Screenshot
Bot Code - Node
async onTurn(turnContext) {
if (turnContext.activity.type === ActivityTypes.Message) {
if (turnContext.activity.channelId === 'facebook') {
await turnContext.sendActivity({
channelData: {
"attachment": {
"type": "template",
"payload": {
"template_type":"button",
"text":"This channel does not allow us to display your results. Click the button to view it on our website.",
"buttons":[{
"type":"web_url",
"url":"https://www.microsoft.com",
"title":"Take me there!"
}]
}
}
}
});
} else {
await turnContext.sendActivity({
attachments: [this.createAdaptiveCard()],
});
}
}
}
Hope this helps!
I had the same problem but I find an alternative using HeroCard.
If you are developing it with C# there it goes the code:
// first of all check if it is Facebook channel
// note that Channels is from Microsoft.Bot.Connector, the old one is deprecated.
if (turnContext.Activity.ChannelId == Channels.Facebook)
{
Activity replyToConversation = _flowService.ConvertMarkdownUrlToFacebookUrl(turnContext.Activity, response.Answer);
await turnContext.SendActivityAsync(replyToConversation, cancellationToken: cancellationToken);
}
public Activity ConvertMarkdownUrlToFacebookUrl(Activity message, string queryResponse)
{
var buttons = getButtons(queryResponse, out string result);
Activity replyToConversation = message.CreateReply();
replyToConversation.Attachments = new List<Attachment>();
List<CardAction> actions = new List<CardAction>();
foreach (var button in buttons)
{
actions.Add(new CardAction()
{
Title = button.Key, // text hyperlink
Type = ActionTypes.OpenUrl,
Value = button.Value // url
});
}
Attachment attachment = new HeroCard
{
Text = result, // your text
Buttons = actions
}.ToAttachment();
replyToConversation.Attachments.Add(attachment);
return replyToConversation;
}
You'll obtain something like this:
(sorry I had to delete the text)
Maybe it's not perfect (my first time with cards and attachments) but I hope that this will help someone 🙂

slack bot and form controls not invoking external api

I'm attempting to invoke my api which is self hosted via ngrok. ngrok is working correctly because I can open the external https site in my browser and it loads up.
So, what do I have
slack form:
var request = require('request');
var RegisterMsgBuilder = function () { }
RegisterMsgBuilder.prototype.build = function (data) {
return {
"text": "Would you like to play a game?",
"attachments": [
{
"text": "Choose a game to play",
"fallback": "You are unable to choose a game",
"callback_id": "wopr_game",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "game",
"text": "Chess",
"type": "button",
"value": "chess"
},
{
"name": "game",
"text": "Falken's Maze",
"type": "button",
"value": "maze"
},
{
"name": "game",
"text": "Thermonuclear War",
"style": "danger",
"type": "button",
"value": "war",
"confirm": {
"title": "Are you sure?",
"text": "Wouldn't you prefer a good game of chess?",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
}
]
}
};
exports.RegisterMsgBuilder = RegisterMsgBuilder;
this produces a form in slack with 3 buttons.
I've set up interactive components with the following url:
https://xyz.ngrok.io/api/values
this url points to a new asp.net webapi project. I've not changed anything in here. Its got a controller which looks like:
namespace WebApplication2.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post()
{
int x = 66;
}
}
}
clicking on a button in my slack message does nothing. How come?
Primarily, you haven't given any kind of response for the API to act on. All your controller seems to do on receiving a POST is set x equal to 66.
When someone clicks that button in-channel, a POST request will be made to your action URL. It'll contain some information about the button clicked, the original message, etc (https://api.slack.com/interactive-messages#responding). You don't seem to be pulling the values from the POST your app receives at all - you'll definitely need to do that.

Resources