Giter Club home page Giter Club logo

telegrambotphp's Introduction

TelegramBotPHP

A very simple PHP Telegram Bot API for sending messages.
Compliant with the May 25, 2016 Telegram Bot API update.

Requirements

  • PHP5
  • Curl 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 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:

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

Installation

  • Copy Telegram.php into your server and include it in your new bot script
include("Telegram.php");
$telegram = new Telegram($bot_id);

Configuration (WebHook)

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

Examples

$telegram = new Telegram($bot_id);
$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($bot_id);
$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["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] (https://telegram.me/cowmooobot).

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

$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 documentation check http://eleirbag89.github.io/TelegramBotPHP/

Build keyboard parameters

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

Send a custom keyboard. $option is an array of array KeyboardButton.
Check [ReplyKeyBoardMarkUp] (https://core.telegram.org/bots/api#replykeyboardmarkup) for more info.

buildInlineKeyBoard(array $inline_keyboard)

Send a custom keyboard. $inline_keyboard is an array of array InlineKeyboardButton.
Check [InlineKeyboardMarkup] (https://core.telegram.org/bots/api#inlinekeyboardmarkup) for more info.

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

Create an InlineKeyboardButton.
Check [InlineKeyBoardButton] (https://core.telegram.org/bots/api#inlinekeyboardbutton) for more info.

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

Create a KeyboardButton.
Check [KeyBoardButton] (https://core.telegram.org/bots/api#keyboardbutton) for more info.

buildKeyBoardHide($selective=true)

Hide a custom keyboard.
Check [ReplyKeyBoarHide] (https://core.telegram.org/bots/api#replykeyboardhide) for more info.

buildForceReply($selective=true)

Show a Reply interface to the user.
Check [ForceReply] (https://core.telegram.org/bots/api#forcereply) for more info.

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

Contact me

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

Support me

You can support me using Flattr.

Flattr this git repo

telegrambotphp's People

Contributors

eleirbag89 avatar happy-code-com avatar ivmaks avatar xpyctum avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.