PHP code example of atanych / telegram-bot

1. Go to this page and download the library: Download atanych/telegram-bot 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/ */

    

atanych / telegram-bot example snippets



//Composer Loader
$loader = BOT_NAME = 'namebot';
$link = 'https://yourdomain/yourpath_to_hook.php';
try {
    // create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
    // set webhook
    $result = $telegram->setWebHook($link);
    if ($result->isOk()) {
        echo $result->getDescription();
    }
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    echo $e;
}


$loader = ur_bot_api_key';
$BOT_NAME = 'namebot';

try {
    // create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

    // handle telegram webhook request
    $telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // log telegram errors
    // echo $e;
}

$result = $telegram->setWebHook($url, $certificate_path);

#!/usr/bin/env php

$loader = OT_NAME = 'namebot';
$credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass', 'database'=>'dbname');

try {
    // create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
    $telegram->enableMySQL($credentials);
    // handle telegram getUpdate request
    $telegram->handleGetUpdates();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // log telegram errors
     echo $e;
}                

$data['chat_id'] = $chat_id;
$result = Request::sendPhoto($data,$this->telegram->getUploadPath().'/'.'image.jpg');

$data['chat_id'] = $chat_id;
$data['photo'] = $file_id;
$result = Request::sendPhoto($data);

Request::sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']);

$results = $telegram->sendToActiveChats(
        'sendMessage', //callback function to execute (see Request.php methods)
        array('text'=>'Hey! Checkout the new feature!!'), //Param to evaluate the request
        true, //Send to chats (group chat)
        true, //Send to users (single chat)
        null, //'yyyy-mm-dd hh:mm:ss' date range from
        null  //'yyyy-mm-dd hh:mm:ss' date range to
    );

$credentials = array('host'=>'localhost', 'user'=>'dbuser',
'password'=>'dbpass', 'database'=>'dbname');

$telegram->enableMySQL($credentials);

$telegram->enableMySQL($credentials, $BOT_NAME.'_');

$COMMANDS_FOLDER = __DIR__.'/Commands/';
$telegram->addCommandsPath($COMMANDS_FOLDER);

$telegram->enableAdmins(array('TelegramUserID','Othersid'));

$telegram->setCommandConfig('date',
array('google_api_key'=>'your_google_api_key_here'));

$telegram->setDownloadPath("yourpath/Download");
$telegram->setUploadPath("yourpath../Upload");    

$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME.'.log');

$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME.'.log');
$telegram->setLogVerbosity(3);     

chmod 775 getUpdateCLI.php

./getUpdateCLI.php