Skip to content

Commit 9ebe48a

Browse files
committed
Bug fixes, code cleanup
1 parent b240160 commit 9ebe48a

File tree

6 files changed

+126
-129
lines changed

6 files changed

+126
-129
lines changed

Controller/TelegramController.php

Lines changed: 100 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Kanboard\Plugin\Telegram\Controller;
43

54
use Kanboard\Controller\BaseController;
@@ -10,142 +9,144 @@
109
use Longman\TelegramBot\Exception\TelegramException;
1110
use Kanboard\Core\Base;
1211

13-
1412
class TelegramController extends BaseController
1513
{
1614
public function get_user_chat_id()
1715
{
1816
$user = $this->getUser();
1917
//$this->checkCSRFParam();
20-
2118
$apikey = $this->userMetadataModel->get($user['id'], 'telegram_apikey', $this->configModel->get('telegram_apikey'));
2219
$bot_username = $this->userMetadataModel->get($user['id'], 'telegram_username', $this->configModel->get('telegram_username'));
23-
$offset = 0+$this->userMetadataModel->get($user['id'], 'telegram_offset', $this->configModel->get('telegram_offset'));
24-
$private_message = mb_substr(urldecode($this->request->getStringParam('private_message')),0,32);
20+
$offset = 0 + $this->userMetadataModel->get($user['id'], 'telegram_offset', $this->configModel->get('telegram_offset'));
21+
$private_message = mb_substr(urldecode($this->request->getStringParam('private_message')), 0, 32);
2522

2623
list($offset, $chat_id, $user_name) = $this->get_chat_id($apikey, $bot_username, $offset, $private_message);
2724

28-
if($offset != 0){
25+
if ($offset != 0)
26+
{
2927
//ok
30-
$this->userMetadataModel->save($user['id'], array('telegram_offset' => $offset) );
31-
$this->response->html($this->template->render('telegram:user/save_chat_id', array(
32-
'chat_id' => $chat_id,
33-
'user_name' => $user_name,
34-
'private_message' => $private_message,
35-
'bot_url' => "https://t.me/".$bot_username,
36-
'user' => $user
37-
)));
38-
}else{
28+
$this->userMetadataModel->save($user['id'], array('telegram_offset' => $offset));
29+
$this->response->html($this->template->render('telegram:user/save_chat_id', array('chat_id' => $chat_id, 'user_name' => $user_name, 'private_message' => $private_message, 'bot_url' => "https://t.me/" . $bot_username, 'user' => $user)));
30+
}
31+
else
32+
{
3933
//error
40-
$this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true);
34+
$this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'])), true);
4135
}
4236
}
4337

4438
public function get_project_chat_id()
4539
{
4640
$project = $this->getProject();
4741
//$this->checkCSRFParam();
48-
4942
$apikey = $this->projectMetadataModel->get($project['id'], 'telegram_apikey', $this->configModel->get('telegram_apikey'));
5043
$bot_username = $this->projectMetadataModel->get($project['id'], 'telegram_username', $this->configModel->get('telegram_username'));
51-
$offset = 0+$this->projectMetadataModel->get($project['id'], 'telegram_offset', $this->configModel->get('telegram_offset'));
52-
$private_message = mb_substr(urldecode($this->request->getStringParam('private_message')),0,32);
44+
$offset = 0 + $this->projectMetadataModel->get($project['id'], 'telegram_offset', $this->configModel->get('telegram_offset'));
45+
$private_message = mb_substr(urldecode($this->request->getStringParam('private_message')), 0, 32);
5346

5447
list($offset, $chat_id, $user_name) = $this->get_chat_id($apikey, $bot_username, $offset, $private_message);
5548

56-
if($offset != 0){
49+
if ($offset != 0)
50+
{
5751
//ok
58-
$this->projectMetadataModel->save($project['id'], array('telegram_offset' => $offset) );
59-
$this->response->html($this->template->render('telegram:project/save_chat_id', array(
60-
'chat_id' => $chat_id,
61-
'user_name' => $user_name,
62-
'private_message' => $private_message,
63-
'bot_url' => "https://t.me/".$bot_username,
64-
'project' => $project
65-
)));
66-
}else{
52+
$this->projectMetadataModel->save($project['id'], array('telegram_offset' => $offset));
53+
$this->response->html($this->template->render('telegram:project/save_chat_id', array('chat_id' => $chat_id, 'user_name' => $user_name, 'private_message' => $private_message, 'bot_url' => "https://t.me/" . $bot_username, 'project' => $project)));
54+
}
55+
else
56+
{
6757
//error
68-
$this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'] )), true);
58+
$this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'])), true);
6959
}
7060
}
7161

7262
private function get_chat_id($apikey, $bot_username, $offset, $private_message)
7363
{
74-
try
75-
{
76-
if(empty($private_message) || mb_strlen($private_message) != 32){
77-
throw new TelegramException("empty private_message!");
78-
}
64+
try
65+
{
66+
if (empty($private_message) || mb_strlen($private_message) != 32)
67+
{
68+
throw new TelegramException("empty private_message!");
69+
}
7970

80-
// Create Telegram API object
81-
$telegram = new TelegramClass($apikey, $bot_username);
82-
83-
$limit=100;
84-
$timeout = 1;
85-
$response = Request::getUpdates(
86-
[
87-
'offset' => $offset+1,
88-
'limit' => $limit,
89-
'timeout' => $timeout,
90-
]
91-
);
92-
93-
$chat_id="";
94-
$user_name="";
95-
96-
if ($response->isOk()) {
97-
//Process all updates
98-
/** @var Update $result */
99-
foreach ((array) $response->getResult() as $result) {
100-
$offset = $result->getUpdateId();
101-
if( $result->getMessage() != NULL){
102-
if( $private_message === mb_substr(trim($result->getMessage()->getText()),0,32) ){
103-
$chat_id = $result->getMessage()->getChat()->getId();
104-
$user_name = $result->getMessage()->getChat()->getFirstName();
105-
break;
106-
}
71+
// Create Telegram API object
72+
$telegram = new TelegramClass($apikey, $bot_username);
73+
74+
$limit = 100;
75+
$timeout = 1;
76+
$response = Request::getUpdates(['offset' => $offset + 1, 'limit' => $limit, 'timeout' => $timeout, ]);
77+
78+
$chat_id = "";
79+
$user_name = "";
80+
81+
if ($response->isOk())
82+
{
83+
//Process all updates
84+
85+
/** @var Update $result */
86+
foreach ((array)$response->getResult() as $result)
87+
{
88+
$offset = $result->getUpdateId();
89+
if ($result->getMessage() != NULL)
90+
{
91+
if ($private_message === mb_substr(trim($result->getMessage()->getText()), 0, 32))
92+
{
93+
$chat_id = $result->getMessage()->getChat()->getId();
94+
$user_name = $result->getMessage()->getChat()->getFirstName();
95+
break;
96+
}
97+
}
98+
}
99+
}
100+
else
101+
{
102+
throw new TelegramException($response->printError(true));
107103
}
108-
}
109-
}else{
110-
throw new TelegramException($response->printError(true));
111104
}
112-
}
113-
catch (TelegramException $e)
114-
{
115-
// log telegram errors
116-
error_log($e->getMessage());
117-
$this->flash->failure(t('Telegram error: ').$e->getMessage());
118-
return 0;//$this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true);
119-
}
120-
121-
return array($offset, $chat_id, $user_name);
105+
catch(TelegramException $e)
106+
{
107+
// log telegram errors
108+
error_log($e->getMessage());
109+
$this->flash->failure(t('Telegram error: ') . $e->getMessage());
110+
return 0; //$this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true);
111+
112+
}
113+
114+
return array($offset, $chat_id, $user_name);
122115
}
123116

124-
public function save_user_chat_id(){
125-
$user = $this->getUser();
126-
$this->checkCSRFParam();
127-
128-
$chat_id = urldecode($this->request->getStringParam('chat_id'));
129-
if(is_numeric($chat_id)){
130-
$this->userMetadataModel->save($user['id'], array('telegram_user_cid' => $chat_id) );
131-
$this->flash->success(t("Chat id was updated to %s",$chat_id));
132-
}else{
133-
$this->flash->failure(t('Telegram error: wrong chat id'));
134-
}
135-
return $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'] )), true);
117+
public function save_user_chat_id()
118+
{
119+
$user = $this->getUser();
120+
$this->checkCSRFParam();
121+
122+
$chat_id = urldecode($this->request->getStringParam('chat_id'));
123+
if (is_numeric($chat_id))
124+
{
125+
$this->userMetadataModel->save($user['id'], array('telegram_user_cid' => $chat_id));
126+
$this->flash->success(t("Chat id was updated to %s", $chat_id));
127+
}
128+
else
129+
{
130+
$this->flash->failure(t('Telegram error: wrong chat id'));
131+
}
132+
return $this->response->redirect($this->helper->url->to('UserViewController', 'integrations', array('user_id' => $user['id'])), true);
136133
}
137134

138-
public function save_project_chat_id(){
139-
$project = $this->getProject();
140-
$this->checkCSRFParam();
141-
142-
$chat_id = urldecode($this->request->getStringParam('chat_id'));
143-
if(is_numeric($chat_id)){
144-
$this->projectMetadataModel->save($project['id'], array('telegram_group_cid' => $chat_id) );
145-
$this->flash->success(t("Chat id was updated to %s",$chat_id));
146-
}else{
147-
$this->flash->failure(t('Telegram error: wrong chat id'));
148-
}
149-
return $this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'] )), true);
135+
public function save_project_chat_id()
136+
{
137+
$project = $this->getProject();
138+
$this->checkCSRFParam();
139+
140+
$chat_id = urldecode($this->request->getStringParam('chat_id'));
141+
if (is_numeric($chat_id))
142+
{
143+
$this->projectMetadataModel->save($project['id'], array('telegram_group_cid' => $chat_id));
144+
$this->flash->success(t("Chat id was updated to %s", $chat_id));
145+
}
146+
else
147+
{
148+
$this->flash->failure(t('Telegram error: wrong chat id'));
149+
}
150+
return $this->response->redirect($this->helper->url->to('ProjectViewController', 'integrations', array('project_id' => $project['id'])), true);
150151
}
151152
}

Notification/Telegram.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,10 @@
2121

2222
// Helper functions
2323

24-
function tempnam_sfx($path, $suffix)
25-
{
26-
do
27-
{
28-
$file = $path."/".mt_rand().$suffix;
29-
$fp = @fopen($file, 'x');
30-
}
31-
while(!$fp);
32-
33-
fclose($fp);
34-
return $file;
35-
}
36-
3724
function clean($string)
3825
{
3926
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
40-
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
27+
return preg_replace('/[^A-Za-z0-9\-.]/', '', $string); // Removes special chars.
4128
}
4229

4330
// Overloaded classes
@@ -57,6 +44,8 @@ public function notifyUser(array $user, $eventName, array $eventData)
5744
$apikey = $this->userMetadataModel->get($user['id'], 'telegram_apikey', $this->configModel->get('telegram_apikey'));
5845
$bot_username = $this->userMetadataModel->get($user['id'], 'telegram_username', $this->configModel->get('telegram_username'));
5946
$chat_id = $this->userMetadataModel->get($user['id'], 'telegram_user_cid');
47+
$forward_attachments = $this->userMetadataModel->get($user['id'], 'forward_attachments', $this->configModel->get('forward_attachments'));
48+
6049
if (! empty($apikey))
6150
{
6251
if ($eventName === TaskModel::EVENT_OVERDUE)
@@ -65,13 +54,13 @@ public function notifyUser(array $user, $eventName, array $eventData)
6554
{
6655
$project = $this->projectModel->getById($task['project_id']);
6756
$eventData['task'] = $task;
68-
$this->sendMessage($apikey, $bot_username, $chat_id, $project, $eventName, $eventData);
57+
$this->sendMessage($apikey, $bot_username, $forward_attachments, $chat_id, $project, $eventName, $eventData);
6958
}
7059
}
7160
else
7261
{
7362
$project = $this->projectModel->getById($eventData['task']['project_id']);
74-
$this->sendMessage($apikey, $bot_username, $chat_id, $project, $eventName, $eventData);
63+
$this->sendMessage($apikey, $bot_username, $forward_attachments, $chat_id, $project, $eventName, $eventData);
7564
}
7665
}
7766
}
@@ -89,9 +78,11 @@ public function notifyProject(array $project, $eventName, array $eventData)
8978
$apikey = $this->projectMetadataModel->get($project['id'], 'telegram_apikey', $this->configModel->get('telegram_apikey'));
9079
$bot_username = $this->projectMetadataModel->get($project['id'], 'telegram_username', $this->configModel->get('telegram_username'));
9180
$chat_id = $this->projectMetadataModel->get($project['id'], 'telegram_group_cid');
81+
$forward_attachments = $this->userMetadataModel->get($project['id'], 'forward_attachments', $this->configModel->get('forward_attachments'));
82+
9283
if (! empty($apikey))
9384
{
94-
$this->sendMessage($apikey, $bot_username, $chat_id, $project, $eventName, $eventData);
85+
$this->sendMessage($apikey, $bot_username, $forward_attachments, $chat_id, $project, $eventName, $eventData);
9586
}
9687
}
9788

@@ -106,7 +97,7 @@ public function notifyProject(array $project, $eventName, array $eventData)
10697
* @param string $eventName
10798
* @param array $eventData
10899
*/
109-
protected function sendMessage($apikey, $bot_username, $chat_id, array $project, $eventName, array $eventData)
100+
protected function sendMessage($apikey, $bot_username, $forward_attachments, $chat_id, array $project, $eventName, array $eventData)
110101
{
111102

112103
// Get required data
@@ -154,15 +145,15 @@ protected function sendMessage($apikey, $bot_username, $chat_id, array $project,
154145

155146
if ($subtask_status == SubtaskModel::STATUS_DONE)
156147
{
157-
$subtask_symbol = '[X] ';
148+
$subtask_symbol = ' ';
158149
}
159150
elseif ($subtask_status == SubtaskModel::STATUS_TODO)
160151
{
161-
$subtask_symbol = '[ ] ';
152+
$subtask_symbol = '';
162153
}
163154
elseif ($subtask_status == SubtaskModel::STATUS_INPROGRESS)
164155
{
165-
$subtask_symbol = '[~] ';
156+
$subtask_symbol = '🕘 ';
166157
}
167158

168159
$message .= "\n<b> ↳ ".$subtask_symbol.'</b> <em>"'.htmlspecialchars($eventData['subtask']['title'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
@@ -181,13 +172,14 @@ protected function sendMessage($apikey, $bot_username, $chat_id, array $project,
181172
$message .= "\n💬 ".'<em>"'.htmlspecialchars($eventData['comment']['comment'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
182173
}
183174

184-
elseif ($eventName === TaskFileModel::EVENT_CREATE) // If attachment available
175+
elseif ($eventName === TaskFileModel::EVENT_CREATE and $forward_attachments) // If attachment available
185176
{
186177
$file_path = getcwd()."/data/files/".$eventData['file']['path'];
187178
$file_name = $eventData['file']['name'];
188179
$is_image = $eventData['file']['is_image'];
189180

190-
$attachment = tempnam_sfx(sys_get_temp_dir(), clean($file_name));
181+
mkdir(sys_get_temp_dir()."/kanboard_telegram_plugin");
182+
$attachment = sys_get_temp_dir()."/kanboard_telegram_plugin/".clean($file_name);
191183
file_put_contents($attachment, file_get_contents($file_path));
192184
}
193185

@@ -229,9 +221,10 @@ protected function sendMessage($apikey, $bot_username, $chat_id, array $project,
229221

230222
// Remove temporory file
231223
unlink($attachment);
224+
rmdir(sys_get_temp_dir()."/kanboard_telegram_plugin");
232225
}
233-
}
234-
catch (TelegramException $e)
226+
}
227+
catch (TelegramException $e)
235228
{
236229
// log telegram errors
237230
error_log($e->getMessage());

Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getPluginAuthor()
4242

4343
public function getPluginVersion()
4444
{
45-
return '1.2.0';
45+
return '1.3.0';
4646
}
4747

4848
public function getPluginHomepage()

Template/config/integration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<p class="form-help"><a href="https://core.telegram.org/bots" target="_blank"><?= t('Help on how to generate a bot') ?></a></p>
1010

11+
<?= $this->form->hidden('forward_attachments', array('forward_attachments' => 0)) ?>
12+
<?= $this->form->checkbox('forward_attachments', t('Sent attachments along with notification'), 1, isset($values['forward_attachments']) && $values['forward_attachments'] == 1) ?>
13+
1114
<div class="form-actions">
1215
<input type="submit" value="<?= t('Save') ?>" class="btn btn-blue"/>
1316
</div>

Template/project/integration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
?>
1313
<p>
1414
<?= t('To get your Telegram chat id,')?><br/>
15-
<?= t('please, send following message %s to',$random )?> <a href="<?= $bot_url ?>" target="_blank" rel="noreferrer"><?= $bot_url ?></a><br/>
16-
<?= t('then press')?> <?= $this->modal->medium('none', t('Get chat id'), 'TelegramController', 'get_project_chat_id',array('plugin' => 'Telegram', 'private_message' => $random,'project_id' => $project['id'] ) ) ?>
15+
<?= t('1. Send the message %s to',$random )?> <a href="<?= $bot_url ?>" target="_blank" rel="noreferrer"><?= $bot_url ?></a><br/>
16+
<?= t('2. Press')?><?= $this->modal->medium('none', t('Get chat id'), 'TelegramController', 'get_project_chat_id',array('plugin' => 'Telegram', 'private_message' => $random,'project_id' => $project['id'] ) ) ?>
1717
</p>
1818
<div class="panel">
1919
<?= $this->form->label(t('Chat id of group chat with bot'), 'telegram_group_cid') ?>

0 commit comments

Comments
 (0)