PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / Telegram / TelegramHandler.php

TelegramHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/Core/Integration/Telegram/TelegramHandler.php

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