Skip to content

Commit d8103c3

Browse files
committed
introduced command channel
1 parent 7c33875 commit d8103c3

File tree

5 files changed

+75
-28
lines changed

5 files changed

+75
-28
lines changed

src/Admin/ChatsCommand.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,12 @@ public function execute()
6060
$text = "List of bot chats:\n";
6161

6262
foreach ($results as $result) {
63-
//I want initialize a chat object
64-
//id, title, first_name, last_name
63+
//initialize a chat object
6564
$result['id'] = $result['chat_id'];
6665

6766
$chat = new Chat($result);
6867

6968
if ($chat->isPrivateChat()) {
70-
//$text .= '- U '.$chat->getFirstName()."\n";
71-
7269
$text .= '- U '.$this->tryMentionChat($chat)."\n";
7370
++$user_chats;
7471
} else {
@@ -87,9 +84,7 @@ public function execute()
8784

8885
$data = [];
8986
$data['chat_id'] = $chat_id;
90-
//$data['reply_to_message_id'] = $message_id;
9187
$data['text'] = $text;
92-
//$data['parse_mode'] = 'Markdown';
9388
$result = Request::sendMessage($data);
9489
return $result;
9590
}

src/Admin/SendtoallCommand.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function executeNoDB()
3333
//Preparing message
3434
$message = $this->getMessage();
3535
$chat_id = $message->getChat()->getId();
36-
$data = array();
36+
$data = [];
3737
$data['chat_id'] = $chat_id;
3838
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
3939
return Request::sendMessage($data);
@@ -49,7 +49,7 @@ public function execute()
4949
$text = $message->getText(true);
5050

5151
if (empty($text)) {
52-
$text = 'Write te message to sent: /sendall <message>';
52+
$text = 'Write the message to sent: /sendall <message>';
5353
} else {
5454
$results = Request::sendToActiveChats(
5555
'sendMessage', //callback function to execute (see Request.php methods)
@@ -94,9 +94,8 @@ public function execute()
9494
$text = "No users or chats found..";
9595
}
9696

97-
$data = array();
97+
$data = [];
9898
$data['chat_id'] = $chat_id;
99-
//$data['reply_to_message_id'] = $message_id;
10099
$data['text'] = $text;
101100

102101
$result = Request::sendMessage($data);

src/Admin/SendtochannelCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
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\DB;
15+
use Longman\TelegramBot\Command;
16+
use Longman\TelegramBot\Entities\Update;
17+
use Longman\TelegramBot\Exception\TelegramException;
18+
19+
class SendtochannelCommand extends Command
20+
{
21+
protected $name = 'sendtochannel';
22+
protected $description = 'Send a message to a channel';
23+
protected $usage = '/sendchannel <message to send>';
24+
protected $version = '0.1.0';
25+
protected $enabled = true;
26+
protected $public = true;
27+
//need Mysql
28+
protected $need_mysql = false;
29+
30+
public function execute()
31+
{
32+
$update = $this->getUpdate();
33+
$message = $this->getMessage();
34+
35+
$chat_id = $message->getChat()->getId();
36+
$message_id = $message->getMessageId();
37+
$text = $message->getText(true);
38+
$your_channel = '@yourchannel';
39+
if (empty($text)) {
40+
$text_back = 'Write the message to sent: /sendtochannel <message>';
41+
} else {
42+
//Send message to channel
43+
$data = [];
44+
$data['chat_id'] = $your_channel;
45+
$data['text'] = $text;
46+
47+
$result = Request::sendMessage($data);
48+
if ($result->isOk()) {
49+
$text_back = 'Message sent succesfully to: '.$your_channel;
50+
} else {
51+
$text_back = 'Sorry message not sent to: '.$your_channel;
52+
}
53+
}
54+
55+
$data = [];
56+
$data['chat_id'] = $chat_id;
57+
$data['text'] = $text_back;
58+
59+
$result = Request::sendMessage($data);
60+
return $result;
61+
}
62+
}

src/Entities/Message.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ protected function init(array & $data, & $bot_name)
8989
}
9090

9191
$this->from = isset($data['from']) ? $data['from'] : null;
92-
if (empty($this->from)) {
93-
throw new TelegramException('from is empty!');
92+
if (!empty($this->from)) {
93+
$this->from = new User($this->from);
9494
}
95-
$this->from = new User($this->from);
9695

9796
$this->chat = isset($data['chat']) ? $data['chat'] : null;
9897
if (empty($this->chat)) {

src/Request.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ private static function log($string)
7777
return false;
7878
}
7979

80-
//$status = file_put_contents($path, self::$input . "\n", FILE_APPEND);
8180
$status = file_put_contents($path, $string . "\n", FILE_APPEND);
8281
return $status;
8382
}
@@ -125,24 +124,18 @@ public static function executeCurl($action, array $data = null)
125124
$curlConfig = array(
126125
CURLOPT_URL => 'https://api.telegram.org/bot' . self::$telegram->getApiKey() . '/' . $action,
127126
CURLOPT_POST => true,
128-
CURLOPT_RETURNTRANSFER => true
127+
CURLOPT_RETURNTRANSFER => true,
128+
CURLOPT_SAFE_UPLOAD => true
129129
);
130130

131131
if (!empty($data)) {
132-
if (!empty($data['text']) && substr($data['text'], 0, 1) === '@') {
133-
$data['text'] = ' ' . $data['text'];
134-
}
135132
$curlConfig[CURLOPT_POSTFIELDS] = $data;
136133
}
137134

138135
if (self::$telegram->getLogVerbosity() >= 3) {
139136
$curlConfig[CURLOPT_VERBOSE] = true;
140137
$verbose = fopen('php://temp', 'w+');
141138
curl_setopt($ch, CURLOPT_STDERR, $verbose);
142-
//Not so useful
143-
//$info = curl_getinfo($ch);
144-
//echo "Info\n";
145-
//print_r($info);
146139
}
147140

148141
curl_setopt_array($ch, $curlConfig);
@@ -189,7 +182,7 @@ public static function downloadFile(File $file)
189182
throw new TelegramException('Directory '.$dirname.' cant be created');
190183
}
191184
}
192-
# open file to write
185+
// open file to write
193186
$fp = fopen($loc_path, 'w+');
194187
if ($fp === false) {
195188
throw new TelegramException('File cant be created');
@@ -214,9 +207,9 @@ public static function downloadFile(File $file)
214207
if ($result === false) {
215208
throw new TelegramException(curl_error($ch), curl_errno($ch));
216209
}
217-
# close curl
210+
// close curl
218211
curl_close($ch);
219-
# close local file
212+
// close local file
220213
fclose($fp);
221214

222215
if (filesize($loc_path) > 0) {
@@ -226,7 +219,6 @@ public static function downloadFile(File $file)
226219
}
227220
}
228221

229-
230222
protected static function encodeFile($file)
231223
{
232224
return new \CURLFile($file);
@@ -421,12 +413,14 @@ public static function getFile($data)
421413
$result = self::send('getFile', $data);
422414
return $result;
423415
}
416+
424417
/**
425418
* Send Message in all the active chat
426419
*
427420
*
428421
* @return bool
429422
*/
423+
430424
public static function sendToActiveChats(
431425
$callback_function,
432426
array $data,
@@ -445,8 +439,6 @@ public static function sendToActiveChats(
445439

446440
$results = [];
447441
foreach ($chats as $row) {
448-
//$result[] = $row;
449-
//print_r($row);
450442
$data['chat_id'] = $row['chat_id'];
451443
$results[] = call_user_func_array($callback_path.'::'.$callback_function, array($data));
452444
}

0 commit comments

Comments
 (0)