All Projects → Eleirbag89 → Telegrambotphp

Eleirbag89 / Telegrambotphp

Licence: mit
A very simple PHP Telegram Bot API for sending messages.

Projects that are alternatives of or similar to Telegrambotphp

Tbot
Go library for Telegram Bot API
Stars: ✭ 301 (-42.56%)
Mutual labels:  bot, telegram
Pytg
Python package that wraps around Telegram messenger CLI. Send and receive messages, and more.
Stars: ✭ 365 (-30.34%)
Mutual labels:  bot, telegram
Tgbot
Modular telegram group management bot
Stars: ✭ 334 (-36.26%)
Mutual labels:  bot, telegram
Alertmanager Bot
Bot for Prometheus' Alertmanager
Stars: ✭ 473 (-9.73%)
Mutual labels:  bot, telegram
Awesome Bots
Awesome Links about bots.
Stars: ✭ 412 (-21.37%)
Mutual labels:  bot, telegram
Tg Keyword Reply Bot
[DEPRECATED] Telegram关键词自动回复机器人: 根据群组管理员设定的关键词或者正则规则,自动回复文字、图片、文件或者进行永久禁言、临时禁言、踢出等群管操作
Stars: ✭ 299 (-42.94%)
Mutual labels:  bot, telegram
Bottender
⚡️ A framework for building conversational user interfaces.
Stars: ✭ 3,803 (+625.76%)
Mutual labels:  bot, telegram
Telegram Bot Swift
Telegram Bot SDK for Swift (unofficial)
Stars: ✭ 275 (-47.52%)
Mutual labels:  bot, telegram
Groupbutler
This bot can help you in managing your group with rules, anti-flood, description, custom triggers, and much more!
Stars: ✭ 399 (-23.85%)
Mutual labels:  bot, telegram
Aiotg
Asynchronous Python library for building Telegram bots
Stars: ✭ 374 (-28.63%)
Mutual labels:  bot, telegram
Telegraf
Modern Telegram Bot Framework for Node.js
Stars: ✭ 5,178 (+888.17%)
Mutual labels:  bot, telegram
Tgbot Cpp
C++ library for Telegram bot API
Stars: ✭ 439 (-16.22%)
Mutual labels:  bot, telegram
Telegram.bot.examples
Examples for the Telegram.Bot C# Library
Stars: ✭ 290 (-44.66%)
Mutual labels:  bot, telegram
The Guard Bot
The Guard, a Telegram bot to moderate groups.
Stars: ✭ 299 (-42.94%)
Mutual labels:  bot, telegram
Telegram List
List of telegram groups, channels & bots // Список интересных групп, каналов и ботов телеграма // Список чатов для программистов
Stars: ✭ 3,362 (+541.6%)
Mutual labels:  bot, telegram
Shieldy
@shieldy_bot Telegram bot repository
Stars: ✭ 351 (-33.02%)
Mutual labels:  bot, telegram
Telegram channel downloader
一个电报群组、频道下载脚本,支持上传到GD、OD等rclone可以挂载的网盘。
Stars: ✭ 216 (-58.78%)
Mutual labels:  bot, telegram
Swiftybot
How to create a Telegram, Facebook Messenger, and Google Assistant bot with Swift using Vapor on Ubuntu / macOS.
Stars: ✭ 247 (-52.86%)
Mutual labels:  bot, telegram
Pokemongo Bot
The Pokemon Go Bot, baking with community.
Stars: ✭ 3,730 (+611.83%)
Mutual labels:  bot, telegram
Telegram Bot
Ruby gem for building Telegram Bot with optional Rails integration
Stars: ✭ 433 (-17.37%)
Mutual labels:  bot, telegram

TelegramBotPHP

API PHP CURL

Total Downloads License StyleCI

A very simple PHP Telegram Bot API.
Compliant with the November 17, 2017 Telegram Bot API update.

Requirements

  • PHP >= 5.3
  • Curl extension for PHP5 must be enabled.
  • Telegram API key, you can get one simply with @BotFather with simple commands right after creating your bot.

For the WebHook:

  • An VALID SSL certificate (Telegram API requires this). You can use Cloudflare's Free Flexible SSL which crypts the web traffic from end user to their proxies if you're using CloudFlare DNS.
    Since the August 29 update you can use a self-signed ssl certificate.

For the getUpdates(Long Polling):

  • Some way to execute the script in order to serve messages (for example cronjob)

Download

Using Composer

From your project directory, run:

composer require eleirbag89/telegrambotphp

or

php composer.phar require eleirbag89/telegrambotphp

Note: If you don't have Composer you can download it HERE.

Using release archives

https://github.com/Eleirbag89/TelegramBotPHP/releases

Using Git

From a project directory, run:

git clone https://github.com/Eleirbag89/TelegramBotPHP.git

Installation

Via Composer's autoloader

After downloading by using Composer, you can include Composer's autoloader:

include (__DIR__ . '/vendor/autoload.php');

$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');

Via TelegramBotPHP class

Copy Telegram.php into your server and include it in your new bot script:

include 'Telegram.php';

$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');

Note: To enable error log file, also copy TelegramErrorLogger.php in the same directory of Telegram.php file.

Configuration (WebHook)

Navigate to https://api.telegram.org/bot(BOT_TOKEN)/setWebhook?url=https://yoursite.com/your_update.php Or use the Telegram class setWebhook method.

