7

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?

JAAD
  • 12,349
  • 7
  • 36
  • 57
Majid Hojati
  • 1,740
  • 4
  • 29
  • 61
  • Why don't you user @TbotifyBot or http://tbotify.com bot creation service? It has a build in feedreader ready to user – Ghasrfakhri Aug 09 '15 at 16:25

7 Answers7

11

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']);
Chris Brand
  • 1,962
  • 16
  • 9
4

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:

Fwolf
  • 671
  • 4
  • 8
  • 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 – Majid Hojati Jun 27 '15 at 13:45
  • @MajidHojati The API repositories on github have already contains some example code, and they are still growing. – Fwolf Jun 29 '15 at 02:00
  • thank you so much for your help – Majid Hojati Jul 01 '15 at 12:30
3

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!

JTE
  • 166
  • 5
3

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.

Nick Lee
  • 5,639
  • 3
  • 27
  • 35
1

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.

0

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.

Vahid Mafi
  • 111
  • 6
  • 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 Jun 27 '15 at 10:18
0

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.

HappyMe
  • 95
  • 7