Very first manual bot steps πŸ€–

Very first manual bot steps πŸ€–

Michael W.

🧾 What this article is about

So, you want to start coding your bot, and you don't have any clue about this kind of art πŸ–ΌοΈ.
I'll give you a short introduction how to achieve your first success very easily.

I'll show you my bot token for better comprehension. But you don't need to try it, he's already revoked πŸ˜‰

After this short introduction (step 1 and 2), I'll give you a few links (step 3) where you can move on.

Hope this short introduction helps you to get started :)

1) Create bot - BotFather πŸ†•

I recommend to use Telegram Desktop, but a mobile application works too.

Create bot (token) with @botfather

2) Try the bot in browser - PlayAround πŸŽ―πŸƒ

Now let's do two simple tests.

First of all, write a message to the bot:

Write a message to the bot

Now, let's get the message from the API.

2.1) Get message from API πŸ“°

The API documents how to get updates. Read this paragraph! Write this URL in your browser / postman.

https://api.telegram.org/bot<token>/getUpdates

In my case:

https://api.telegram.org/bot1705100941:AAG6lNJXEM9vgHRiCsdi_dFxm2ghWhrzCn4/getUpdates

Make sure to replace the numbers and letters - called bot token. Let "bot" where it is, and change afterwards, compare it with the bottoken from step 1.

It should look like this:

Test bot in browser
Hint: to be able to easier read the message, copy the whole content and let it format here: Best JSON Pretty Print Online (jsonformatter.org)

2.2) Send message via API πŸ“

Now, a bot that can only read messages is almost non-sense (in most cases).

So, use the sendMessage function:

https://api.telegram.org/bot<token>/sendMessage?chat_id=<user_id>&text=<text>

In my case:

https://api.telegram.org/bot1705100941:AAG6lNJXEM9vgHRiCsdi_dFxm2ghWhrzCn4/sendMessage?chat_id=XYZXYZXYZ&text=I'm doing well

For the chat_id parameter, you have to use the from.id (marked green in 2.1), to respond to the user which has sent message.

Back in Telegram Desktop:

Response from the bot

2.3) Buttons πŸ†—

Now, to understand the whole API, you need to understand the concept of passing a json to the API. I'll demonstrate it using inline link buttons. We create a link to google.com.

We use the sendMessage function once more, but with the parameter reply_markup:

https://api.telegram.org/bot<token>/sendMessage?chat_id=<user_id>&text=<text_in_message>&reply_markup={"inline_keyboard":[[{"text":"<text_on_button>","url":"<url_to_open>"}]]}

In my case:

https://api.telegram.org/bot1705100941:AAG6lNJXEM9vgHRiCsdi_dFxm2ghWhrzCn4/sendMessage?chat_id=XYZXYZXYZ&text=Open google using button below&reply_markup={"inline_keyboard":[[{"text":"hi google","url":"https://google.com"}]]}

Back in telegram desktop:

Inline Button in Telegram Desktop

To easier write the json use this site:

Write in the left, click on compress, copy from the right

Best JSON Minifier, JSON Minify and JSON Compressor (codebeautify.org)

Now you could simply add more buttons, just use JSON syntax.

2.4) Going further ⏩

With those tools and experiences, you should be able to read and understand the API docs. Just analyze the used functions and compare them to those you want to use.

❗❗ Telegram Bot API πŸ“ƒ


--


3) Create a real bot πŸ›«

Now, after your first success, you know the very basics of creating bots.

You should start now to learn a language, choose and use a framework and then, finally, you are ready to start to work on your bot.

3.1) Get an overview πŸ—ΊοΈ

Please read this article, which desribes various and very important stuff regarding bot developing:
Introduction to bot programming – Telegraph

3.2) Learn code ⌨️

I'd recommend to use Python for your bots. Use this tutorial and take time (very important!!) to complete it.
Learn Python Programming (programiz.com)

πŸ’‘ If something doesn't work for at least an hour of trying, you can ask someone for help. But first try it yourself, it doesn't only save time of others, but it also helps you to understand more about programming.

3.3) Use a library πŸͺ›

Use this library, it simplifies many things you wouldn't even imagine they exist (for example rate limit handling).

I'll almost bet you'll not regret it (at least when you try first without it πŸ˜‰). Below is a simple tutorial, that explains the most used library for python telegram bots. Make sure you completed the linked tutorial in 3.2.

Extensions – Your first Bot Β· python-telegram-bot/python-telegram-bot Wiki (github.com)

3.4) What if "problems" 🀯

  • Take time and try again πŸ•°οΈ
  • Reread the documentation πŸ“ƒ
  • Make sure you read the linked article in 3.1 πŸ“°

If not yet working:

  • Ask for help in Bot Talk πŸ’¬ (if the problem is related to the API)
  • Ask somewhere else (stackoverflow, github issues, ...) ❓




Report Page