Skip to content

Commit 7283a1d

Browse files
committed
Introducing chats command
1 parent 8892a60 commit 7283a1d

File tree

5 files changed

+157
-29
lines changed

5 files changed

+157
-29
lines changed

src/Admin/ChatsCommand.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramBot\Commands;
11+
12+
use Longman\TelegramBot\Request;
13+
use Longman\TelegramBot\DB;
14+
use Longman\TelegramBot\Command;
15+
use Longman\TelegramBot\Entities\Update;
16+
use Longman\TelegramBot\Entities\Chat;
17+
use Longman\TelegramBot\Exception\TelegramException;
18+
19+
class ChatsCommand extends Command
20+
{
21+
protected $name = 'chats';
22+
protected $description = 'List all chats stored by the bot';
23+
protected $usage = '/chats ';
24+
protected $version = '1.0.0';
25+
protected $enabled = true;
26+
protected $public = true;
27+
//need Mysql
28+
protected $need_mysql = true;
29+
30+
public function executeNoDB()
31+
{
32+
//Database not setted or without connection
33+
//Preparing message
34+
$message = $this->getMessage();
35+
$chat_id = $message->getChat()->getId();
36+
$data = array();
37+
$data['chat_id'] = $chat_id;
38+
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
39+
return Request::sendMessage($data);
40+
}
41+
42+
public function execute()
43+
{
44+
$update = $this->getUpdate();
45+
$message = $this->getMessage();
46+
47+
$chat_id = $message->getChat()->getId();
48+
$message_id = $message->getMessageId();
49+
$text = $message->getText(true);
50+
51+
$results = DB::selectChats(
52+
true, //Send to chats (group chat)
53+
true, //Send to users (single chat)
54+
null, //'yyyy-mm-dd hh:mm:ss' date range from
55+
null //'yyyy-mm-dd hh:mm:ss' date range to
56+
);
57+
58+
$user_chats = 0;
59+
$group_chats = 0;
60+
$text = "List of bot chats:\n";
61+
62+
foreach ($results as $result) {
63+
//I want initialize a chat object
64+
//id, title, first_name, last_name
65+
$result['id'] = $result['chat_id'];
66+
67+
$chat = new Chat($result);
68+
69+
if ($chat->isSingleChat()) {
70+
//$text .= '- U '.$chat->getFirstName()."\n";
71+
72+
$text .= '- U '.$this->tryMentionChat($chat)."\n";
73+
++$user_chats;
74+
} else {
75+
$text .= '- G '.$chat->getTitle()."\n";
76+
++$group_chats;
77+
}
78+
79+
}
80+
if (($group_chats + $user_chats) == 0) {
81+
$text = "No chats found..";
82+
} else {
83+
$text .= "\nUser Chats: ".$user_chats;
84+
$text .= "\nGroup Chats: ".$group_chats;
85+
$text .= "\nTot: ".($group_chats + $user_chats);
86+
}
87+
88+
$data = [];
89+
$data['chat_id'] = $chat_id;
90+
//$data['reply_to_message_id'] = $message_id;
91+
$data['text'] = $text;
92+
//$data['parse_mode'] = 'Markdown';
93+
$result = Request::sendMessage($data);
94+
return $result;
95+
}
96+
}

