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

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