| 1 |
<?php |
| 2 |
/** |
| 3 |
* Telegram Bot API Actions |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Actions; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Telegram_Actions |
| 16 |
*/ |
| 17 |
class Telegram_Actions extends Action { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'telegram'; |
| 24 |
$this->group = __( 'Telegram', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Execute the action. |
| 29 |
* |
| 30 |
* @param array $action_data Configuration for this action. |
| 31 |
* @param array $trigger_data Data passed from the trigger. |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function execute( $action_data, $trigger_data ) { |
| 35 |
$action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; |
| 36 |
|
| 37 |
if ( empty( $action_id ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
switch ( $action_id ) { |
| 42 |
case 'tg_send_message': |
| 43 |
return $this->send_message( $action_data, $trigger_data ); |
| 44 |
default: |
| 45 |
return false; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Send a message via Telegram Bot API. |
| 51 |
*/ |
| 52 |
private function send_message( $action_data, $trigger_data ) { |
| 53 |
$cfg = isset( $action_data['config'] ) ? $action_data['config'] : array(); |
| 54 |
|
| 55 |
$cred_id = isset( $cfg['credential'] ) ? $cfg['credential'] : ''; |
| 56 |
$credentials = get_option( 'wpbot_automator_credentials', array() ); |
| 57 |
$cred_data = isset( $credentials[ $cred_id ]['data'] ) ? $credentials[ $cred_id ]['data'] : array(); |
| 58 |
|
| 59 |
$bot_token = trim( $this->parse_tokens( isset( $cred_data['bot_token'] ) ? $cred_data['bot_token'] : '', $trigger_data ) ); |
| 60 |
$chat_id = trim( $this->parse_tokens( isset( $cfg['chat_id'] ) ? $cfg['chat_id'] : '', $trigger_data ) ); |
| 61 |
$text = $this->parse_tokens( isset( $cfg['text'] ) ? $cfg['text'] : '', $trigger_data ); |
| 62 |
|
| 63 |
if ( empty( $bot_token ) || empty( $chat_id ) || empty( $text ) ) { |
| 64 |
return array( |
| 65 |
'success' => false, |
| 66 |
'message' => 'Telegram Send Message: bot_token (from credential), chat_id, and text are required.', |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$url = "https://api.telegram.org/bot{$bot_token}/sendMessage"; |
| 71 |
$body = array( |
| 72 |
'chat_id' => $chat_id, |
| 73 |
'text' => $text, |
| 74 |
'parse_mode' => 'HTML', |
| 75 |
); |
| 76 |
|
| 77 |
$response = wp_remote_post( |
| 78 |
$url, |
| 79 |
array( |
| 80 |
'headers' => array( 'Content-Type' => 'application/json' ), |
| 81 |
'body' => wp_json_encode( $body ), |
| 82 |
'timeout' => 20, |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
if ( is_wp_error( $response ) ) { |
| 87 |
return array( 'success' => false, 'message' => $response->get_error_message() ); |
| 88 |
} |
| 89 |
|
| 90 |
$code = wp_remote_retrieve_response_code( $response ); |
| 91 |
$body_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 92 |
|
| 93 |
if ( $code >= 200 && $code < 300 && isset($body_response['ok']) && $body_response['ok'] === true ) { |
| 94 |
$message_id = isset( $body_response['result']['message_id'] ) ? $body_response['result']['message_id'] : 'N/A'; |
| 95 |
return array( |
| 96 |
'success' => true, |
| 97 |
'message' => "Telegram Message sent successfully. Message ID: {$message_id}", |
| 98 |
'message_id' => $message_id, |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
$error = isset( $body_response['description'] ) ? $body_response['description'] : 'Unknown API error.'; |
| 103 |
return array( 'success' => false, 'message' => "Telegram Send Message failed (HTTP {$code}): {$error}" ); |
| 104 |
} |
| 105 |
} |
| 106 |
|