| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Telegrom Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Telegram; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Telegram integration |
| 16 |
*/ |
| 17 |
class TelegramHandler { |
| 18 |
private $_formID; |
| 19 |
private $_integrationID; |
| 20 |
public const API_ENDPOINT = 'https://api.telegram.org/bot'; |
| 21 |
|
| 22 |
public function __construct($integrationID, $fromID) { |
| 23 |
$this->_formID = $fromID; |
| 24 |
$this->_integrationID = $integrationID; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Helps to register ajax function's with wp |
| 29 |
* |
| 30 |
* @return null |
| 31 |
*/ |
| 32 |
public static function registerAjax() { |
| 33 |
add_action('wp_ajax_bitforms_telegram_authorize', [__CLASS__, 'telegramAuthorize']); |
| 34 |
add_action('wp_ajax_bitforms_refresh_get_updates', [__CLASS__, 'refreshGetUpdates']); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Process ajax request for generate_token |
| 39 |
* |
| 40 |
* @return JSON zoho crm api response and status |
| 41 |
*/ |
| 42 |
public static function telegramAuthorize() { |
| 43 |
$authorizationHeader = null; |
| 44 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 45 |
$inputJSON = file_get_contents('php://input'); |
| 46 |
$requestsParams = json_decode($inputJSON); |
| 47 |
if ( |
| 48 |
empty($requestsParams->bot_api_key) |
| 49 |
) { |
| 50 |
wp_send_json_error( |
| 51 |
__( |
| 52 |
'Requested parameter is empty', |
| 53 |
'bit-form' |
| 54 |
), |
| 55 |
400 |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
$apiEndpoint = self::API_ENDPOINT . $requestsParams->bot_api_key . '/getMe'; |
| 60 |
$authorizationHeader['Accept'] = 'application/x-www-form-urlencoded'; |
| 61 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 62 |
|
| 63 |
if (is_wp_error($apiResponse) || !$apiResponse->ok) { |
| 64 |
wp_send_json_error( |
| 65 |
empty($apiResponse->error_code) ? 'Unknown' : $apiResponse, |
| 66 |
400 |
| 67 |
); |
| 68 |
} |
| 69 |
$apiEndpoint = self::API_ENDPOINT . $requestsParams->bot_api_key . '/getUpdates'; |
| 70 |
$authorizationHeader['Accept'] = 'application/x-www-form-urlencoded'; |
| 71 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 72 |
|
| 73 |
if (is_wp_error($apiResponse) || !$apiResponse->ok) { |
| 74 |
wp_send_json_error( |
| 75 |
empty($apiResponse->error_code) ? 'Unknown' : $apiResponse, |
| 76 |
400 |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
wp_send_json_success(true); |
| 81 |
} else { |
| 82 |
wp_send_json_error( |
| 83 |
__( |
| 84 |
'Token expired', |
| 85 |
'bit-form' |
| 86 |
), |
| 87 |
401 |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Process ajax request for refresh telegram get Updates |
| 94 |
* |
| 95 |
* @return JSON telegram get Updates data |
| 96 |
*/ |
| 97 |
public static function refreshGetUpdates() { |
| 98 |
$authorizationHeader = null; |
| 99 |
$response = null; |
| 100 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 101 |
$inputJSON = file_get_contents('php://input'); |
| 102 |
$requestsParams = json_decode($inputJSON); |
| 103 |
if ( |
| 104 |
empty($requestsParams->bot_api_key) |
| 105 |
) { |
| 106 |
wp_send_json_error( |
| 107 |
__( |
| 108 |
'Requested parameter is empty', |
| 109 |
'bit-form' |
| 110 |
), |
| 111 |
400 |
| 112 |
); |
| 113 |
} |
| 114 |
$apiEndpoint = self::API_ENDPOINT . $requestsParams->bot_api_key . '/getUpdates'; |
| 115 |
$authorizationHeader['Accept'] = 'application/json'; |
| 116 |
$telegramResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 117 |
|
| 118 |
$allList = []; |
| 119 |
if (!is_wp_error($telegramResponse) && $telegramResponse->ok) { |
| 120 |
$telegramChatLists = $telegramResponse->result; |
| 121 |
|
| 122 |
foreach ($telegramChatLists as $list) { |
| 123 |
if (!empty($list->my_chat_member->chat->title) && !empty($list->my_chat_member->chat->id)) { |
| 124 |
$allList[$list->my_chat_member->chat->title] = (object) [ |
| 125 |
'id' => $list->my_chat_member->chat->id, |
| 126 |
'name' => $list->my_chat_member->chat->title, |
| 127 |
]; |
| 128 |
} |
| 129 |
} |
| 130 |
uksort($allList, 'strnatcasecmp'); |
| 131 |
|
| 132 |
$response['telegramChatLists'] = $allList; |
| 133 |
} else { |
| 134 |
wp_send_json_error( |
| 135 |
$telegramResponse->description, |
| 136 |
400 |
| 137 |
); |
| 138 |
} |
| 139 |
wp_send_json_success($response, 200); |
| 140 |
} else { |
| 141 |
wp_send_json_error( |
| 142 |
__( |
| 143 |
'Token expired', |
| 144 |
'bit-form' |
| 145 |
), |
| 146 |
401 |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { |
| 152 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 153 |
// var_dump($integrationDetails); die; |
| 154 |
|
| 155 |
$bot_api_key = $integrationDetails->bot_api_key; |
| 156 |
$parse_mode = $integrationDetails->parse_mode; |
| 157 |
$chat_id = $integrationDetails->chat_id; |
| 158 |
$body = $integrationDetails->body; |
| 159 |
|
| 160 |
if ( |
| 161 |
empty($bot_api_key) |
| 162 |
|| empty($parse_mode) |
| 163 |
|| empty($chat_id) |
| 164 |
|| empty($body) |
| 165 |
) { |
| 166 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Telegram api', 'bit-form')); |
| 167 |
} |
| 168 |
$recordApiHelper = new RecordApiHelper(self::API_ENDPOINT . $bot_api_key, $this->_integrationID, $logID, $entryID); |
| 169 |
$telegramApiResponse = $recordApiHelper->executeRecordApi( |
| 170 |
$integrationDetails, |
| 171 |
$fieldValues, |
| 172 |
$this->_formID, |
| 173 |
$entryID |
| 174 |
); |
| 175 |
|
| 176 |
if (is_wp_error($telegramApiResponse)) { |
| 177 |
return $telegramApiResponse; |
| 178 |
} |
| 179 |
return $telegramApiResponse; |
| 180 |
} |
| 181 |
} |
| 182 |
|