PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
3.3.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 All 138 releases
bit-form / includes / Core / Integration / Telegram / RecordApiHelper.php

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

90 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Telegram Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Telegram;
9
10 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
11 use BitCode\BitForm\Core\Util\FieldValueHandler;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for Record insert,upsert
16 */
17 class RecordApiHelper {
18 private $_defaultHeader;
19 private $_integrationID;
20 private $_logID;
21 private $_logResponse;
22 private $_entryID;
23 private $_apiEndPoint;
24
25 public function __construct($apiEndPoint, $integId, $logID, $entryID) {
26 // wp_send_json_success($tokenDetails);
27 $this->_defaultHeader['Content-Type'] = 'multipart/form-data';
28 $this->_integrationID = $integId;
29 $this->_logID = $logID;
30 $this->_logResponse = new UtilApiResponse();
31 $this->_entryID = $entryID;
32 $this->_apiEndPoint = $apiEndPoint;
33 }
34
35 public function sendMessages($data) {
36 $insertRecordEndpoint = $this->_apiEndPoint . '/sendMessage';
37 return HttpHelper::get($insertRecordEndpoint, $data, $this->_defaultHeader);
38 }
39
40 public function executeRecordApi($integrationDetails, $fieldValues, $formID, $entryID) {
41 $msg = FieldValueHandler::replaceFieldWithValue($integrationDetails->body, $fieldValues);
42 $messagesBody = str_replace(['<p>', '</p>'], ' ', $msg);
43
44 if (!empty($integrationDetails->actions->attachments)) {
45 foreach ($fieldValues as $fieldKey => $fieldValue) {
46 if ($integrationDetails->actions->attachments === $fieldKey) {
47 $file = $fieldValue;
48 }
49 }
50
51 if (!empty($file)) {
52 $data = [
53 'chat_id' => $integrationDetails->chat_id,
54 'caption' => $messagesBody,
55 'parse_mode' => $integrationDetails->parse_mode,
56 'photo' => is_array($file) ? $file[0] : $file
57 ];
58
59 $sendPhotoApiHelper = new FilesApiHelper($formID, $entryID);
60 $recordApiResponse = $sendPhotoApiHelper->uploadFiles($this->_apiEndPoint, $data);
61 } else {
62 $data = [
63 'chat_id' => $integrationDetails->chat_id,
64 'text' => $messagesBody,
65 'parse_mode' => $integrationDetails->parse_mode
66 ];
67 $recordApiResponse = $this->sendMessages($data);
68 }
69
70 $type = 'insert';
71 } else {
72 $data = [
73 'chat_id' => $integrationDetails->chat_id,
74 'text' => $messagesBody,
75 'parse_mode' => $integrationDetails->parse_mode
76 ];
77 $recordApiResponse = $this->sendMessages($data);
78 $type = 'insert';
79 }
80 $recordApiResponse = json_decode($recordApiResponse);
81
82 if ($recordApiResponse && $recordApiResponse->ok) {
83 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $recordApiResponse);
84 } else {
85 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse);
86 }
87 return $recordApiResponse;
88 }
89 }
90