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?
Question posted in Telegram API
A comprehensive official documentation can be found here.
A comprehensive official documentation can be found here.
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.
According to Official Bot API:
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.
If there are new messages
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 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.
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!
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.
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 the
from
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.