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   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 112 All time: 9,623 This week: 56Up
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  
Innovation award
Innovation award
Nominee: 10x

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 (19)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:112
This week:0
All time:9,623
This week:56Up