forked from farshadth/telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.php
126 lines (114 loc) · 4.02 KB
/
Bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
include 'Functions.php';
include 'Api.php';
/**
* @author farshadth
* @website https://farshadth.ir
* @document https://github.com/farshadth/Telegram
* @version 1.0
*/
class Bot extends Api
{
use Functions;
public $sleep;
public $method;
public $message;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function __construct()
{
set_time_limit(0);
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
date_default_timezone_set('Asia/Tehran');
@DB::connect('HOST' , 'USERNAME' , 'PASSWORD' , 'DATABASE');
$this->token = "Bot_Token";
$this->sleep = 0.2; // sleep per request in long_polling method
$this->method = 'long_polling'; // "long_polling" or "webhook"
if($this->method == 'long_polling')
$this->deleteWebhook();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function Run()
{
if($this->method == 'webhook')
$messages = array(json_decode(file_get_contents( 'php://input' ), true));
else if($this->method == 'long_polling')
$messages = $this->getUpdates();
foreach($messages as $this->message)
{
$last_update_id = $this->message->update_id;
if(isset($this->message->message))
{
// when user send message
$this->message = $this->message->message;
if($this->message->text == '/start')
$this->start_command();
else if($this->message->text == '/help')
$this->help_command();
}
else if(isset($this->message->edited_message))
{
// when user edit his message
$this->message = $this->message->edited_message;
// code here
}
else if(isset($this->message->callback_query))
{
// when user click on inline keyboard
$this->message = $this->message->callback_query;
// code here
}
// save last update id
if($this->method == 'long_polling')
file_put_contents('update_id.txt' , $last_update_id);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function start_command()
{
$text = "start command";
// set button keyboard
$keyboard1 =
[
[
[ 'text' => 'Button 1' ],
[ 'text' => 'Button 2' ]
],
[
[ 'text' => 'Button 3' ],
],
];
// set inline keyboard
$keyboard2 =
[
[
[ 'text' => 'Button', 'callback_data' => 'data' ],
],
[
[ 'text' => 'farshadth.ir', 'url' => 'https://farshadth.ir' ],
],
];
$keyboard = $this->buttonKeyboard($keyboard1);
// $keyboard = $this->inlineKeyboard($keyboard2); // use for send inline keyboard instead
$optional = "&reply_markup=$keyboard";
$this->sendMessage($this->message->chat->id, $text, $optional);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function help_command()
{
$text = "help command";
$this->sendMessage($this->message->chat->id, $text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$obj = new Bot();
if($obj->method == 'webhook')
$obj->Run();
else if($obj->method == 'long_polling')
{
while (true)
{
$obj->Run();
sleep($obj->sleep);
}
}