-
Notifications
You must be signed in to change notification settings - Fork 194
[ENH] Add telegram notification webhook when new messages are detected #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c516fa7
[ENH] Preparing webhook token setting
Shadow243 3fb1d36
[FIX]fixing reset icon to work on Restore current value and Restore d…
Baraka24 4ce0bae
[ENH]Telegram webhook notification
Baraka24 d822178
[REF] Refactor webhook codebase and fix small issues
Shadow243 391e40d
[FIX]Telegram webhook notification: Notify for unread emails every 5m…
Baraka24 c9792fe
[FIX]Telegram webhook notification: Set an configurable interval for …
Baraka24 84fdd85
Adding phpunit tests
Shadow243 9d501bd
[ENH]Telegram webhook interval: making it work
Baraka24 8ce1ea4
Merge branch 'webhook' of github.com:Shadow243/cypht into webhook
Shadow243 3d92d40
Correct phpunit tests
Shadow243 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Webhook | ||
| * @package framework | ||
| * @subpackage webhook | ||
| */ | ||
|
|
||
| class Hm_Telegram_Webhook { | ||
|
|
||
| const PREFIX_URI = 'https://api.telegram.org/'; | ||
|
|
||
| /** | ||
| * send telegram notiofication using curl | ||
| * @param array $extracted_msgs | ||
| */ | ||
| public static function send(array $extracted_msgs, $webhook_token) { | ||
| self::delete_webhook($webhook_token); | ||
| // Get the chat ID | ||
| $chatId = self::get_chat_id($webhook_token); | ||
| if (!empty($chatId)) { | ||
| $text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nTo: {$extracted_msgs['to']}"; | ||
| $curl_handle = Hm_Functions::c_init(); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/sendMessage'); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_POST, true); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text])); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]); | ||
| $curl_result = Hm_Functions::c_exec($curl_handle); | ||
| if (trim($curl_result)) { | ||
| $response_data = json_decode($curl_result, true); | ||
| if (!$response_data['ok']) { | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * get the chat ID using webhook_token | ||
| * @param string $webhook_token | ||
| */ | ||
| private static function get_chat_id($webhook_token) { | ||
| $curl_handle = Hm_Functions::c_init(); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/getUpdates'); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); | ||
| $curl_result = Hm_Functions::c_exec($curl_handle); | ||
| if (trim($curl_result)) { | ||
| $response_data = json_decode($curl_result, true); | ||
| if(!empty($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 ''; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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 if there is one | ||
| * sometimes the webhook is not deleted, so we need to delete it manually | ||
| * and sometines we are gettiting not found error | ||
| * @param string $webhook_token | ||
| */ | ||
| private static function delete_webhook($webhook_token) { | ||
| $curl_handle = Hm_Functions::c_init(); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/delete_webhook'); | ||
| Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); | ||
| Hm_Functions::c_exec($curl_handle); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.