2017 © Pedro Peláez
 

library telegrambot

A Telegram bot api

image

dratejinn/telegrambot

A Telegram bot api

  • Wednesday, July 4, 2018
  • by Dratejinn
  • Repository
  • 1 Watchers
  • 0 Stars
  • 66 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 10 Versions
  • 6 % Grown

The README.md

Telegram bot API

Code documented, (*1)

A PHP 7.0+ Telegram bot API used to create bots for Telegram. For more information check Telegrams bot API, (*2)

Base API

The basis of the bot API can be found in src/API. The structure reflects telegrams API as much as possible., (*3)

Basic examples

All sendable objects have a ->call(\Telegram\API\Bot $bot) method which can be used to send the object to the telegram API. Some examples can be found in Examples/Examples.php, (*4)

sending a chatmessage, (*5)

<?php
require_once 'vendor/autoload.php';

use 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);

Sending a photo, (*6)

<?php
require_once 'vendor/autoload.php';

use 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);

Creating a bot

Creating a bot is very easy. Telegram accepts two type of bots. One where the bot is continuously requesting updates using the getUpdates API call and the other is using WebHooks. Both options are available., (*7)

A simple bot The bot, (*8)

<?php
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);
    }
}

Message handler, (*9)

<?php
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);
    }
}

Command handler, (*10)

<?php
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'
        ];
    }
}

Running the bot with continuous polling

<?php
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;

require_once __DIR__ . '/../vendor/autoload.php';

if (file_exists(__DIR__ . '/userCreds.php')) {
    require_once __DIR__ . '/userCreds.php';
    $userCreds = mysqlUserCreds();
} else {
    $userCreds = NULL;
}
define('DEVELOPMENT_MODE', TRUE);
API\ConfigManager::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();

Running the bot using webhooks

<?php

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

require_once __DIR__ . '/../vendor/autoload.php';

$input = file_get_contents('php://input');
if ($input) {
    $bot = new ExampleBot('tokenHere');
    $update = json_decode($input);
    $updateObj = new Update($update);
    $bot->handleUpdate($update);
}

The Versions

04/07 2018

dev-develop

dev-develop

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

10/03 2018

dev-master

9999999-dev

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

10/03 2018

v1.7.0

1.7.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

04/02 2018

v1.6.0

1.6.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

12/10 2017

v1.5.0

1.5.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

23/08 2017

v1.4.0

1.4.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

21/07 2017

v1.3.0

1.3.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

03/07 2017

v1.2.0

1.2.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

21/05 2017

v1.1.0

1.1.0.0

A Telegram bot api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Freek van der Veer

01/05 2017

v1.0.1

1.0.1.0

A Telegram bot api

  Sources   Download

by Freek van der Veer