skip to Main Content

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?

7

Answers


  1. I am quite new to Telegram API as well but you may start with accessing this URL in which you should replace (token) with your own token generated buy BotFather:

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

    For example if you want to start handling requests sent to your bot by your PHP script you should call this:

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

    Please not that you MUST have SSL enabled website to start using telegram API.

    Login or Signup to reply.
  2. 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:

    Login or Signup to reply.
  3. 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']);
    
    Login or Signup to reply.
  4. 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!

    Login or Signup to reply.
  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.

    Login or Signup to reply.
  6. As an answer to the script not being able to run for more then 30 seconds:

    use set_time_limit(0); to make it last forever. However be advised that any infinite time loop is somewhat dangerous; side effects like cpu hogs or memory leaks will eat at your server. Thats why many ISPs disallow this setting.

    Login or Signup to reply.
  7. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search