درس: كيفية إرسال الرسائل في بوت تليجرام باستخدام PHP

الخطوة 1: إعداد مكتبة Telegram Bot API

يجب أولاً تثبيت مكتبة Telegram Bot API لـ PHP. يمكنك استخدام Composer لتثبيتها بسهولة. يمكنك تشغيل الأمر التالي في مجلد مشروعك:

composer require telegram-bot/api

الخطوة 2: كتابة كود البوت

قم بإنشاء ملف PHP جديد للبوت وقم بفتحه للتعديل.

<?php

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

use Telegram\Bot\Api;

// Replace 'YOUR_BOT_TOKEN' with your actual bot token
$token = 'YOUR_BOT_TOKEN';

// Initialize the bot
$telegram = new Api($token);

// Replace 'CHAT_ID' with the chat id you want to send the message to
$chatId = 'CHAT_ID';

// The message you want to send
$message = 'Hello, this is a test message from your Telegram bot!';

// Send the message
$response = $telegram->sendMessage([
    'chat_id' => $chatId,
    'text' => $message
]);

// Check if the message was sent successfully
if ($response->isOk()) {
    echo 'Message sent successfully!';
} else {
    echo 'Failed to send message: ' . $response->getDescription();
}
?>

الخطوة 3: تشغيل البوت

قم بتشغيل البوت عن طريق تنفيذ الملف PHP على الخادم.

php your_bot_file.php

الخطوة 4: اختبار البوت

أرسل رسالة إلى البوت على تطبيق Telegram وتأكد من أن البوت يستجيب برسالة الاختبار التي قمت بإرسالها.