0

PHP를 사용하여 Telegram Chatbot를 통합하기 위해 아래 단계를 수행했습니다. ?텔레 그램에 대한 메시지가 자동으로 수신되지 않습니다.

을 가지고, 나는 간단한 만든 다음 코드 줄을 사용하여 PHP 파일.

<?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); 
} 

메시지는 제대로 수신되지 않습니다.

답변

0

확실하지 않지만이 경우에 해당 될 수 있습니다.

당신은 당신이 성공적으로 실행은 webhook URL 전보 ChatBot에 메시징 전에 요청을 듣기 시작이 있는지 확인해야합니다.

URL : 지금 현재로 https://api.telegram.org/bot/setWebhook?url=https://mywebsite.com/path/to/filename.php

는 전보의 Chatbot의 마지막으로, 나는 성공적으로 만든 데모는 PHP를 사용하여.