Call Telegram API to create a bot

I saw that the new bot API is included for creating custome bots, I saw some sources like this and this I also read about @fatherbot , which is about registering bots, I also looked for some examples of telegram bots such as this one, I I know how to write codes in php and python , but I can’t find out how to call api methods and where to start. Anyone have an idea how to get started?

+7
api php telegram telegram-bot
source share
7 answers

You can just use my new library for telegram api bot! https://github.com/tekook/TelegramLibrary

It contains all the features of the new api and is easy to use and event-based libarry!

Good luck

+3
source share

You can use this basic example to get started. I would suggest adding a little more benefit using curl and adding some error handling.

 <?php $bot_id = "<bot ID generated by BotFather>"; # Note: you want to change the offset based on the last update_id you received $url = 'https://api.telegram.org/bot' . $bot_id . '/getUpdates?offset=0'; $result = file_get_contents($url); $result = json_decode($result, true); foreach ($result['result'] as $message) { var_dump($message); } # You can send a message like this: # The chat_id variable will be provided in the getUpdates result # TODO: urlencode your message $url = 'https://api.telegram.org/bot' . $bot_id . '/sendMessage?text=message&chat_id=0'; $result = file_get_contents($url); $result = json_decode($result, true); var_dump($result['result']); 
+10
source share

According to the Official Bots API :

 Getting updates There are two mutually exclusive ways of receiving updates for your bot β€” the getUpdates method on one hand and Webhooks on the other. 

So PHP bot script works differently, getting a schema

Use getUpdates

Access to the bot API is via HTTP GET / POST, in detail in the official help.

  • Use an infinite loop to read messages from a telegram using HTTP GET / POST
  • If there are new posts

    • Message parsing
    • Send HTTP GET / POST message
    • Sleep a few seconds

Use webhook

When using WebHook (and a well-tuned one), a new message to your bot will trigger an HTTP POST request from the telegram server to your configured URL on your own server, analyzed by your PHP script.

In your PHP script, parse the new message from HTTP POST and send the message from HTTP POST to the telegram server.


Thus, the difference only exists when receiving messages from a telegram, all responses are sent to the telegram via HTTP GET / POST, in detail in the Request Execution section in the official API.

Some people are crazy about the unofficial PHP api on github:

+4
source share

I suggest that beginners begin this journey:

  • Find BotFather in the Telegram App

  • Send him the / newbot command . Follow his instructions.

  • It will give you a token, something like 123456789:ABCDefGHIJKLmnopQRstUVwXYz

  • Open a browser window, enter in the address bar something from this form: https://api.telegram.org/bot<token>/getMe
    For example, using a fake token from above: https://api.telegram.org/bot123456789:ABCDefGHIJKLmnopQRstUVwXYz/getMe
    It should return your bot information in JSON format. This shows that accessing the Bot API is nothing more than making HTTP requests.

  • Find your bot in the Telegram app. Send him a message.

  • In the browser window, enter: https://api.telegram.org/bot<token>/getUpdates
    Remember to replace the token. You should see the message you just sent. Pay attention to the from and chat fields. It's you.

  • Then you can try some libraries. To give some language balance here, I suggest telepot , the Python framework I created. The project page contains many documents and examples.

Finally, even with the help of libraries, I recommend that you familiarize yourself with the basic Bot API documents. Understanding this helps you use all your power.

Good luck.

+3
source share

about the getUpdates API and the infinite loop, the php server cannot execute the code for more than 30 seconds., so the infinite loop does not work correctly.

+1
source share

I am also new to the Telegram API, but you can start by accessing this URL where you must replace the (token) with your own token generated by the BotFather buyer:

https://api.telegram.org/bot(token)/METHOD_NAME

For example, if you want to start processing requests sent to your bot using a PHP script, you should call this:

https://api.telegram.org/bot(token)/setWebhook?url=https://yourdomain.com/path_to_your_script/

Please do not have MUST have an SSL enabled website to start using the telegram APIs.

0
source share

As an answer to a script that cannot work for more than 30 seconds:

use set_time_limit (0); so that it lasts forever. However, keep in mind that any endless loop of time is somewhat dangerous; side effects like cpu hogs or memory leaks will feed on your server. This is why many ISPs prohibit this setting.

0
source share

All Articles