-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBot.php
85 lines (70 loc) · 1.91 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
<?php
include_once 'Core.php';
include_once 'Api.php';
/**
* @author Farshad Tofighi
* @see https://farshadth.ir
* @see https://github.com/farshadth/Telegram
* @version 1.0
*/
class Bot
{
use Core, Api;
public $message;
public $commands;
public function __construct()
{
// initial setting
$this->init([
'bot_token' => "Bot_Token",
'sleepPerRequest' => 0.2, // optional, sleep per request in long_polling method, default is 0.5 second
'method' => 'long_polling', // optional, "long_polling" or "webhook", default is "long_polling"
'mysql' => [ // optional, if you dont want to use database just remove it
'host' => 'HOST',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE',
],
]);
// set commands
$this->setCommands([
'start,/start' => 'startCommand',
'help' => 'helpCommand',
]);
}
public function run()
{
$messages = $this->getMessages();
foreach($messages as $this->message)
{
$this->saveLastUpdateId(); // save last update id
// when user send message
if($this->MessageSent())
{
// code here
$this->messageIsCommand($this);
}
// when user edit his message
else if($this->messageEdited())
{
// code here
}
// when user click on inline keyboard
else if($this->keyboardClicked())
{
// code here
}
}
}
}
$obj = new Bot();
if($obj->method == 'webhook')
$obj->run();
else if($obj->method == 'long_polling')
{
while (true)
{
$obj->run();
sleep($obj->sleepPerRequest);
}
}