Automate a Cocktail Telegram Bot with n8n Workflow Tool

You are currently viewing Automate a Cocktail Telegram Bot with n8n Workflow Tool
  • Post author:
  • Reading time:8 mins read

Introduction

In the software development field, automation is the key to efficiency and productivity. With the rise of messaging platforms like Telegram, businesses, and developers are leveraging chatbots to streamline various processes. In this blog post, we will explore the power of n8n, a versatile workflow automation tool, and demonstrate how to build a simple Cocktail Telegram bot (CockTailNinjaBot) using n8n. By the end of this post, you’ll have the knowledge to create your very own custom Telegram bot to automate tasks and enhance user experiences.

At its core, n8n operates on a workflow-based approach. Workflows are created by connecting various nodes, each node representing a specific task or operation. These nodes can perform actions such as sending emails, making API calls, updating spreadsheets, interacting with databases, and more. By connecting nodes together, users can define the flow of data and actions with little or no code to automate complex tasks.

Prerequisites

Before diving into the n8n workflow automation tool, you should have a basic understanding of the n8n workflow editor, Telegram, and familiarity with JavaScript. Additionally, ensure you have n8n installed on your local machine or a cloud-based server.

Visit this page for docker installation instructions or sign up for n8n cloud.

Setting Up Telegram Bot API

To create a Telegram bot, you’ll need to interact with the Telegram Bot API. Follow these steps to set up your bot:-

  1. Open the Telegram app and search for the “BotFather” bot.
  2. Start a chat with the BotFather and create a new bot by following the instructions.
  3. Once the bot is created, copy the bot token and use it in the Telegram node credentials in n8n.
Telegram bot token

Building the workflow

Below is an overview of the workflow.

Telegram Bot Workflow

With this workflow, a user is able to receive a specific cocktail recipe on his/her telegram app by sending a cocktail name to the bot or a random recipe by sending the word random.

Explanation

Telegram Trigger Node (1)

The Telegram Trigger is the entry point that listens for incoming telegram messages.

Switch node (2)

Determines the workflow execution path depending on the input. The bot supports only two inputs commands: /start and /random. Every other text goes to the second output and is treated as a cocktail name.

Switch Node

Telegram Node (3)

Responds with a welcome message whenever a /start command is received

HTTP Request Nodes (4, 5)

These HTTP Request Nodes retrieve recipes from the TheCocktailDB API. When the command is /random the Random Recipe Node (4) is executed otherwise Node (5) is executed.

CheckResponse Node (6)

If the cocktail name was invalid or the recipe was not found the API returns an empty array. The IF node determines the execution path depending on the API response state.

FormatResponse Node (8)

This node transforms the API data into a presentable json message. The Code Node allows you to add your JavaScript custom logic into the flow.

n8n Code Node

Telegram Node (8)

After the message has been constructed it’s time to respond to the user. The Telegram Node does that, in our case we post back an image (Note: the Operation is set to Send Photo) and the recipe information as the image caption(See Additional Fields).

This is how it appears on Telegram

Testing and Deploying the Telegram Bot

  1. Save the workflow and click the “Execute Workflow” button to test your Telegram bot.
  2. Follow the prompts and verify that the bot performs as expected.
  3. Once satisfied with the results, deploy the workflow by running n8n on a cloud-based server.

Conclusion

In this post, we explored the capabilities of n8n, a powerful workflow automation tool, and learned how to create a Telegram bot. With further exploration and experimentation, you can unlock even more possibilities with n8n workflow automation.

To learn more about n8n please visit their website. Check out the bot here https://t.me/CockTailNinjaBot

Leave a Reply