Examples

$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');

$chat_id = $telegram->ChatID();
$content = array('chat_id' => $chat_id, 'text' => 'Test');
$telegram->sendMessage($content);

If you want to get some specific parameter from the Telegram response:

$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');

$result = $telegram->getData();
$text = $result['message'] ['text'];
$chat_id = $result['message'] ['chat']['id'];
$content = array('chat_id' => $chat_id, 'text' => 'Test');
$telegram->sendMessage($content);

To upload a Photo or some other files, you need to load it with CurlFile:

// Load a local file to upload. If is already on Telegram's Servers just pass the resource id
$img = curl_file_create('test.png','image/png'); 
$content = array('chat_id' => $chat_id, 'photo' => $img );
$telegram->sendPhoto($content);

To download a file on the Telegram's servers

$file = $telegram->getFile($file_id);
$telegram->downloadFile($file['result']['file_path'], './my_downloaded_file_on_local_server.png');

See update.php or update cowsay.php for the complete example. If you wanna see the CowSay Bot in action add it.

If you want to use getUpdates instead of the WebHook you need to call the the serveUpdate function inside a for cycle.

$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');

$req = $telegram->getUpdates();

for ($i = 0; $i < $telegram-> UpdateCount(); $i++) {
	// You NEED to call serveUpdate before accessing the values of message in Telegram Class
	$telegram->serveUpdate($i);
	$text = $telegram->Text();
	$chat_id = $telegram->ChatID();

	if ($text == '/start') {
		$reply = 'Working';
		$content = array('chat_id' => $chat_id, 'text' => $reply);
		$telegram->sendMessage($content);
	}
	// DO OTHER STUFF
}

See getUpdates.php for the complete example.

Functions

For a complete and up-to-date functions documentation check http://eleirbag89.github.io/TelegramBotPHP/

Build keyboards

Telegram's bots can have two different kind of keyboards: Inline and Reply.
The InlineKeyboard is linked to a particular message, while the ReplyKeyboard is linked to the whole chat.
They are both an array of array of buttons, which rapresent the rows and columns.
For instance you can arrange a ReplyKeyboard like this: ReplyKeabordExample using this code:

$option = array( 
    //First row
    array($telegram->buildKeyboardButton("Button 1"), $telegram->buildKeyboardButton("Button 2")), 
    //Second row 
    array($telegram->buildKeyboardButton("Button 3"), $telegram->buildKeyboardButton("Button 4"), $telegram->buildKeyboardButton("Button 5")), 
    //Third row
    array($telegram->buildKeyboardButton("Button 6")) );
$keyb = $telegram->buildKeyBoard($option, $onetime=false);
$content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "This is a Keyboard Test");
$telegram->sendMessage($content);

When a user click on the button, the button text is send back to the bot.
For an InlineKeyboard it's pretty much the same (but you need to provide a valid URL or a Callback data) InlineKeabordExample

$option = array( 
    //First row
    array($telegram->buildInlineKeyBoardButton("Button 1", $url="http://link1.com"), $telegram->buildInlineKeyBoardButton("Button 2", $url="http://link2.com")), 
    //Second row 
    array($telegram->buildInlineKeyBoardButton("Button 3", $url="http://link3.com"), $telegram->buildInlineKeyBoardButton("Button 4", $url="http://link4.com"), $telegram->buildInlineKeyBoardButton("Button 5", $url="http://link5.com")), 
    //Third row
    array($telegram->buildInlineKeyBoardButton("Button 6", $url="http://link6.com")) );
$keyb = $telegram->buildInlineKeyBoard($option);
$content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "This is a Keyboard Test");
$telegram->sendMessage($content);

This is the list of all the helper functions to make keyboards easily:

buildKeyBoard(array $options, $onetime=true, $resize=true, $selective=true)

Send a custom keyboard. $option is an array of array KeyboardButton.
Check ReplyKeyBoardMarkUp for more info.

buildInlineKeyBoard(array $inline_keyboard)

Send a custom keyboard. $inline_keyboard is an array of array InlineKeyboardButton.
Check InlineKeyboardMarkup for more info.

buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query)

Create an InlineKeyboardButton.
Check InlineKeyBoardButton for more info.

buildKeyBoardButton($text, $url, $request_contact, $request_location)

Create a KeyboardButton.
Check KeyBoardButton for more info.

buildKeyBoardHide($selective=true)

Hide a custom keyboard.
Check ReplyKeyBoarHide for more info.

buildForceReply($selective=true)

Show a Reply interface to the user.
Check ForceReply for more info.

List of Bots using the library

Let me know using this Issue if you have made a bot using this API, I will add it to this section.

Emoticons

For a list of emoticons to use in your bot messages, please refer to the column Bytes of this table: http://apps.timwhitlock.info/emoji/tables/unicode

License

This open-source software is distributed under the MIT License. See LICENSE.md

Contributing

All kinds of contributions are welcome - code, tests, documentation, bug reports, new features, etc...

  • Send feedbacks.
  • Submit bug reports.
  • Write/Edit the documents.
  • Fix bugs or add new features.

Contact me

You can contact me via Telegram but if you have an issue please open one.

Support me

You can support me using via LiberaPay Donate using Liberapay

or buy me a beer or two using Paypal.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].