Messenger Bot with DialogFlow & Golang

This post is part of “ChatOps” series. In this part, I will show you how to create a Messenger Bot in Golang with DialogFlow (formerly API.AI) to show list of movies playing today in cinema.

Note: all the code used in this demo can be found on my Github.

Start with an HTTP server exposing 2 endpoints:

1 – GET /webhook

Handles Facebook challenge verification. It simply looks for the Verify Token and responds with the challenge sent in the verification request.

2 – POST /webhook

Handles messages coming from Messenger:

It calls the ProcessMessage method which uses Facebook Graph API to send a GIF image to the user:

Note: for more in depth details check my tutorial Build a Facebook Messenger bot with Go and Messenger API

Create a Facebook page. It will be “identity ” of your bot:

Then create a Facebook application. It will be the middleware that connects the server and your public page.

Click Add Product from the left menu, then choose Messenger:

At the Token Generation, choose the page you just created from the dropdown menu, and it will generate a token:

Once you’ve gotten your PAGE_ACCESS_TOKEN and VERIFY_TOKEN, make sure you add those two as environment variables for the server:

In new terminal session, issue the following command to start the HTTP server:

In order to make our server publically accessible, I will use a tool called ngrok. It basically creates a secure tunnel on your local machine along with a public URL you can use for browsing your local server.

Note: Keep in mind, to use your bot in production, you need to use a :

  • IaaS like AWSGCP, Azure …
  • PaaS like Heroku, Clever Cloud …

Then, at the Webhooks section, click the Setup Webhooks button:

After you’ve configured your webhook, you will need to subscribe to the page you created earlier:

Go to the Facebook Page you created and and click on “Message” button, next to the “Like” button near the top of the page. Start sending your Page messages and the bot should reply with a GIF !

By default, the bot should respond to everything with a GIF image.

Now lets make it smarter, for that we will use an NLP (Natural Language Processing) backend like DialogFlow (formerly API.AI):

So after signing up to Dialogflow, create a new Agent:

Give it a name and fill out the required fields:

Once created, lets use Small Talk feature of DialogFlow to give our bot the ability to have simple conversations:

Enable the Small Talk checkbox. With this feature enabled we imported a lot of predefined answers for simple questions and phrases. You can easily change the responses to the questions if you don’t like them:

To test it out, you can use the Console at the right hand side:

Now lets use this feature in out bot. DialogFlow offers many SDKs in different programming languages:

But unfortunately, there’s no SDK for Golang

But dont be sad, I made an SDK to integrate DialogFlow with Golang:

Résultat de recherche d'images pour "thank you sir meme"

So, install DialogFlow Golang library:

Go back to DialogFlow dashboard and copy the Client Access Token:

Set it as environment variable:

Create a new function that takes the message sent from a user via Messenger as an argument, and pass it to DialogFlow Client to get the appropriate response:

Go to the Facebook Page and click on Message to start chatting :

But that’s not enough, lets take this further and make our bot tell us about the movies playing today in cinema, and series airing today on TV.

Create an entity, to store the type of the show (movie or series) the user is asking about:

Then, create a new intent, which represents a mapping between what a user says and what action should be taken:

Create some more questions:

Finally, update the ProcessMessage method to respond with a list of shows if the intent name is shows. The method is using the moviedb library to get the list of shows.

Let’s test the bot from Messenger:

Wow !! you have created your first chatbot in Golang with DialogFlow ! It was easy, wasn’t it ?

Image associée

In the upcoming tutorial, I will show you how to create a Serverless Messenger Bot with Lambda & API Gateway.

Build a Serverless Memes Function with OpenFaaS

In this quick post, I will show you how to build a Serverless function in Go to get the latest 9Gag Memes using OpenFaaS.

This tutorial assume that you have:

  • faas-cli installed – The easiest way to install the faas-cli is through cURL:

  • Swarm or Kubernetes environment configured – See Docs.

1 – Create a function

Create a “handler.go” file with the following content:

The code is self-explanatory, it uses 9Gag Web Crawler to parse the website and fetch memes by their tag.

2 – Docker Image

I wrote a simple Dockerfile using the Multi-stage builds technique to reduce the image size down:

3 – Configuration file

Note: If pushing to a remote registry change the name from mlabouardy to your own Hub account.

4 – Build

Issue the following command:

5 – Deploy

6 – Tests

Once deployed, you can invoke the function via:

