PHP code example of dratejinn / telegrambot

1. Go to this page and download the library: Download dratejinn/telegrambot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dratejinn / telegrambot example snippets



Telegram\API\Bot;
use Telegram\API\Method\SendMessage;

$bot = new Bot('bottoken here');

$sendMessage = new SendMessage;
$sendMessage->chatId = 1234566;
$sendMessage->text = 'Hello world!';
$sendMessage->call($bot);


Telegram\API\Bot;
use Telegram\API\Method\SendPhoto;

$bot = new Bot('bottoken here');

$sendPhoto = new SendPhoto;
$sendPhoto->chatId = 1234566;
$sendPhoto->photo = new InputFile('pathToFile');
$sendPhoto->call($bot);


declare(strict_types = 1);
namespace Examples\Bot;
use Telegram\Bot\ABot as TBot;
class ExampleBot extends TBot {
    const TOKEN = ''; //place your token here (optionally)
    protected $_handlers = [
        self::UPDATE_TYPE_MESSAGE => Handlers\MessageHandler::class
    ];
    public function __construct(string $token = NULL) {
        if ($token === NULL) {
            $token = self::TOKEN;
        }
        parent::__construct($token);
    }
}


declare(strict_types = 1);
namespace Examples\Bot\Handlers;
use Telegram\Bot\Handler\AMessageHandler;
class MessageHandler extends AMessageHandler {
    protected $_commandHandlers = [
        Command\ExampleCommandHandler::class,
    ];
    public function handleText(string $text) {
        $this->sendTextMessage('You typed: ' . $text);
    }
}


declare(strict_types = 1);
namespace Examples\Bot\Handlers\Command;
use Telegram\Bot\Handler\ACommandHandler;
class ExampleCommandHandler extends ACommandHandler {
    
    public function handleCommand(string $command) {
        $this->sendTextMessage('You fired the command: ' . $command);
        $this->sendTextMessage('Provided with arguments: ' . implode(' ', $this->getArguments()));
    }
    public static function GetRespondsTo() : array {
        return [
            'test',
            'example'
        ];
    }
}


declare(strict_types = 1);

namespace Examples\Bot;

use Telegram\API;
use Telegram\API\ConfigManager;
use Telegram\Storage\PDO\ConnectionDetails\ConnectionDetailsMySQL;
use Telegram\Storage\PDO\MySQLHandler;

AddConfigFromINIFile(__DIR__ . '/Log.ini', 'Log');
ConfigManager::AddConfigFromINIFile(__DIR__ . '/../Tokens.ini', 'token');
$tokens = ConfigManager::GetConfig('token');
$token = $tokens['devbot']['token'];
$bot = new ExampleBot($token);
if ($userCreds) {
    $connectionDetails = new ConnectionDetailsMySQL;
    $mysqlHandler = new MySQLHandler($connectionDetails, $userCreds, 'telegrambot');
    $bot->setStorageHandler($mysqlHandler);
}
$bot->run();



use Examples\Bot\ExampleBot;
use Telegram\API\Type\Update;

new ExampleBot('tokenHere');
	$update = json_decode($input);
	$updateObj = new Update($update);
	$bot->handleUpdate($update);
}