Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Telegram API to create a feedreader bot [closed]

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?

like image 669
Majid Hojati Avatar asked Jun 25 '15 08:06

Majid Hojati


People also ask

Is it difficult to create a telegram bot?

You'll see a list of commands that help you create, edit, and manage your bots. Since it's your first time, you'll want /newbot . Compared to the process for building a Twitter bot, a Mastodon bot, or any other kind of bot I've experimented with, Telegram makes the initial setup super easy.

Does Telegram have an open API?

Telegram API This API allows you to build your own customized Telegram clients. It is 100% open for all developers who wish to create Telegram applications on our platform.

Can I create a private telegram bot?

Not exactly a private bot With Telegram bots are not private. Everybody can find them. The difference is that a certain communication channel with the bot can be made private. This is a group with the bot that you and the bot are members of.


2 Answers

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']);
like image 160
Chris Brand Avatar answered Oct 17 '22 17:10

Chris Brand


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:

  • https://github.com/tgbot/api
  • https://github.com/Ardakilic/Telegram-bot-php
  • https://github.com/zelenin/telegram-bot-api
like image 4
Fwolf Avatar answered Oct 17 '22 16:10

Fwolf