v2.5.2
Giriş yap

php ile telegram botu

Anonim
2,094 defa görüntülendi

merhaba,
youtubeda tayfun erbilen anlattığı gibi
telegram botu yapmak istiyorum. Ama bir noktada tıkandım
hocamın yaptığı gibi telegram da 'hello' yazınca
'sanada' yazmasını denedim. ama olmadı.
Kodlarım:

<?php
class TelegramBot {

const API_URL = 'https://api.telegram.org/bot1082539916:AAFiAaozsqvZrphXrOo1XsINc6jxOOnczsY';
public $token;
public $chatid;

public function setToken($token){

$this->token=$token;

}
public function getData(){

$data= json_decode(file_get_contents('php://input'));
$this->chatid = $data->message->chat->id;
return $data->message;

}
public function sendMessage($message){
return $this->request('sendMessage',[

'chat_id' => $this->chatid,
'text' => $message

]);
}

public function request($method,$posts){

$ch = curl_init();
$url =self::API_URL.'/'.$method;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($posts));

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
  echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;

}
public function setWebhook($url){

return $this->request('setWebhook',[ 'url' => $url

]);
}

}

$telegram = new TelegramBot();
// echo $telegram->setWebhook('https://kodogren.requestcatcher.com/');

$data = $telegram->getData();

if ($data->text == 'hello') {

$telegram->sendMessage('sanada');
}

h4ckdr0
1191 gün önce

Aynı hatayı bende alıyordum...
Söyle çozdüm:

01- Şu kısım yerine
// echo $telegram->setWebhook('https://kodogren.requestcatcher.com/');

02- Böyle yaz sonra websiteni yenile
echo $telegram->setWebhook('https://domanin_adresin.com/telegram.php');

sonra kodu tekrar pasif yap

  • Bunu yapman webhook adresini değiştirecektir

DİPNOT: Local'de kod çalışmaz !