Skip to content

Commit

Permalink
[ENH]Telegram webhook notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Baraka24 authored and Shadow243 committed Jun 5, 2024
1 parent 3fb1d36 commit 4ce0bae
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
118 changes: 118 additions & 0 deletions lib/telegram_webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/**
* Webhook
* @package framework
* @subpackage webhook
*/

class Hm_Telegram_Webhook {

/* webhook_token value */
private $webhook_token;

private $prefix_ui = 'https://api.telegram.org/';

/**
* Load webhook token
* @param string $webhook_token
*/
public function __construct($webhook_token) {
$this->webhook_token = $webhook_token;
}

// Function to send Telegram notification
/**
* send telegram notiofication using curl
* @param array $extracted_msgs
*/
public function send(array $extracted_msgs) {
// Delete the webhook
if ($this->delete_webhook($this->webhook_token)) {
// Get the chat ID
$chatId = $this->get_chat_id();
if ($chatId) {
$text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nContent: {$extracted_msgs['body']}";

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_uri}bot{$this->webhook_token}/sendMessage");
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text]));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]);

$response = curl_exec($curl_handle);
if (curl_errno($ch)) {
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
} else {
$response_data = json_decode($response, true);
if (!$response_data['ok']) {
Hm_Msgs::add('ERRFailed to send message: ' . $response_data['description']);
}
}
curl_close($curl_handle);
unset($curl_handle);
}
}
}

/**
* get the chat ID using webhook_token
*/
private function get_chat_id() {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}/bot{$this->webhook_token}/getUpdates");
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_handle);

if (curl_errno($curl_handle)) {
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
return false;
} else {
$response_data = json_decode($response, true);
if ($response_data['ok']) {
if (!empty($response_data['result'])) {
$chatId = $response_data['result'][0]['message']['chat']['id'];
return $chatId;
} else {
Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
return false;
}
} else {
Hm_Msgs::add('ERRFailed to get chat ID: ' . $response_data['description'] . '<br>');
return false;
}
}

curl_close($curl_handle);
unset($curl_handle);
}

/**
* This function is usefull when trying to resend, we need to delete old webhook before we send a new one
* delete the webhook using webhook_token
*/
private function delete_webhook() {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}bot{$this->webhook_token}/delete_webhook");
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_handle);

if (curl_errno($curl_handle)) {
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
return false;
} else {
$response_data = json_decode($response, true);
if ($response_data['ok']) {
Hm_Msgs::add('ERRWebhook was deleted successfully.<br>');
return true;
} else {
Hm_Msgs::add('ERRFailed to delete webhook: ' . $response_data['description'] . '<br>');
return false;
}
}

curl_close($curl_handle);
unset($curl_handle);
}
}
2 changes: 1 addition & 1 deletion modules/feeds/hm-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function get_feed_data($url) {
}
switch ($type) {
case 'curl':
$curl_handle=curl_init();
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,15);
Expand Down
8 changes: 6 additions & 2 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* IMAP modules
* @package modules
Expand All @@ -8,7 +7,6 @@

if (!defined('DEBUG_MODE')) { die(); }


/**
* Check for attachments when forwarding a message
* @subpackage imap/handler
Expand Down Expand Up @@ -1316,6 +1314,12 @@ public function process() {
$this->out('imap_server_ids', $form['imap_server_ids']);
}
}






}

/**
Expand Down

0 comments on commit 4ce0bae

Please sign in to comment.