Introduction to bot programming

Introduction to bot programming

Kunal Diwan

Many users are interested in programming their own Telegram bot. Sadly there's not much information out there on how to start out creating one. This article should help you get started.

1. How can I create my own Bot ?

To begin with, there is this big question: Are you able to code? To create a "real" Telegram bot you need to write a little program to interact with the Telegram servers. If you can't, there are alternative ways like "Manybot , Menubuiderbot" which requires no coding. These will not be covered in detail in this article.

1.1) Using Botmakers such as "Manybot" (etc.)

These kind of services offer to create a bot on their servers. They provide a skeleton bot, to which you can add minor stuff such as dialogs. Important: You can only do what they (e.g. Manybot) let you do. You are very limited in your actions! If they don't offer a feature you need, you can't use their service. This botmakers show their own functions different as we think . But of course you don't need to care about programming and hosting the bot.

1.2) Coding it yourself

When you do all the coding yourself, you're not as limited: You can do everything that's possible via the Telegram bot API. For example: You could create a bot that's connected to your home automation system, so that you can switch on/off lights in your house via a simple Telegram message. BUT: when coding it yourself you need to think about where you'll host the code (more on that later). If you can't code and aren't going to learn programming any time soon, you should stop reading here and go with a botmaker service (e.g. Manybot) or pay a programmer for creating your bot.

Programmer - @kunaldiwan

Okay: since you're still reading, I assume you are able to code or at least want to try it. Let's dive into detail.

2. How do I start ?

Quick reminder: From here on out, you must know a programming language.

2.1) Hosting

First things first. To avoid frustration later on, you should NOW think about where to host your bot. A bot is a piece of code which interacts with the (Telegram) servers via an API. In order to have a 24/7 online bot, it needs to run on any type of computer for 24/7 (obviously). Telegram does not host your code. You have several ways to go:

Micro-Controller/Raspberry Pi

Having your PC running 24/7

Pay for a VPS from a hosting provider

The advantages and disadvantages are clear: RasPi consumes only a tiny amount of energy compared to a PC and costs ~30$. When paying for a VPS, you have a lot more computing power and don't need to care about power/internet connection outages, but it costs on a monthly basis.

Now that you have decided where to host your bot, let's decide for a programming language.

2.2) Programming Language

If you already know a programming language, you can go ahead and use it. If you don't know any language or want to use another/a better suited one, check the list of languages I can suggest for bot programming.

Python

Go

C#

Java

PHP 

Node.js (JavaScript)

I mostly code my bots in Python since there are quite a few nice libraries and frameworks (more on that later) for it and it's a very easy language: Easy to learn and easy to code. Languages such as C#, Go, and Java are (also) very powerful and offer a wide variety of features. PHP and Node.js are widely used in the web development branch. They are not bad in general, but I personally am not a big fan of them. Check them out if you're curious and go with the one you like the most. There is no right or wrong.

2.3) Getting ready to code

You're now only a few steps away from coding your first own bot. Go ahead and download any IDE for your language (or a simple text editor such as Atom or Notepad++ for coding). For Python I can suggest PyCharm or VS Code. Also you might want to start checking out the basics of the language you chose.

In this post I cannot help you lear a programming language or programming in general. Go ask Google for nice tutorials, there are thousands of them. 

Now that you have set up your IDE and familiarised yourself with the programming language, we can begin. You need to contact @BotFather on Telegram. BotFather is an official Telegram bot for creating new bot accounts. Use the command /newbot and follow its instructions to create a new bot.

After following BotFather's procedure, you'll get a bot token, which allows you to interact with the Telegram servers. You can try it yourself. Insert your bot token into the following link https://api.telegram.org/bot<your_token_here>/getMe, open it in your browser and and it will display information about your bot. Read the Telegram bot api documentation thoroughly and play with the available methods. Try sending and receiving some messages.

4. Let's code

I will not teach you how to code. I just want to give you a short introduction into how to get started.

Now that you have decided about hosting, chosen a language, set up a new bot with BotFather , we can start. I'll use the Python programming language .

4.1 Going on

As we have choosed programming language . So now we have to add codes for our bot. Let's start coding.......

• So first create Github account if you already have then good. Now create a new repository - Github.com/new then Choose name of your repository.

Create new repository


• Then add new file on that repository called as Main.py or if you don't have files you can write code on your file .

Creating new file

• As Main.py file add more files. We will not teach you how to create files by programming. When you successfully started out with the above code, try something more complicated.

4.2 Going professional

When you finally made your bot work the way you desired, you might not want your bot to ask the Telegram servers for updates every X seconds (also known as "long polling"). This is why Telegram offers "webhooks". The Telegram servers will actively start an HTTPS connection to your server when new messages arrive (also known as "push technology"). But this is nothing for beginners. Apart from your VPS you need some kind of webserver which takes care of the requests - and more importantly, you need a TLS certificate. Since the whole process isn't easy, a kind guy named Marvin created a comprehensive guide on how to setup webhooks with Telegram bots.

4.3 Further questions ?

If you still have questions not covered by this guide, come to the Telegram Bot Talk group @DevelopedBotz and ask your questions. It's a group made up of hundreds of bot enthusiasts and developers. In most cases there will be someone able to help you. Feel free to ask any bot-related questions in there.

PS: If you got suggestions regarding this article, ping me @kunaldiwan on Telegram.

4.4 Sample modular bots

https://github.com/kunal-1405/Groupsupporter

https://github.com/DevelopedBots/Brenzo-Lio-Bot

https://github.com/Teameviral/HaritaRobot


➖➖➖➖➖➖➖➖➖➖➖➖➖➖





Report Page