How use api.ai + webhook + lambda - aws-lambda

I am in the development of a chatbot and I am doing tests of how to gear the technologies to use. My thought is to use Facebook Messenger linked to API.ai and from API.ai use Webhook to send to Amazon API Gateway + Lambda to perform some tasks with DynamoDB.
My question is in Webhook (this is the first time I use it) if I have a single url (API Gateway) associated with API.ai. As I call different Lambda functions when different Intents require it. That is to say:
Intent1 -> API Gateway -> Lambda1
Intent2 -> API Gateway -> Lambda2
I have thought that since it is a single url (API Gateway) which can be associated in API.ai. Should a Lambda function be used as a filter?
Intention1 -> API Gateway -> LambdaFilter -> Lambda1
Intention2 -> API Gateway -> LambdaFilter -> Lambda2
I appreciate any help and advices.

Well, I am struggling with this architecture planning as well.
You will need a single API entry, and that said, it will point to a specific lambda function.
You can activate "Proxy Integration" for your Lambda, and than you will be able to capture all headers sent from API.ai on fulfillment webhook.
Then, using LambdaFilter (which is a Lambda), you will need to code with boto3 to call other lambdas, and this will add more lambda calls and processing time. Another thing to try (I didnt try yet) is StepFuncions, which claims to solve lambda workflow management.
To illustrate how bad is a lambda calling another one in my tests, I did SLACK > APIGW > LAMBDAs > SLACK. A chain of 4 lambda calls (slack-handler, general-handler, general-notifier, slack-notifier) was taking around .6~.9s. And I still was not calling API.ai!!! Compressing it to only 2 lambdas (handler and notifier, with more complex code in it) got me to a .3~.4s scenario.
I did that to avoid coupling my solution to API.ai or Slack.

Related

Slack - Interactive button with authentication

What I'm trying to accomplish?
Send an API (chat.postMessage) from a 3rd party app to a Slack user - Done
The message will have two buttons (approve and decline) which the user can click on - Done
Upon clicking on one of the buttons, Slack should send an API back to the 3rd party app - TBD
Before sending an API, Slack should retrieve an authentication token via API - TBD
Note: I've seen in Slack docs mentioning of /incoming endpoint (also /interactive-message). My 3rd party app has a strict set of available APIs. I'm not able to create new API endpoints. Also, I need the payload to be sent in a specific JSON structure.
I've reviewed Slack documentation but I can't figure it out.
Making notifications actionable
I'm technical but not a developer so there might be nuances which I'm missing.
Is there any section in Slack where I can add custom code to accomplish such integration?
If not, how I can accomplish it?
Edit:
I've configured a Request URL (in slack), when I click on one of the buttons in the message, I can see an API request is being sent to the URL.
I'm able to see the payload as well.
My problem is that I need the information to be sent in a different format and to a different endpoint.
What is the best approach to take for this? Do I need to develop a new service to capture and parse the payload and then generate the API request to my 3rd party app?
Ok, so here is how I've accomplished it.
Upon clicking a button in slack, Slack sends an API with a certain payload to the predefined endpoint which you configure (Request URL).
I've used free tier AWS serverless components:
API Gateway and Lambda
API gateway receives the payload and invokes the Lambda function (I used Python as it seems the easier for a non developer)
My Lambda function is processing the payload and has the logic to interact with my 3rd party app in the required way.

Differences between the lambda function template in AWS Amplify

When creating the lambda function through Amplify CLI, there are 4 function templates provided:
CRUD function for Dynamodb (Integration with API Gateway)
Hellow World
Lambda trigger
Serverless ExpressJS function (Integration with API Gateway)
I am confused about the usage of "Hello World" and "Serverless ExpressJS function". Let's say I want to implement a lambda function that contains custom query to get the result from Dynamodb, which template is suitable, or is the best practice to use?
It all helps you start the project with boilerplate code.
For your use case, you can use CRUD Function for DynamoDB. This will create a DynamoDB table and Integrate it with API Gateway and generate boilerplate code for your chosen programming language. You can change the CRUD operations to more custom - changing the parameter.
API Gateway directs all requests to your Express API, so it's up to your route handling, so you can change them later.
As your NodeJS app grows, you might need to organize your routes and use express.Router to make it modular.
It's a simple boilerplate code that returns a string. You can use this for simple tasks or private API calls.
You can run any lambda function when a certain event happens. For example you can execute a lambda function if you configured a lambda trigger for the DynamoDB event.
It's not integrated with DynamoDB, this option will generate simple Express endpoints like GET/, POST/, PUT/, DELETE/
I'd recommend you to try out all these options and see how it works.

