Skip to content

Commit d8a3403

Browse files
committed
More detailed notifications
1 parent 13ac009 commit d8a3403

File tree

1 file changed

+121
-31
lines changed

1 file changed

+121
-31
lines changed

Notification/Telegram.php

Lines changed: 121 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,34 @@
88
use Kanboard\Core\Base;
99
use Kanboard\Core\Notification\NotificationInterface;
1010
use Kanboard\Model\TaskModel;
11+
use Kanboard\Model\SubtaskModel;
12+
use Kanboard\Model\CommentModel;
13+
use Kanboard\Model\TaskFileModel;
1114

1215
/**
1316
* Telegram Notification
1417
*
1518
* @package notification
1619
* @author Manu Varkey
1720
*/
18-
21+
22+
// Helper functions
23+
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+
37+
// Overloaded classes
38+
1939
class Telegram extends Base implements NotificationInterface
2040
{
2141
/**
@@ -70,18 +90,21 @@ public function notifyProject(array $project, $eventName, array $eventData)
7090
}
7191

7292
/**
73-
* Get message to send
93+
* Send message to Telegram
7494
*
75-
* @access public
95+
* @access protected
96+
* @param string $apikey
97+
* @param string $bot_username
98+
* @param string $chat_id
7699
* @param array $project
77100
* @param string $eventName
78101
* @param array $eventData
79-
* @return array
80102
*/
81-
public function getMessage(array $project, $eventName, array $eventData)
103+
protected function sendMessage($apikey, $bot_username, $chat_id, array $project, $eventName, array $eventData)
82104
{
83-
105+
84106
// Get required data
107+
85108
if ($this->userSession->isLogged())
86109
{
87110
$author = $this->helper->user->getFullname();
@@ -92,51 +115,118 @@ public function getMessage(array $project, $eventName, array $eventData)
92115
$title = $this->notificationModel->getTitleWithoutAuthor($eventName, $eventData);
93116
}
94117

118+
$proj_name = isset($eventData['project_name']) ? $eventData['project_name'] : $eventData['task']['project_name'];
119+
$task_title = $eventData['task']['title'];
120+
$task_url = $this->helper->url->to('TaskViewController', 'show', array('task_id' => $eventData['task']['id'], 'project_id' => $project['id']), '', true);
121+
122+
$attachment = '';
123+
95124
// Build message
96-
$message = "\[".(isset($eventData['project_name']) ? $eventData['project_name'] : $eventData['task']['project_name'])."]\n";
97-
$message .= $title."\n";
125+
126+
$message = "[".htmlspecialchars($proj_name, ENT_NOQUOTES | ENT_IGNORE)."]\n";
127+
$message .= htmlspecialchars($title, ENT_NOQUOTES | ENT_IGNORE)."\n";
98128

99129
if ($this->configModel->get('application_url') !== '')
100130
{
101-
$message .= "[".$eventData['task']['title']."](";
102-
$message .= $this->helper->url->to('TaskViewController', 'show', array('task_id' => $eventData['task']['id'], 'project_id' => $project['id']), '', true);
103-
$message .= ")";
131+
$message .= '📝 <a href="'.$task_url.'">'.htmlspecialchars($task_title, ENT_NOQUOTES | ENT_IGNORE).'</a>';
104132
}
105133
else
106134
{
107-
$message .= $eventData['task']['title'];
135+
$message .= htmlspecialchars($task_title, ENT_NOQUOTES | ENT_IGNORE);
108136
}
109137

110-
// Return message array
111-
return $message;
112-
}
113-
114-
/**
115-
* Send message to Telegram
116-
*
117-
* @access protected
118-
* @param string $chat_id
119-
* @param array $project
120-
* @param string $eventName
121-
* @param array $eventData
122-
*/
123-
protected function sendMessage($apikey, $bot_username, $chat_id, array $project, $eventName, array $eventData)
124-
{
125-
$message = $this->getMessage($project, $eventName, $eventData);
126-
$data = array('chat_id' => $chat_id, 'text' => $message, 'parse_mode' => 'Markdown');
138+
// Add additional informations
127139

128-
try
140+
$description_events = array(TaskModel::EVENT_CREATE, TaskModel::EVENT_UPDATE, TaskModel::EVENT_USER_MENTION);
141+
$subtask_events = array(SubtaskModel::EVENT_CREATE, SubtaskModel::EVENT_UPDATE, SubtaskModel::EVENT_DELETE);
142+
$comment_events = array(CommentModel::EVENT_UPDATE, CommentModel::EVENT_CREATE, CommentModel::EVENT_DELETE, CommentModel::EVENT_USER_MENTION);
143+
144+
if (in_array($eventName, $subtask_events)) // If description available
145+
{
146+
$subtask_status = $eventData['subtask']['status'];
147+
$subtask_symbol = '';
148+
149+
if ($subtask_status == SubtaskModel::STATUS_DONE)
150+
{
151+
$subtask_symbol = '[X] ';
152+
}
153+
elseif ($subtask_status == SubtaskModel::STATUS_TODO)
154+
{
155+
$subtask_symbol = '[ ] ';
156+
}
157+
elseif ($subtask_status == SubtaskModel::STATUS_INPROGRESS)
158+
{
159+
$subtask_symbol = '[~] ';
160+
}
161+
162+
$message .= "\n<b> ↳ ".$subtask_symbol.'</b> <em>"'.htmlspecialchars($eventData['subtask']['title'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
163+
}
164+
165+
elseif (in_array($eventName, $description_events)) // For subtasks available
166+
{
167+
$message .= "\n✏️ ".'<em>"'.htmlspecialchars($eventData['task']['description'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
168+
}
169+
170+
elseif (in_array($eventName, $comment_events)) // If comment available
171+
{
172+
$message .= "\n💬 ".'<em>"'.htmlspecialchars($eventData['comment']['comment'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
173+
}
174+
175+
elseif ($eventName === TaskFileModel::EVENT_CREATE) // If attachment available
129176
{
177+
$file_path = getcwd()."/data/files/".$eventData['file']['path'];
178+
$file_name = $eventData['file']['name'];
179+
$is_image = $eventData['file']['is_image'];
180+
181+
$attachment = tempnam_sfx(sys_get_temp_dir(), $file_name);
182+
file_put_contents($attachment, file_get_contents($file_path));
183+
}
184+
185+
// Send Message
186+
187+
try
188+
{
189+
130190
// Create Telegram API object
131191
$telegram = new TelegramClass($apikey, $bot_username);
132192

193+
// Message pay load
194+
$data = array('chat_id' => $chat_id, 'text' => $message, 'parse_mode' => 'HTML');
195+
133196
// Send message
134197
$result = Request::sendMessage($data);
198+
199+
// Send any attachment if exists
200+
if ($attachment != '')
201+
{
202+
if ($is_image == true)
203+
{
204+
// Sent image
205+
$data_file = ['chat_id' => $chat_id,
206+
'photo' => Request::encodeFile($attachment),
207+
'caption' => '📎 '.$file_name,
208+
];
209+
$result_att = Request::sendPhoto($data_file);
210+
}
211+
else
212+
{
213+
// Sent attachment
214+
$data_file = ['chat_id' => $chat_id,
215+
'document' => Request::encodeFile($attachment),
216+
'caption' => '📎 '.$file_name,
217+
];
218+
$result_att = Request::sendDocument($data_file);
219+
}
220+
221+
// Remove temporory file
222+
unlink($attachment);
223+
}
135224
}
136225
catch (TelegramException $e)
137226
{
138227
// log telegram errors
139-
// echo $e->getMessage();
228+
error_log($e->getMessage());
140229
}
141230
}
142231
}
232+

0 commit comments

Comments
 (0)