PHP Classes

PHP Telegram Bot Send Image or Random File: Telegram bot that responds by sending random files

Recommend this page to a friend!
  Info   View files Example   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 109 This week: 1All time: 9,634 This week: 560Up
Version License PHP version Categories
rand-file 1.0.0MIT/X Consortium ...5PHP 5, Web services, Audio, Chat
Description 

Author

This package implements a Telegram bot that responds by sending random files.

It provides a script and class files that can respond to requests sent to the script URL after registering it as Telegram bot Web hook.

The script can respond with a message with a file that can be of many types like images of photos, stickers, audio files, voice notes, documents, GIF images, etc..

Innovation Award
PHP Programming Innovation award nominee
March 2022
Number 4
Telegram is an application that became popular as an alternative to other traditional chat applications.

Telegram allows the creation of robots that can respond automatically to messages sent to bot users.

This package implements a Telegram chatbot that can send messages to respond to chat messages.

Manuel Lemos
Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 21 packages by
Country: Peru Peru
Age: ???
All time rank: 21074 in Peru Peru
Week rank: 8 Up1 in Peru Peru Up
Innovation award
Innovation award
Nominee: 9x

Winner: 3x

Example

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

use
App\Config\Files;
use
App\Models\Bot;
use
App\Config\StringUtils;

const
BOT_TOKEN = 'YOUR BOT TOKEN';
const
GITHUB = 'https://github.com/Mateodioev/';
const
REPO = 'rand-file';

$bot = new Bot(BOT_TOKEN);

$up = $bot::GetContent();

if (isset(
$up['message'])) {
   
$msg = $up['message'];
   
$chat_id = $msg['chat']['id'];
   
$msg_id = $msg['message_id'];
}

// Messages text
if (isset($msg['text'])) {
   
   
$message = $msg['text'];
   
$name = StringUtils::QuitMarkdown(@$msg['from']['first_name'] . ' ' . @$msg['from']['last_name']);

    if (
$bot::Cmd('start')) {
       
$hi = "Hola " . $name . ", enviame un sticker, gif, foto, o un audio y yo te enviare otro archivo del mismo tipo\n\nPd: No me hago responsable de los archivos enviados por el bot, ya que estos proviene de los usuarios";
       
$bot::SendMsg($chat_id, $hi, $msg_id, ['inline_keyboard' => [[['text' => 'Gihub', 'url' => GITHUB], ['text' => 'Repository', 'url' => GITHUB.REPO]]]], 'markdown');
    }
    exit;
}

$all = Files::Open('all_ids');

// Stickers
if (isset($msg['sticker'])) {
   
$sticker_id = $msg['sticker']['file_id'];
   
$unique = $msg['sticker']['file_unique_id'];

   
Bot::Sticker($chat_id, SaveAndGet('sticker', $sticker_id, $unique, $all), $msg_id);
    exit;
}

// Animations (Gifs)
if (isset($msg['animation'])) {
   
$gif_id = $msg['animation']['file_id'];
   
$unique = $msg['animation']['file_unique_id'];

   
Bot::Gif($chat_id, SaveAndGet('animation', $gif_id, $unique, $all), null, $msg_id);
    exit;
}

// Photos
if (isset($msg['photo'])) {
   
$photo = $msg['photo'][count($msg['photo']) - 1];
   
$photo_id = $photo['file_id'];
   
$unique = $photo['file_unique_id'];

   
Bot::Photo($chat_id, SaveAndGet('photo', $photo_id, $unique, $all), null, $msg_id);
    exit;
}

// Audio
if (isset($msg['audio'])) {
   
$audio_id = $msg['audio']['file_id'];
   
$unique = $msg['audio']['file_unique_id'];

   
Bot::Audio($chat_id, SaveAndGet('audio', $audio_id, $unique, $all), null, $msg_id);
    exit;
}

// Voice
if (isset($msg['voice'])) {
   
$voice_id = $msg['voice']['file_id'];
   
$unique = $msg['voice']['file_unique_id'];

   
Bot::Voice($chat_id, SaveAndGet('voice', $voice_id, $unique, $all), null, $msg_id);
    exit;
}

// Documents
if (isset($msg['document'])) {
   
$doc_id = $msg['document']['file_id'];
   
$unique = $msg['document']['file_unique_id'];

   
Bot::sendDocument(['chat_id' => $chat_id, 'document' => SaveAndGet('document', $doc_id, $unique, $all), 'reply_to_message_id' => $msg_id, 'allow_sending_without_reply' => true]);
    exit;
}



/**
 * Guardar el id según su tipo y obtener otro id random del mismo tipo
 */
function SaveAndGet(string $type, string $id, string $unique, array $all): string
{
   
SaveId($type, $id, $unique, $all);
    return
Files::OpenUnique($type);
}

/**
 * Guardar un id solo si no es repetido el unique_id
 */
function SaveId(string $type, string $id, string $unique_id, array $all)
{
    if (
StringUtils::Compare($all, $unique_id)) return;

   
Files::Save($type, $id);
   
Files::Save('all_ids', $unique_id);
}


Details

Random file telegram bot ?

Enviale un archivo y este te devolverá otro del mismo tipo

Obtener un token para el bot

Valla a @BotFather y escriba los siguientes comandos:

1. /newbot
2. Responda con un nombre para su bot
3. Escriba un username (@) para su bot, este debe terminar si o si en 'bot'

BotFather al final le devolvera un token

Setwebhook

    $endpoint = 'https://api.telegram.org/bot`BOT_TOKEN`/'
    $method = 'setwebhook'
    $params = url=dominio.donde.esta.hosteado.su.bot.com

    Con cURL
    $ curl 'https://api.telegram.org/botBOT_TOKEN/setwebhook?url=dominio.com/path_a_su_archivo'

  Files folder image Files  
File Role Description
Files folder imagesrc (2 directories)
Files folder imagevendor (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageConfig (3 files)
Files folder imageModels (1 file)

  Files folder image Files  /  src  /  Config  
File Role Description
  Plain text file Files.php Class Class source
  Plain text file Request.php Class Class source
  Plain text file StringUtils.php Class Class source

  Files folder image Files  /  src  /  Models  
File Role Description
  Plain text file Bot.php Class Class source

  Files folder image Files  /  vendor  
File Role Description
Files folder imagecomposer (10 files)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script

  Files folder image Files  /  vendor  /  composer  
File Role Description
  Accessible without login Plain text file autoload_classmap.php Aux. Auxiliary script
  Accessible without login Plain text file autoload_namespaces.php Aux. Auxiliary script
  Accessible without login Plain text file autoload_psr4.php Aux. Auxiliary script
  Plain text file autoload_real.php Class Class source
  Plain text file autoload_static.php Class Class source
  Plain text file ClassLoader.php Class Class source
  Accessible without login Plain text file installed.json Data Auxiliary data
  Accessible without login Plain text file installed.php Aux. Auxiliary script
  Plain text file InstalledVersions.php Class Class source
  Accessible without login Plain text file LICENSE Lic. License text

 Version Control Unique User Downloads Download Rankings  
 100%
Total:109
This week:1
All time:9,634
This week:560Up