Dialogflow Webhook C# integration

What is the Dialogflow webhook format? I want to capture the data in an MVC controller and then respond to the webhook whilst maintaining context. How do I do this in C# with the Dialogflow v2 client library? I haven't found any examples to follow in this language and am unsure how to capture the data.
You can read more about Dialogflow webhook format here. Regarding the Dialoglow v2 client library, it is specifically stating that you don't need to use it and can answer in JSON instead using JObject.
If you want to use Protobuf to lower the overhead and save some traffic you will have to use utilities from idiomatic cloud libraries described in the previous link.
The "Dialogflow V2 client library" is for people who are writing clients that send requests to Dialogflow. Dialogflow calls this a "Detect Intent Request", since you're sending a text string (or audio stream) to Dialogflow and it will determine and process the Intent.
If you're interested in building a Fulfillment webhook that is called when an Intent is matched, then you want to look at the Dialogflow Fulfillment API, which is different. There is no supported library for C# yet, but you can parse and return the JSON. The JSON for V1 and V2 are slightly different (mostly in the names of the fields used, but also occasionally in what the values should be).
You can see the fields for the JSON as well as several examples of the request and response formats. There was also an article on Medium recently posted by a Google Developer Relations member discussing using C# for fulfillment in Dialogflow.

How to consume Dialogflow small talk APIs in a HTML5 app?

I have a chat bot that is deployed in SAP Cloud Platform which answers using an NLP.
I want to integrate Dialogflow small talk with it using APIs.
I have created a project in Dialogflow and able to use small talk. However, I need APIs to call this small talk in a HTML5 applicaiton.
Is it possible to use small talk using APIs or is it only available with Dialogflow and if APIs are available, How to consume them?
I have tried dialogflow.projects.getAgent in Google API Explorer which gives me response but in postman I am getting following error as I am unable to generate API Key:
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential
Please help.
Thanks
Looks like you have two questions:
How do you properly authenticate a Dialogflow v2 API request
there are two versions of Dialogflow's API: v1 and the v2 beta.
You might consider using the v1 API if you're having trouble using the v2 API as it uses much simpler client and developer access tokens. If you'd like to use v2 though, see Dialogflow's v2 authentication guide.
is there a way to get a Dialogflow agent's response via an API call
there is an API to query a Dialogflow agent and get a response back. In Dialogflow's v2 API its called detectIntent and for v1 its called query

Difference between creating a method that accesses a lambda function in API Gateway and adding an API Gateway trigger to a lambda function

I am currently learning how to expose my lambda function with API Gateway. I followed the instructions documented here and created an API that triggers my test lambda function. Here's the summary of what I did.
First I created a test lambda function with hello world template BUT WITH NO TRIGGER ADDED to it.
Then I go to the API Gateway console, added a resource with GET method specifying the integration type to Lambda function. I entered my test lambda function name there.
Ok, so I tested the solution above in API Gateway console. It's working fine and I just need to deploy that by creating a stage and I am done.
But then I noticed another way of exposing lambda with API Gateway. That is, by going to that lambda function and add an API Gateway trigger to it. Like the following:
It will ask to to enter the API name of the API that I will use. This API somehow relates to the one that I created under API Gateway console.
After creating the trigger, the test lambda function will now have a trigger that looks like this, and an https URL exposed under it.
Then after that, when I go to the API Gateway console I noticed that a new resource is added.
The resource name is the name of my test lambda function and the method is ANY and I don't quite understand the use of this.
By comparing the above with the resource I created earlier. The one above does not have the ARN address of the lambda function while this one has
So, my question is,
what is the difference between creating an API and adding an integration for it with lambda and adding a trigger from lambda with an existing API from API Gateway?
Can the https address exposed under lambda function(After adding trigger from API Gateway) be used directly?
If adding trigger to lambda will work as the same. Then do I still need to create a stage to deploy my api?
what is the difference between creating an API and adding an integration for it with lambda and adding a trigger from lambda with an existing API from API Gateway?
Those are two different ways to create an API Gateway-Lambda integration. There is no difference if you configure them both the same way.
Can the https address exposed under lambda function(After adding trigger from API Gateway) be used directly?
Yes, you can use it directly.
If adding trigger to lambda will work as the same. Then do I still need to create a stage to deploy my api?
Yes. The URL comes from the stage, so you need at least one.

Resources