Skip to content

Commit 5ab89fd

Browse files
committed
Introducing Admin Interface and ServerResponse type
1 parent 235fd82 commit 5ab89fd

17 files changed

+919
-28
lines changed

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.org/bots/api)
1414

1515

16-
## introduction
16+
## Introduction
1717
This is a pure php Telegram Bot, fully extensible via plugins. Telegram recently announced official support for a [Bot API](https://telegram.org/blog/bot-revolution) allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one could simply write a plugin and have interactions in a matter of minutes.
1818
The Bot supports Reply Markup and handle commands in group chat with multiple bot.
1919

@@ -139,6 +139,7 @@ try {
139139
}
140140
```
141141

142+
##MySQL storage
142143
If you want insert in database messages/users for further usage in commands, create database and import structure.sql and enable mysql support after object creation and BEFORE handle method
143144

144145
```php
@@ -154,8 +155,7 @@ $telegram->enableMySQL($credentials, $BOT_NAME.'_');
154155

155156
```
156157

157-
Commads
158-
--------------
158+
##Commands
159159
The bot is able to recognise commands in chat with multiple bot.
160160
It can execute command triggering a chat event. Here's the list:
161161

@@ -165,23 +165,31 @@ It can execute command triggering a chat event. Here's the list:
165165
- New chat title (**NewchattitleCommand.php**)
166166
- Left chat participant (**LeftchatparticipantCommand.php**)
167167

168-
**GenericCommand.php** let you handle commands that non exist or use commands as var:
169-
Favourite colour? **/black /red**
170-
Favourite number? **/1 /134**
168+
**GenericCommand.php** let you handle commands that non exists or use commands as variable:
169+
Favourite colour? **/black, /red**
170+
Favourite number? **/1, /134**
171171

172172

173-
Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done adding before the method:
173+
Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done adding the method:
174174

175175
```php
176176
$COMMANDS_FOLDER = __DIR__.'/Commands/';
177177
$telegram->addCommandsPath($COMMANDS_FOLDER);
178178

179179
```
180180

181+
##Admin Commands (new!)
182+
Enabling this feature, the admin bot can perform some super user command like send message to all.
183+
You can specify one or more admin with this option:
181184

185+
```php
186+
$telegram->enableAdmins(array('TelegramUserID','Othersid'));
187+
```
188+
Telegram user id can be retrieved with the command /whoami.
189+
Admin commands are stored in src/Admin/ folder.
190+
To know all the commands avaiable type /help.
182191

183-
Send message to all active chats (new!)
184-
---------------------------------------
192+
##Send message to all active chats
185193
To do this you have to enable the mysql connection.
186194
Here's an example of use:
187195

@@ -198,8 +206,7 @@ $results = $telegram->sendToActiveChats(
198206
print_r($results);
199207
```
200208

201-
Utilis
202-
------
209+
##Utilis
203210
You can also log incoming messages on a text file, set this option with the methods:
204211
```php
205212
$telegram->setLogRequests(true);
@@ -210,14 +217,12 @@ $telegram->setLogPath($BOT_NAME.'.log');
210217
This code is available on [Github][0]. Pull requests are welcome.
211218

212219

213-
Troubleshooting
214-
-------------
220+
##Troubleshooting
215221

216222
If you like living on the edge, please report any bugs you find on the [PHP Telegram Bot issues](https://github.com/akalongman/php-telegram-bot/issues) page.
217223

218224

219-
Contributing
220-
-------------
225+
##Contributing
221226

222227
See [CONTRIBUTING.md](CONTRIBUTING.md) for information.
223228

12 KB
Binary file not shown.

src/Admin/SendtoallCommand.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Longman\TelegramBot\Commands;
12+
13+
use Longman\TelegramBot\Request;
14+
use Longman\TelegramBot\Command;
15+
use Longman\TelegramBot\Entities\Update;
16+
use Longman\TelegramBot\Exception\TelegramException;
17+
18+
class SendtoallCommand extends Command
19+
{
20+
protected $name = 'sendall';
21+
protected $description = 'Send the message to all the user\'s bot';
22+
protected $usage = '/sendall <message to send>';
23+
protected $version = '1.2.0';
24+
protected $enabled = true;
25+
protected $public = true;
26+
27+
28+
public function execute()
29+
{
30+
$update = $this->getUpdate();
31+
$message = $this->getMessage();
32+
33+
$chat_id = $message->getChat()->getId();
34+
$message_id = $message->getMessageId();
35+
$text = $message->getText(true);
36+
37+
if (empty($text)) {
38+
$text = 'Write te message to sent: /sendall <message>';
39+
} else {
40+
$results = $this->telegram->sendToActiveChats(
41+
'sendMessage', //callback function to execute (see Request.php methods)
42+
array('text'=> $text), //Param to evaluate the request
43+
true, //Send to chats (group chat)
44+
true, //Send to users (single chat)
45+
null, //'yyyy-mm-dd hh:mm:ss' date range from
46+
null //'yyyy-mm-dd hh:mm:ss' date range to
47+
);
48+
49+
$tot = 0;
50+
$fail = 0;
51+
52+
$text = "Message sended to:\n";
53+
foreach ($results as $result) {
54+
$status = '';
55+
$type = '';
56+
print_r($result);
57+
if ($result->isOk()) {
58+
$status = '✔️';
59+
60+
$ServerResponse = $result->getResult();
61+
$chat = $ServerResponse->getChat();
62+
if ($chat->isSingleChat()) {
63+
$name = $chat->getFirstName();
64+
$type = 'user';
65+
} else {
66+
$name = $chat->getTitle();
67+
$type = 'chat';
68+
}
69+
} else {
70+
$status = '✖️';
71+
++$fail;
72+
}
73+
++$tot;
74+
75+
$text .= $tot.') '.$status.' '.$type.' '.$name."\n";
76+
}
77+
$text .= "Delivered: ".($tot - $fail).'/'.$tot."\n";
78+
}
79+
if ($tot == 0) {
80+
$text = "No users or chats found..";
81+
}
82+
83+
$data = array();
84+
$data['chat_id'] = $chat_id;
85+
//$data['reply_to_message_id'] = $message_id;
86+
$data['text'] = $text;
87+
88+
$result = Request::sendMessage($data);
89+
return $result;
90+
}
91+
}

src/Commands/GenericCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class GenericCommand extends Command
1818
{
1919
protected $name = 'Generic';
20-
protected $description = 'Handle genric commands or is executed by defaul when a command is not found';
20+
protected $description = 'Handle generic commands or is executed by defaul when a command is not found';
2121
protected $usage = '/';
2222
protected $version = '1.0.0';
2323
protected $enabled = true;
@@ -26,9 +26,12 @@ public function execute()
2626
{
2727
$update = $this->getUpdate();
2828
$message = $this->getMessage();
29+
$chat_id = $message->getChat()->getId();
2930
//you can use $command as param
3031
$command = $message->getCommand();
3132

33+
$data = [];
34+
$data['chat_id'] = $chat_id;
3235
$data['text'] = 'Command: '.$command.' not found.. :(';
3336
$result = Request::sendMessage($data);
3437
return $result;

src/Entities/Audio.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Longman\TelegramBot\Entities;
12+
13+
use Longman\TelegramBot\Exception\TelegramException;
14+
15+
class Audio extends Entity
16+
{
17+
protected $file_id;
18+
protected $duration;
19+
protected $mime_type;
20+
protected $file_size;
21+
22+
23+
public function __construct(array $data)
24+
{
25+
26+
$this->file_id = isset($data['file_id']) ? $data['file_id'] : null;
27+
if (empty($this->file_id)) {
28+
throw new TelegramException('file_id is empty!');
29+
}
30+
31+
$this->width = isset($data['duration']) ? $data['duration'] : null;
32+
if (empty($this->duration)) {
33+
throw new TelegramException('duration is empty!');
34+
}
35+
36+
$this->height = isset($data['mime_type']) ? $data['mime_type'] : null;
37+
$this->file_size = isset($data['file_size']) ? $data['file_size'] : null;
38+
39+
}
40+
41+
public function getFileId()
42+
{
43+
return $this->file_id;
44+
}
45+
46+
public function getiDuration()
47+
{
48+
return $this->duration;
49+
}
50+
51+
public function getMimeType()
52+
{
53+
return $this->mime_type;
54+
}
55+
56+
public function getFileSize()
57+
{
58+
return $this->file_size;
59+
}
60+
}

src/Entities/Chat.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ public function __construct(array $data)
3434
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
3535
$this->username = isset($data['username']) ? $data['username'] : null;
3636
}
37+
public function isGroupChat()
38+
{
39+
if ($this->id < 0) {
40+
return true;
41+
} else {
42+
return false;
43+
}
44+
}
3745

46+
public function isSingleChat()
47+
{
48+
return ! $this->isGroupChat();
49+
}
3850
public function getId()
3951
{
4052

src/Entities/Contact.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Longman\TelegramBot\Entities;
12+
13+
use Longman\TelegramBot\Exception\TelegramException;
14+
15+
class Contact extends Entity
16+
{
17+
18+
protected $phone_number;
19+
protected $first_name;
20+
protected $last_name;
21+
protected $user_id;
22+
23+
public function __construct(array $data)
24+
{
25+
26+
$this->phone_number = isset($data['phone_number']) ? $data['phone_number'] : null;
27+
if (empty($this->phone_number)) {
28+
throw new TelegramException('phone_number is empty!');
29+
}
30+
31+
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
32+
if (empty($this->first_name)) {
33+
throw new TelegramException('first_name is empty!');
34+
}
35+
36+
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
37+
$this->user_id = isset($data['user_id']) ? $data['user_id'] : null;
38+
}
39+
40+
public function getPhoneNumber()
41+
{
42+
return $this->phone_number;
43+
}
44+
45+
public function getFirstName()
46+
{
47+
return $this->first_name;
48+
}
49+
50+
public function getLastName()
51+
{
52+
return $this->last_name;
53+
}
54+
55+
public function getUserId()
56+
{
57+
return $this->user_id;
58+
}
59+
}

0 commit comments

Comments
 (0)