2017 © Pedro Peláez
 

library php-telegram-bot

wrapper class for tg bot api

image

smoqadam/php-telegram-bot

wrapper class for tg bot api

  • Wednesday, October 11, 2017
  • by smoqadam
  • Repository
  • 3 Watchers
  • 23 Stars
  • 132 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 3 Versions
  • 2 % Grown

The README.md

Scrutinizer Code Quality Codacy Badge, (*1)

php-telegram-bot

A wrapper class for Telegram Bot API, (*2)

Install

create composer.json :, (*3)

{
    "require": {
        "smoqadam/php-telegram-bot": "v1.0"
    }
}

$ composer install, (*4)

How to use:

Smoqadam\Telegram is a wrapper around Telegram bot API. After you instantiate Telegram object, you can register callbacks on the updates you receive and then respond accordingly., (*5)

Register callbacks: Use following functions: - cmd(), all normal messages - inlineQuery(), (*6)

API Methods: The available methods are almost same of official Telegram API (for now the wrapper does not natively handles games and messages update): - sendMessage(), getMe(), forwardMessage(), sendPhoto(), sendVideo(), sendSticker(), sendLocation(), sendDocument(), sendAudio(), sendChatAction(), getUserProfilePhotos(), answerInlineQuery(), (*7)

Getting current update: The current update is stored in the property Telegram::result (that is an object)., (*8)

Inline Result helper: To facilitate the creation of Inline Bot there are some helper classes under namespace InlineQuery\Result: - Article, (*9)

Keyboards helper: Also the same with keyboards; namespace is Keyboard: - Standard, classic keyboard - Remove, remove custom keyboard and show letter-keyboard - Inline, inline keyboard, (*10)

Use Keyboard: Use the keyboard is pretty simple with helpers, after you instantiate keyboard (Standard or Inline) you use: - addButton() - addRow(), (*11)

Note: These methods are chainable., (*12)

Example

Using long polling:, (*13)

<?php

require 'vendor/autoload.php';

use Smoqadam\Telegram;

$tg = new Telegram('API_TOKEN');

$tg->cmd('\/name:<<[a-zA-Z]{0,}>>', function ($args) use ($tg){
    $tg->sendMessage("my username is @".$args , $tg->getChatId());
});


$tg->cmd('\/number: <<:num>>' , function($args) use($tg){
    $tg->sendMessage("your number is : ".$args , $tg->getChatId());
});


$tg->cmd('Hello',function () use ($tg){
    $tg->sendChatAction(Telegram::ACTION_TYPING);
    $image = 'urltoqrcode.png';
    $tg->sendPhoto($image);
});

$tg->run();

Using webHooks:, (*14)

If you want to use webhooks you have to first call setWebhook method or open the following URL with your own data:, (*15)

https://api.telegram.org/API_TOKEN/setWebhook?url=https://yourdomain.com/index.php, (*16)

please change API_TOKEN and url parameter, (*17)

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

$message = file_get_contents('php://input');

use Smoqadam\Telegram;

$tg = new Telegram('API_TOKEN');

$tg->cmd('\/name:<<[a-zA-Z]{0,}>>', function ($args) use ($tg){
        $tg->sendMessage("my username is @".$args , $tg->getChatId());
});


$tg->cmd('\/number: <<:num>>' , function($args) use($tg){
    $tg->sendMessage("your number is : ".$args , $tg->getChatId());
});


$tg->cmd('Hello',function () use ($tg){
    $tg->sendChatAction(Telegram::ACTION_TYPING);
    $image = 'urltoqrcode.png';
    $tg->sendPhoto($image);
});

$tg->process(json_decode($message, true));

Now when you send /number 123 in telegram bot page , bot will answer to you your number is 123, (*18)

You can set argument in regex between << and >>, (*19)

Running bot

$ php index.php, (*20)

The Versions

11/10 2017

dev-master

9999999-dev

wrapper class for tg bot api

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Saeed

11/10 2017

v1.0

1.0.0.0

wrapper class for tg bot api

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Saeed

07/12 2016

dev-revert-3-develop

dev-revert-3-develop

wrapper class for tg bot api

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Saeed