cURL:

FaaS CLI:

UI:

Note: all code used in this demo, is available on my Github 😍

Youtube to MP3 using S3, Lambda & Elastic Transcoder

In this tutorial, I will show you how to convert a Youtube video 📺 to a mp3 file 💿 using AWS Elastic Transcoder. How can we do that ?

We will create a Lambda function to consume events published by S3. For any video uploaded to a bucket, S3 will invoke our Lambda function by passing event information. AWS Lambda executes the function. As the function executes, it reads the S3 event data, logs some of the event information to Amazon CloudWatch. Then, kick off a transcoding job.

Let’s start, by creating an S3 bucket to store the inputs files (videos) and the outputs files (audio) :

Next, let’s define a Transcoder pipeline. A pipeline essentially defines a queue for future transcoding jobs. To create a pipeline, we need to specify the input bucket (where the videos will be).

Note: Copy down the Pipeline ID, we will need later on

Having created a pipeline, go to the AWS Management Console, navigate to Lambda service & click on “Create a Lambda Function“, add S3 as the event source for Lambda function:

I used the following Node.JS code:

The script does the following:

  • Extract the filename of the uploaded file from the event object
  • Create a Transcoder job and specify the required outputs
  • Launch the job

Note: you might notice in the function above is the use of presets (1351620000001-300040). It describes how to encode the given file (in this case mp3). The full list of available presets can be found in AWS Documentation.

Finally, set the pipeline id as an envrionment variable and select an IAM role with permission to access Elastic Transcoder:

Once created, upload a video file to the inputs bucket:

If everything went well, you should see the file in your outputs bucket:

S3 will trigger our Lambda function. It will then execute our function. and log the S3 object name to CloudWatch Logs:

After couple of seconds (or minutes depends on the size of the video ) , you should see a new MP3 file has been generated by Elastic Transcoder job inside the outputs directory in the S3 bucket:

Create Front-End for Serverless RESTful API

In this post, we will build an UI for our Serverless REST API we built in the previous tutorial, so make sure to read it before following this part.

Note: make sure to enable CORS for the endpoint. In the API Gateway Console under Actions and Enable CORS:

The first step is to clone the project:

Head into the ui folder, and modify the js/app.js with your own API Gateway Invoke URL:

Once done, you are ready to create a new S3 bucket:

Copy all the files in the ui directory into the bucket:

Finally, turns website hosting on for your bucket:

After running this command all of our static files should appear in our S3 bucket:

Your bucket is configured for static website hosting, and you now have an S3 website url like this http://<bucket_name>.s3-website-us-east-1.amazonaws.com

Create a Serverless REST API with Node.JS, AWS Lambda, DynamoDB & API Gateway

In this post, I will show you how to build a RESTful API in Node.JS following the Serverless approach using AWS Lambda, API Gateway & DynamoDB.

Serverless computing is a cloud computing execution model in which the cloud provider dynamically manages the allocation of machine resources

All the code is available on my Github 😎

1 – API Specification

The REST API service will expose endpoints to manage a store of movies. The operations that our endpoints will allow are:

2 – DynamoDB Table

Go to DynamoDB Console, then click on “Create table” button, and fill the table name and set a primary key:

3 – Write a new Movie Lambda Function

The code is self explanatory, I have used the put method to insert a new movie item and uuid library to generate a random unique id for the movie item:

Go to the Lambda Dashboard and create a new function as below:

Add the DynamoDB table name as an environment variable and update the handler name to the function name in the code above:

Note: you should use a IAM Role with permission to access DynamoDB & Lambda.

Once created, you can click on “Test” and send a JSON as below:

The item has been successfuly inserted into DynamoDB:

4 – List all Movies Lambda Function

The code is self explanatory, I used scan method to fetch all items from the table:

Fill the function configuration as below:

Similar to the write function, we need to add DynamoDB table name as an environment variable:

Once created, you could test the function by clicking on the “Test” button:

Congratulation ! 😁 We have successfuly created our Lambda functions:

5 – Setup API Gateway

Go to API Gateway Console and create a new API.

5.1 – GET /movies

5.2 – POST /movies

Once done, deploy the API:

Copy down the API Invoke URL:

6 – Test

Let’s test it out:

6.1 – Create a Movie

With Postman:

With cURL:

6.2 – List Movies

With Postman:

With cURL:

If we check our DynamoDB Table 😇