Skip to content

Commit

Permalink
More detailed notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
manuvarkey committed Jan 5, 2018
1 parent 13ac009 commit d8a3403
Showing 1 changed file with 121 additions and 31 deletions.
152 changes: 121 additions & 31 deletions Notification/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@
use Kanboard\Core\Base;
use Kanboard\Core\Notification\NotificationInterface;
use Kanboard\Model\TaskModel;
use Kanboard\Model\SubtaskModel;
use Kanboard\Model\CommentModel;
use Kanboard\Model\TaskFileModel;

/**
* Telegram Notification
*
* @package notification
* @author Manu Varkey
*/


// Helper functions

function tempnam_sfx($path, $suffix)
{
do
{
$file = $path."/".mt_rand().$suffix;
$fp = @fopen($file, 'x');
}
while(!$fp);

fclose($fp);
return $file;
}

// Overloaded classes

class Telegram extends Base implements NotificationInterface
{
/**
Expand Down Expand Up @@ -70,18 +90,21 @@ public function notifyProject(array $project, $eventName, array $eventData)
}

/**
* Get message to send
* Send message to Telegram
*
* @access public
* @access protected
* @param string $apikey
* @param string $bot_username
* @param string $chat_id
* @param array $project
* @param string $eventName
* @param array $eventData
* @return array
*/
public function getMessage(array $project, $eventName, array $eventData)
protected function sendMessage($apikey, $bot_username, $chat_id, array $project, $eventName, array $eventData)
{

// Get required data

if ($this->userSession->isLogged())
{
$author = $this->helper->user->getFullname();
Expand All @@ -92,51 +115,118 @@ public function getMessage(array $project, $eventName, array $eventData)
$title = $this->notificationModel->getTitleWithoutAuthor($eventName, $eventData);
}

$proj_name = isset($eventData['project_name']) ? $eventData['project_name'] : $eventData['task']['project_name'];
$task_title = $eventData['task']['title'];
$task_url = $this->helper->url->to('TaskViewController', 'show', array('task_id' => $eventData['task']['id'], 'project_id' => $project['id']), '', true);

$attachment = '';

// Build message
$message = "\[".(isset($eventData['project_name']) ? $eventData['project_name'] : $eventData['task']['project_name'])."]\n";
$message .= $title."\n";

$message = "[".htmlspecialchars($proj_name, ENT_NOQUOTES | ENT_IGNORE)."]\n";
$message .= htmlspecialchars($title, ENT_NOQUOTES | ENT_IGNORE)."\n";

if ($this->configModel->get('application_url') !== '')
{
$message .= "[".$eventData['task']['title']."](";
$message .= $this->helper->url->to('TaskViewController', 'show', array('task_id' => $eventData['task']['id'], 'project_id' => $project['id']), '', true);
$message .= ")";
$message .= '📝 <a href="'.$task_url.'">'.htmlspecialchars($task_title, ENT_NOQUOTES | ENT_IGNORE).'</a>';
}
else
{
$message .= $eventData['task']['title'];
$message .= htmlspecialchars($task_title, ENT_NOQUOTES | ENT_IGNORE);
}

// Return message array
return $message;
}

/**
* Send message to Telegram
*
* @access protected
* @param string $chat_id
* @param array $project
* @param string $eventName
* @param array $eventData
*/
protected function sendMessage($apikey, $bot_username, $chat_id, array $project, $eventName, array $eventData)
{
$message = $this->getMessage($project, $eventName, $eventData);
$data = array('chat_id' => $chat_id, 'text' => $message, 'parse_mode' => 'Markdown');
// Add additional informations

try
$description_events = array(TaskModel::EVENT_CREATE, TaskModel::EVENT_UPDATE, TaskModel::EVENT_USER_MENTION);
$subtask_events = array(SubtaskModel::EVENT_CREATE, SubtaskModel::EVENT_UPDATE, SubtaskModel::EVENT_DELETE);
$comment_events = array(CommentModel::EVENT_UPDATE, CommentModel::EVENT_CREATE, CommentModel::EVENT_DELETE, CommentModel::EVENT_USER_MENTION);

if (in_array($eventName, $subtask_events)) // If description available
{
$subtask_status = $eventData['subtask']['status'];
$subtask_symbol = '';

if ($subtask_status == SubtaskModel::STATUS_DONE)
{
$subtask_symbol = '[X] ';
}
elseif ($subtask_status == SubtaskModel::STATUS_TODO)
{
$subtask_symbol = '[ ] ';
}
elseif ($subtask_status == SubtaskModel::STATUS_INPROGRESS)
{
$subtask_symbol = '[~] ';
}

$message .= "\n<b> ↳ ".$subtask_symbol.'</b> <em>"'.htmlspecialchars($eventData['subtask']['title'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
}

elseif (in_array($eventName, $description_events)) // For subtasks available
{
$message .= "\n✏️ ".'<em>"'.htmlspecialchars($eventData['task']['description'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
}

elseif (in_array($eventName, $comment_events)) // If comment available
{
$message .= "\n💬 ".'<em>"'.htmlspecialchars($eventData['comment']['comment'], ENT_NOQUOTES | ENT_IGNORE).'"</em>';
}

elseif ($eventName === TaskFileModel::EVENT_CREATE) // If attachment available
{
$file_path = getcwd()."/data/files/".$eventData['file']['path'];
$file_name = $eventData['file']['name'];
$is_image = $eventData['file']['is_image'];

$attachment = tempnam_sfx(sys_get_temp_dir(), $file_name);
file_put_contents($attachment, file_get_contents($file_path));
}

// Send Message

try
{

// Create Telegram API object
$telegram = new TelegramClass($apikey, $bot_username);

// Message pay load
$data = array('chat_id' => $chat_id, 'text' => $message, 'parse_mode' => 'HTML');

// Send message
$result = Request::sendMessage($data);

// Send any attachment if exists
if ($attachment != '')
{
if ($is_image == true)
{
// Sent image
$data_file = ['chat_id' => $chat_id,
'photo' => Request::encodeFile($attachment),
'caption' => '📎 '.$file_name,
];
$result_att = Request::sendPhoto($data_file);
}
else
{
// Sent attachment
$data_file = ['chat_id' => $chat_id,
'document' => Request::encodeFile($attachment),
'caption' => '📎 '.$file_name,
];
$result_att = Request::sendDocument($data_file);
}

// Remove temporory file
unlink($attachment);
}
}
catch (TelegramException $e)
{
// log telegram errors
// echo $e->getMessage();
error_log($e->getMessage());
}
}
}

0 comments on commit d8a3403

Please sign in to comment.