89 lines
3.4 KiB
PHP
89 lines
3.4 KiB
PHP
<?
|
|
namespace themes\merakomis;
|
|
|
|
class Telegram {
|
|
const BOT = '7325473761:AAEpgtz1Ek4_wPDnE1KC341nIhUWIYLIwM0';
|
|
const BOT_TEXT_ID = 'meraProjectBot';
|
|
|
|
|
|
|
|
static function getStartLink($start = ''){
|
|
return "https://t.me/".self::BOT_TEXT_ID."?start=".$start;
|
|
}
|
|
static function getUpdates(){
|
|
|
|
$last = intval(file_get_contents($path = __DIR__.'/last.txt'));
|
|
|
|
$url = 'https://api.telegram.org/bot'.self::BOT.'/getUpdates?offset='.($last);
|
|
if ($curl = curl_init()){
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
|
|
$out = curl_exec($curl);
|
|
curl_close($curl);
|
|
$res = json_decode($out,true);
|
|
foreach ($res['result'] as $v){
|
|
$update_id = $v['update_id'];
|
|
$mess = $v['message'];
|
|
$text = $mess['text'];
|
|
$acc_id = $mess['from']['id'];
|
|
if(substr($text,0,7)=='/start '){
|
|
$code = substr($text,7);
|
|
$keys = explode("-",$code);
|
|
$key = $keys[0];
|
|
$id = $keys[1];
|
|
$hash = $keys[2];
|
|
$emp = Emp::getByID(intval($id));
|
|
$hash2 = Emp::getTgHash($key,$emp);
|
|
|
|
if(strcmp($hash,$hash2)===0){
|
|
if($emp[Emp::$TG_ACC]){
|
|
if($emp[Emp::$TG_ACC]!=$acc_id){
|
|
self::send($emp[Emp::$TG_ACC],'От вашего телеграм аккаунт откреплён аккаунт '.$emp[Emp::$EMAIL]);
|
|
}
|
|
}
|
|
$r = Emp::updateById($id,[
|
|
Emp::$TG_ACC=>$acc_id,
|
|
]);
|
|
self::send($acc_id,'Ваш телеграм аккаунт закреплён за '.$emp[Emp::$EMAIL]);
|
|
//self::send($acc_id,'Ваш телеграм аккаунт закреплён за '.$emp[Emp::$EMAIL]."\n".$r->query."\n".($r->res?1:0));
|
|
}
|
|
//self::send($acc_id,'Твой код: '.$code);
|
|
}
|
|
file_put_contents($path,$update_id+1);
|
|
file_put_contents(__DIR__.'/updates.txt',date('d.m.Y H:i:s')."\n".je($v,true)."\n\n",FILE_APPEND);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
static function send($acc_id,$text){
|
|
|
|
//$url = 'https://api.telegram.org/bot'.self::$TELEGRAM_BOT_TOKEN.'/sendMessage?chat_id=-491226996&text='.urlencode($text);
|
|
$url = 'https://api.telegram.org/bot' . self::BOT . '/sendMessage?chat_id=' . $acc_id . '&text=' . urlencode($text).'&parse_mode=Markdown';
|
|
|
|
$res = [];
|
|
if ($curl = curl_init()) {
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
|
|
$out = curl_exec($curl);
|
|
curl_close($curl);
|
|
$res = json_decode($out);
|
|
}
|
|
file_put_contents(__DIR__.'/log.txt',date('d.m.Y H:i:s')."\n".je([
|
|
'acc'=>$acc_id,
|
|
'text'=>$text,
|
|
'res'=>$res,
|
|
],true)."\n\n",FILE_APPEND);
|
|
return [
|
|
'out'=>$out,
|
|
'res'=>$res,
|
|
];
|
|
}
|
|
|
|
|
|
|
|
}
|