v2.5.2
Giriş yap

php ile telegram botu

Anonim
2,080 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');
}

Cevap yaz
Cevaplar (3)
resootr43
611 gün önce

Çalışması için bir hosting ve domaine sahip olmam şart mı

emiremrahkaragoz
805 gün önce

Merhabalar, chat id yi sisteme kayıt ettirebiliyorum ama mesajı diyelim ki bir kişiye göndericem bunu nasıl yaparım sistem de kaydettiğim chat id ye göndermek istediğimde?

h4ckdr0
1181 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 !