PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
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 3.2.2, at includes/Core/Integration/Telegram/TelegramHandler.php

208 lines 6.0 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 GlobalHelper::requirePostMethod();
55
56 try {
57 $requestsParams = GlobalHelper::formatRequestData();
58 } catch (\InvalidArgumentException $e) {
59 wp_send_json_error($e->getMessage(), 400);
60 }
61
62 if (
63 empty($requestsParams->bot_api_key)
64 ) {
65 wp_send_json_error(
66 __(
67 'Requested parameter is empty',
68 'bit-form'
69 ),
70 400
71 );
72 }
73
74 $apiEndpoint = self::$api_endpoint . $requestsParams->bot_api_key . '/getMe';
75 $authorizationHeader['Accept'] = 'application/x-www-form-urlencoded';
76 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
77
78 if (is_wp_error($apiResponse) || !$apiResponse->ok) {
79 wp_send_json_error(
80 empty($apiResponse->error_code) ? 'Unknown' : $apiResponse,
81 400
82 );
83 }
84 $apiEndpoint = self::$api_endpoint . $requestsParams->bot_api_key . '/getUpdates';
85 $authorizationHeader['Accept'] = 'application/x-www-form-urlencoded';
86 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
87
88 if (is_wp_error($apiResponse) || !$apiResponse->ok) {
89 wp_send_json_error(
90 empty($apiResponse->error_code) ? 'Unknown' : $apiResponse,
91 400
92 );
93 }
94
95 wp_send_json_success(true);
96 } else {
97 wp_send_json_error(
98 __(
99 'Token expired',
100 'bit-form'
101 ),
102 401
103 );
104 }
105 }
106
107 /**
108 * Process ajax request for refresh telegram get Updates
109 *
110 * @return JSON telegram get Updates data
111 */
112 public static function refreshGetUpdates()
113 {
114 $authorizationHeader = null;
115 $response = null;
116 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
117 GlobalHelper::requirePostMethod();
118
119 try {
120 $requestsParams = GlobalHelper::formatRequestData();
121 } catch (\InvalidArgumentException $e) {
122 wp_send_json_error($e->getMessage(), 400);
123 }
124 if (
125 empty($requestsParams->bot_api_key)
126 ) {
127 wp_send_json_error(
128 __(
129 'Requested parameter is empty',
130 'bit-form'
131 ),
132 400
133 );
134 }
135 $apiEndpoint = self::$api_endpoint . $requestsParams->bot_api_key . '/getUpdates';
136 $authorizationHeader['Accept'] = 'application/json';
137 $telegramResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
138
139 $allList = [];
140 if (!is_wp_error($telegramResponse) && $telegramResponse->ok) {
141 $telegramChatLists = $telegramResponse->result;
142
143 foreach ($telegramChatLists as $list) {
144 if (!empty($list->my_chat_member->chat->title) && !empty($list->my_chat_member->chat->id)) {
145 $allList[$list->my_chat_member->chat->title] = (object) [
146 'id' => $list->my_chat_member->chat->id,
147 'name' => $list->my_chat_member->chat->title,
148 ];
149 } elseif (isset($list->channel_post->chat->title, $list->channel_post->chat->id)) {
150 $allList[$list->channel_post->chat->title] = (object) [
151 'id' => $list->channel_post->chat->id,
152 'name' => $list->channel_post->chat->title,
153 ];
154 }
155 }
156 uksort($allList, 'strnatcasecmp');
157
158 $response['telegramChatLists'] = $allList;
159 } else {
160 wp_send_json_error(
161 $telegramResponse->description,
162 400
163 );
164 }
165 wp_send_json_success($response, 200);
166 } else {
167 wp_send_json_error(
168 __(
169 'Token expired',
170 'bit-form'
171 ),
172 401
173 );
174 }
175 }
176
177 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
178 {
179 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
180
181 $bot_api_key = $integrationDetails->bot_api_key;
182 $parse_mode = $integrationDetails->parse_mode;
183 $chat_id = $integrationDetails->chat_id;
184 $body = $integrationDetails->body;
185
186 if (
187 empty($bot_api_key)
188 || empty($parse_mode)
189 || empty($chat_id)
190 || empty($body)
191 ) {
192 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Telegram api', 'bit-form'));
193 }
194 $recordApiHelper = new RecordApiHelper(self::$api_endpoint . $bot_api_key, $this->_integrationID, $logID, $entryID);
195 $telegramApiResponse = $recordApiHelper->executeRecordApi(
196 $integrationDetails,
197 $fieldValues,
198 $this->_formID,
199 $entryID
200 );
201
202 if (is_wp_error($telegramApiResponse)) {
203 return $telegramApiResponse;
204 }
205 return $telegramApiResponse;
206 }
207 }
208