Afin d'intégrer Telegram Chatbot en utilisant PHP, j'ai déjà suivi les étapes ci-dessous.Message ne recevant pas automatiquement pour Telegram Fonction CHATBOT utilisant PHP
- Server avec certificat SSL
- webhook avec état de fonctionnement URL: https://api.telegram.org/bot/setWebhook url = https://mywebsite.com/path/to/filename.php
- Créé ChatBot et un jeton valide
Après cela, je l'ai fait simples Fichier PHP en utilisant la ligne de code suivante.
<?php
define('BOT_TOKEN', 'CHANGE-ME');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
// This is to read incoming message and fetch chatid
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
// compose reply
$reply ="";
switch ($message) {
case "/start":
$reply = "Welcome to chatbot world. Type /help to see commands";
break;
case "/test":
$reply = "test message";
break;
case "/hi":
$reply = "Hello from ChatBot";
break;
case "/help":
$reply = "commands: /start , /test , /hi , /help ";
break;
default:
$reply = "Something wrong";
}
// send reply
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
// Create a debug log.txt to check the response/reply from Telegram in JSON format.
// You can disable it by commenting checkJSON.
checkJSON($chatID,$update);
function checkJSON($chatID,$update){
$myFile = "log.txt";
$updateArray = print_r($update,TRUE);
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $chatID ."nn");
fwrite($fh, $updateArray."nn");
fclose($fh);
}
Bien que, je ne suis pas la réception du message correctement.