src/Admin/SendtoallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function execute()
5151
if (empty($text)) {
5252
$text = 'Write te message to sent: /sendall <message>';
5353
} else {
54-
$results = DB::sendToActiveChats(
54+
$results = Request::sendToActiveChats(
5555
'sendMessage', //callback function to execute (see Request.php methods)
5656
array('text'=> $text), //Param to evaluate the request
5757
true, //Send to chats (group chat)

src/Command.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Longman\TelegramBot\Entities\Update;
1414
use Longman\TelegramBot\Entities\User;
15+
use Longman\TelegramBot\Entities\Chat;
1516

1617
abstract class Command
1718
{
@@ -132,4 +133,17 @@ public function tryMention(User $user)
132133
return $user->getFirstName();
133134
}
134135
}
136+
137+
public function tryMentionChat(Chat $chat)
138+
{
139+
if ($chat->isGroupChat()) {
140+
return $chat->getTitle();
141+
} else {
142+
if (!is_null($chat->getUsername())) {
143+
return '@'.$chat->getUsername();
144+
} else {
145+
return $chat->getFirstName();
146+
}
147+
}
148+
}
135149
}

src/DB.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public static function insertRequest(Update $update)
333333
$sth->bindParam(':text', $text, \PDO::PARAM_STR);
334334
$sth->bindParam(':audio', $audio, \PDO::PARAM_STR);
335335
$sth->bindParam(':document', $document, \PDO::PARAM_STR);
336-
//Try with to magic shoud work
336+
337337
$var = [];
338338
if (is_array($photo)) {
339339
foreach ($photo as $elm) {
@@ -383,48 +383,40 @@ public static function insertRequest(Update $update)
383383

384384

385385
/**
386-
* Send Message in all the active chat
386+
* Select Group and single Chats
387387
*
388388
* @param date string yyyy-mm-dd hh:mm:ss
389389
*
390-
* @return bool
390+
* @return array selected rows
391391
*/
392-
//TODO separe send from query?
393-
public static function sendToActiveChats(
394-
$callback_function,
395-
array $data,
396-
$send_chats = true,
397-
$send_users = true,
392+
public static function selectChats(
393+
$select_chats = true,
394+
$select_users = true,
398395
$date_from = null,
399396
$date_to = null
400397
) {
401398
if (!self::isDbConnected()) {
402399
return false;
403400
}
404401

405-
$callback_path = __NAMESPACE__ .'\Request';
406-
if (! method_exists($callback_path, $callback_function)) {
407-
throw new TelegramException('Methods: '.$callback_function.' not found in class Request.');
408-
}
409-
410-
if (!$send_chats & !$send_users) {
402+
if (!$select_chats & !$select_users) {
411403
return false;
412404
}
413-
414405
try {
415406
$query = 'SELECT * ,
416407
'.TB_CHATS.'.`id` AS `chat_id`,
417-
'.TB_CHATS.'.`updated_at` AS `chat_updated_at`
408+
'.TB_CHATS.'.`updated_at` AS `chat_updated_at`,
409+
'.TB_USERS.'.`id` AS `user_id`
418410
FROM `'.TB_CHATS.'` LEFT JOIN `'.TB_USERS.'`
419411
ON '.TB_CHATS.'.`id`='.TB_USERS.'.`id`';
420412

421413
//Building parts of query
422414
$chat_or_user = '';
423415
$where = [];
424416
$tokens = [];
425-
if ($send_chats & !$send_users) {
417+
if ($select_chats & !$select_users) {
426418
$where[] = TB_CHATS.'.`id` < 0';
427-
} elseif (!$send_chats & $send_users) {
419+
} elseif (!$select_chats & $select_users) {
428420
$where[] = TB_CHATS.'.`id` > 0';
429421
}
430422

@@ -454,18 +446,11 @@ public static function sendToActiveChats(
454446
$sth = self::$pdo->prepare($query);
455447
$sth->execute($tokens);
456448

457-
$results = [];
458-
while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
459-
//$result[] = $row;
460-
//print_r($row);
461-
$data['chat_id'] = $row['chat_id'];
462-
$results[] = call_user_func_array($callback_path.'::'.$callback_function, array($data));
463-
}
449+
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
464450

465451
} catch (PDOException $e) {
466452
throw new TelegramException($e->getMessage());
467453
}
468-
469-
return $results;
454+
return $result;
470455
}
471456
}

src/Request.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,37 @@ public static function setWebhook($url)
242242
}
243243

244244
//getFile
245+
246+
/**
247+
* Send Message in all the active chat
248+
*
249+
*
250+
* @return bool
251+
*/
252+
public static function sendToActiveChats(
253+
$callback_function,
254+
array $data,
255+
$send_chats = true,
256+
$send_users = true,
257+
$date_from = null,
258+
$date_to = null
259+
) {
260+
261+
$callback_path = __NAMESPACE__ .'\Request';
262+
if (! method_exists($callback_path, $callback_function)) {
263+
throw new TelegramException('Methods: '.$callback_function.' not found in class Request.');
264+
}
265+
266+
$chats = DB::selectChats($send_chats, $send_users, $date_from, $date_to);
267+
268+
$results = [];
269+
foreach ($chats as $row) {
270+
//$result[] = $row;
271+
//print_r($row);
272+
$data['chat_id'] = $row['chat_id'];
273+
$results[] = call_user_func_array($callback_path.'::'.$callback_function, array($data));
274+
}
275+
276+
return $results;
277+
}
245278
}

0 commit comments

Comments
 (0)