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?
Calling Telegram API to create a feedreader bot
16.4k views Asked by Majid Hojati AtThere are 7 answers
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.
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']);
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.
I suggest beginners to start this way:
Search for BotFather on your Telegram app
Send him a /newbot command. Follow his instructions.
He will give you a token, something like
123456789:ABCDefGHIJKLmnopQRstUVwXYz
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.Search for your bot on Telegram app. Send it a message.
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 thefrom
andchat
field. That is You.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.
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:
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!