A sentimental AI can analyse your misunderstood text messages.

Francesco
4 min readSep 6, 2017

--

Yes Wall-E, you are emotional enough.

Texting is complicated.

When someone gives you a good news and you reply with “great” maybe you are not exactly transmitting the happiest of the feelings, but you are happy, really.

Lucky us, we live in the age of Artificial Intelligence and writing some code has never been so easy!

Let’s see how to build a Telegram Bot which checks the emotions in your text with a quantitative approach through sentiment analysis.

Ingredients for an emotional Bot

As already said, the weapon of choice is a Telegram Bot for the APIs ease of use, but any other programmable messenger system will work.

What really makes all interesting is the chance to build all this in a serverless architecture: thanks to Webtask.io, which offers an amazing environment, security (powered by Auth0) and serverless endpoints, our server side core will receive the message, send it to the our Text Analysis engine and prettify the answer in order to reply eventually with a nicely formatted response.

Let’s dig into the details.

Creating a new bot

  • Send the /newbot message to @BotFather which will ask few questions to create your first Bot (name, description, etc…).
  • Add eventually a set of commands with /setcommands to differentiate the types of analysis running on your text, they will show up pressing / in the message composition field.
  • Open a chat window with your bot in order to start the underlying service.

The sentimental AI

A lot of companies offer a good text analysis API (IBM Watson, Google, Api.ai), but this time I decided to use Indico for the ease of use, the nice dashboard and mostly for the node module we can easily integrate in our webtask.

Of course the choice of the API, Knowledge Base and the Machine Learning system has a huge impact on the performances of our messages analysis.

A serverless core

After the Bot is up and running we need to create the service to handle the messages in a webtask and webtask.io works flawlessly and the easy way (but in a truly powerful way). Follow the documentation if you want to use a client or alternatively jump into the web editor and past the code below in the text area:

A if/else to run a separate analysis for the command /sentiment and for the command /emotions

At this point you will need two different keys to run the web service:

  1. The Telegram Bot token: to wire the webhook (and send messages in a safe way) back to the @BotFather, run /token and follow the instruction to generate a bot token like 1234:abcd56789.
  2. The Indico API Key: quickly copy the API Key on top of the Dashboard after signing in for a free 10K/monthy calls base plan.

Note: in the example the keys are baked in the code, but it’s possible to add and store keys in secrets through the management panel in the editor, in order to improve security and limit the access to the running process.

Let’s wire it together

We have a running bot, a running webtask which is not getting any message so far (🤦‍) and our AI ready to analyse our emotions and sentiments (👊).

In order to share bidirectionally our messages we need to set a webhook using the link of our webtask as shown in the bottom section of the online editor:

curl -X POST -H "Content-Type: multipart/form-data" -F "url=https://wt-12345-0.run.webtask.io/sentigram-webtask" 'https://api.telegram.org/bot1234:1234567890/setWebhook'

waiting to see something like:

{"ok":true,"result":true,"description":"Webhook was set"}

At this point we can defeat our lack of sensitivity with a nice analysis of our messages checking which one is more appropriate! 🤖

Sentiment and emotional analysis

Robots have gotten steadily more capable, but humans’ expectations that robots should have minds keeps biting robot developers. — David Hanson

The second sounds more positive, but be careful with sarcasm!

Although Text Analysis is getting better and better, sometimes it still lacks of that quid a human being is able to extract from the context. The second of the answers above is great if your girlfriend is pregnant, less if your brother used your credit card to buy a new pair of shoes.

Same thing if you don’t use keywords but express a concept with a saying or in a “non conventional” but “human understandable” way:

Our bot is surprised that someone made my day, but not that it’s the best thing I’ve ever heard in my life.

There’s a lot more to improve the flow after the text analysis that I was not able to cover here, but if you still have questions or want to see more about Telegram Bot and Sentiment Analysis, let me know in the comments below or pull a request on the project repository.

Francesco is a traveler, developer, photographer, cook. Sometimes a serious person.

--

--