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

emiremrahkaragoz
814 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?