Calling Telegram API to create a feedreader bot

16,121

Solution 1

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

It features all functions of a new api and is an easy to use and event based libarry!

Have fun!

Solution 2

You can use this basic example to get you going. I would suggest adding a bit more polish using like 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']);

Solution 3

According to Official Bot 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 different way by receive schema

Use getUpdates

The accessing of bot API is through HTTP GET/POST, detail in official help.

  • Use an endless loop to read messages from telegram, with HTTP GET/POST
  • If there are new messages

    • Parse message
    • Send message with HTTP GET/POST
    • Sleep some seconds

Use WebHook

When using WebHook(and well configured), new message to your bot will trigger a HTTP POST request from telegram server to your configured url, on your own server, parsed by your PHP script.

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


So, the difference only exists when getting messages from telegram, all response send to telegram is via HTTP GET/POST, detail in Making requests part in official API.

Some people have mad some unofficial PHP api on github:

Solution 4

I suggest beginners to start this way:

  1. Search for BotFather on your Telegram app

  2. Send him a /newbot command. Follow his instructions.

  3. He will give you a token, something like 123456789:ABCDefGHIJKLmnopQRstUVwXYz

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

  5. Search for your bot on Telegram app. Send it a message.

  6. On the browser window, enter: https://api.telegram.org/bot<token>/getUpdates
    Remember to substitute the token. You should see the message you just sent. Note the from and chat field. That is You.

  7. Then, you can try out some libraries. To give some language balance here, I suggest telepot, a Python framework I have created. The project page has a lot of documentations and examples.

Finally, even with the help of libraries, I encourage you to read the underlying Bot API documentations. Understanding it helps you exploit its full power.

Good luck.

Solution 5

about getUpdates API and endless loop, the php server can't let execute the code over 30 sec. , so endless loop doesn't work correctly.

Share:
16,121
Majid Hojati
Author by

Majid Hojati

Updated on June 17, 2022

Comments

  • Majid Hojati
    Majid Hojati almost 2 years

    I have seen New API for bots are enabled to create custome bots,I have seen some sources such as this and this I have also read about @fatherbot which is about registering bots,I also searched about some examples about telegram bots such as this one,I know how write codes in php and python but can not find out how to call api methods and where to get start.Does any one has any idea how to get start?

    • Ghasrfakhri
      Ghasrfakhri over 8 years
      Why don't you user @TbotifyBot or tbotify.com bot creation service? It has a build in feedreader ready to user
  • Majid Hojati
    Majid Hojati almost 9 years
    Thank you so much dear Vahid,Is there any example of php files for me tho learn more?I mean I only can Call methods in adresses and then post them to my php file?How return the result?
  • Majid Hojati
    Majid Hojati almost 9 years
    Thank you so much for your answer,It seems that I can lean WebHook method because I have already worked with HTTP POST/GET but Is there some example of its real work?Thank you so much
  • Fwolf
    Fwolf almost 9 years
    @MajidHojati The API repositories on github have already contains some example code, and they are still growing.
  • Majid Hojati
    Majid Hojati almost 9 years
    thank you so much for your help
  • Majid Hojati
    Majid Hojati almost 9 years
    Wowww...Thank you for great eample..thank you
  • Majid Hojati
    Majid Hojati almost 9 years
    humm,Thank you ,,It seems that it is a great point for me to start