DialogFlow (API.AI) Golang SDK

DialogFlow (formerly API.AI) gives users new ways to interact with your bot by building engaging voice and text-based conversational interfaces powered by AI.

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"

This library allows integrating agents from the DialogFlow natural language processing service with your Golang application.

Issue the following command to install the library:

The example below, display list of entities:

Note: for more details about the available methods, check the project Github repository.

For a real world example on how to use this library, check my previous tutorial, on how to create a Messenger Bot in Golang to show list of movies playing in cinema, and tv shows airing on TV:

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.

Cleanup old Docker images from Nexus Repository

Many of us, are using Nexus as a repository to publish Docker Images. Typically we build images tagged with the commit hash (or using semver ideally) after SCM change automatically in CI and we push them to registry. As result there are many “unneeded” & “old” images that in our case take significant amount of disk space.

I looked around the graphical interface of Nexus and there’s apparently nothing to remove several Docker images at the same time. Or even, a scheduled task  to clean up old hosted Docker images, and to also clean up layers which are no longer used by any hosted images.

So I have come up with a simple bash script which uses Docker Registry API to purge Docker images and keep the last X images and delete all other. But, is there a better solution ? YES ! I built a Nexus CLI

To install Nexus CLI, find the appropriate package for your system and download it. For linux:

After downloading Nexus CLI. Add the execution permission to the binary:

Note: For Windows make sure that nexus-cli binary is available on the PATHThis page contains instructions for setting the PATH on Windows.

After installing, verify the installation worked, by opening a new terminal session and checking if nexus-cli is available :

Once done, configure the Nexus credentials:

Through nexus-cli configure, the Nexus CLI will prompt you for four pieces of information. The Username and Password are your account credentials. Nexus Hostname & Docker repository name.

That should be it. Try out the following command from your cmd prompt and, if you have any images, you should see them listed

Display image tags:

Image description:

To remove a specific image:

To keep only the last X images and delete all other:

That’s it ! Let’s go back to Nexus Dashboard:

As you can see, Nexus kept only the last 4 images and deleted the others.

Résultat de recherche d'images pour "awesome meme"

The CLI is still in its early stages, so you are welcome to contribute to the project in Github.

Docker Swarm Networking and Dynamic Reverse Proxy

This post will show you how to setup a Swarm Cluster, deploy a couple of microservices, and create a Reverse Proxy Service (with Traefik) in charge of routing requests on their base URLs.

Résultat de recherche d'images pour "microservices memes"

If you haven’t already, create a Swarm cluster, you could use the shell script below to setup a cluster with 3 nodes (1 Manager & 2 Workers)

Issue the following command to execute the script:

The output of the above command is as follows:

At this moment, we have 3 nodes:

Our example microservice application consists of two parts. The Books API and the Movies API. For both parts I have prepared images for you that can be pulled from the DockerHub.

The Dockerfiles for both images can be found on my Github.

Create docker-compose.yml file with the following content:

  • We use an overlay network named traefik-net, on which we add the services we want to expose to Traefik.
  • We use constraints to deploy the APIs on workers & Traefik on Swarm manager.
  • Traefik container is configured to listen on port 80 for the standard HTTP traffic, but also exposes port 8080 for a web dashboard.
  • The use of docker socket (/var/run/docker.sock) allows Traefik to listen to Docker Daemon events, and reconfigure itself when containers are started/stopped.
  • The label traefik.frontend.rule is used by Træfik to determine which container to use for which Request Path.
  • The configs part create a configuration file for Traefik from config.toml (it enables the Docker backend)

In order to deploy our stack, we should execute the following command:

Let’s check the overlay network:

Traefik configuration:

To display the configuration content:

And finally, to list all the services:

In the list of above, you can see that the 3 containers are being running on node-1, node-2 & node-3 :

If you point your favorite browser (not you IE 😂) to the Traefik Dashboard URL (http://MANAGER_NODE_IP:8080) you should see that the frontends and backends are well defined:

If you check http://MANAGER_NODE_IP/books, you will get a list of books

If you replace the base URL with /movies:

What happens if we want to scale out the books & movies APIs. With the docker service scale command:

We can confirm that:

Obviously Traefik did recognise that we started more containers and made them available to the right frontend automatically:

In the diagram below, you will find that the manager has decied to schedule the new containers on node-2 (3 of them) and node-3 (4 of them) using the Round Robin strategy

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 😍