Skip to content

Instantly share code, notes, and snippets.

@Padilo300
Created March 21, 2020 15:35
Show Gist options
  • Save Padilo300/d48f3c4955604617c481482ace0bea5e to your computer and use it in GitHub Desktop.
Save Padilo300/d48f3c4955604617c481482ace0bea5e to your computer and use it in GitHub Desktop.
telegram
<?php
namespace App\Http\Controllers\telegram;
use App\Http\Controllers\bitrix\user;
use App\Http\Controllers\BX;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Telegram\Bot\Api;
use Telegram\Bot\Keyboard\Keyboard;
class telegramController extends Controller
{
public $telegram = null;
public $updates = null;
protected $chat_id = null;
protected $user_text = null;
protected $first_name = null;
protected $last_name = null;
protected $username = null;
public $cmd_bestUser = "\xF0\x9F\x9A\x80 ТОП лучших";
public $cmd_failUser = "\xF0\x9F\x90\x8C ТОП опоздаших";
public $cmd_allUser = "\xF0\x9F\x91\xAA Список сотрудников";
public $cmd_report = "\xF0\x9F\x93\x8A Отчет за месяц";
protected $arrCommand = null;
public function __construct(){
$this->telegram = new Api('3423423102134127123405213412012374611:AAHTxXFBSzqTU8aSa4nXiblAMgEKG71qlSc');
$update = $this->telegram->getWebhookUpdates();
$this->loadUserData($update);
$this->loadCommand();
$this->aMTyping();
}
/**
* @param array
* @return void
*/
private function loadUserData( $update){
$this->chat_id = $update['message']['from']['id'];
$this->user_text = $update['message']['text'];
if(array_key_exists('first_name',$update['message']['from'])){
$this->first_name = $update['message']['from']['first_name'] ;
}else{
$this->first_name = '';
}
if(array_key_exists('last_name',$update['message']['from'])){
$this->last_name = $update['message']['from']['last_name'] ;
}else{
$this->last_name = '';
}
if(array_key_exists('username',$update['message']['from'])){
$this->username = $update['message']['from']['username'] ;
}else{
$this->username = '';
}
}
public function userList(){
$userController = new user();
$users = $userController->downloadAllUsers();
foreach ($users as $user) {
$name = $user['NAME'] . ' ' . $user['LAST_NAME'];
}
$this->sendMessage($name);
}
/**
* @return string
* @return string | null
*/
private function findCommand($str){
return $this->arrCommand[$str];
}
/**
* @return boolean
*/
private function loadCommand(){
$this->arrCommand = [
$this->cmd_bestUser => 'cmd_bestUser' ,
$this->cmd_failUser => 'cmd_failUser' ,
$this->cmd_allUser => 'cmd_allUser' ,
$this->cmd_report => 'cmd_report' ,
];
}
private function cmd_report(){
$this->sendMessage('Отчет');
}
private function cmd_bestUser(){
$this->sendMessage('Лучшие');
}
private function cmd_failUser(){
$this->sendMessage('постоянно опаздывают');
}
private function cmd_allUser(){
$this->sendMessage('все пользователи');
}
public static function sm($messaggio = 'Аргумент не переданн'){
$chatID = '544625714';
$token = '860948571:AAFNanFWQrcKn-YbL6Pq9dzx1E3RHfMGQEI';
$url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
$url = $url . "&text=" . urlencode($messaggio);
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
}
public function index(){
$command = $this->findCommand($this->user_text);
$this->$command();
}
public function nav(){
$Keyboard = new Keyboard();
$k = $Keyboard
->row($this->cmd_bestUser,$this->cmd_failUser)
->row($this->cmd_allUser)
->row($this->cmd_report)
->toJson();
$this->sendMessage('Меню',$k);
}
protected function sendMessage(string $text ='',$keyboard = false){
$data = [
'chat_id' => $this->chat_id,
'text' => $text,
];
if($keyboard != false){
$data['reply_markup'] = $keyboard;
}
$this->telegram->sendMessage($data);
}
protected function aMTyping(){
$this->telegram->sendChatAction([
'chat_id' => $this->chat_id,
'action' => 'typing'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment