-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-elementor-telegram-notification.php
More file actions
172 lines (136 loc) · 4.46 KB
/
class-elementor-telegram-notification.php
File metadata and controls
172 lines (136 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Elementor Telegram Notification Plugin
*
* @package ElementorTelegramNotification
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
/**
* Elementor_Telegram_Notification class
*/
class Elementor_Telegram_Notification
{
private $bot_token;
private $chat_id;
private $settings;
/**
* Constructor
*/
public function __construct()
{
// Telegram bot token
$this->settings = get_option('telegram_notification_settings');
$this->bot_token = $this->settings['telegram_bot_token'];
$this->chat_id = $this->settings['telegram_chat_id'];
if (strlen($this->bot_token) == 0 || strlen($this->chat_id) == 0) {
return;
}
// Handle form submission
add_action('elementor_pro/forms/new_record', function ($record, $handler) {
$this->send_form_content_via_telegram($record, $handler);
$this->send_vcard_via_telegram($record, $handler);
}, 10, 2);
}
/**
* Generic telegram request
*
* @return void
*/
private function telegram_request($endpoint, $method, $body = [])
{
// Telegram API base URL
$url = "https://api.telegram.org/bot{$this->bot_token}/{$endpoint}";
$body['chat_id'] = $this->chat_id;
// Initialize cURL session
$ch = curl_init();
// Set cURL options
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
];
// If method is POST and body is provided, include body in the request
if ($method == 'POST' && !empty($body)) {
$options[CURLOPT_POST] = true;
$options[CURLOPT_HTTPHEADER] = ['Content-Type: multipart/form-data'];
$options[CURLOPT_POSTFIELDS] = $body;
}
// Set cURL options
curl_setopt_array($ch, $options);
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
// Close cURL session
curl_close($ch);
// Return the response
return $response;
}
/**
* Send form content via Telegram
*
* @return void
*/
private function send_form_content_via_telegram($record, $handler)
{
// Extract form data
$form_data = $record->get_formatted_data();
// Compose message
$message = "New form submission:\n";
foreach ($form_data as $field_name => $field_value) {
$message .= "$field_name: $field_value\n";
}
$telegram_params['text'] = $message;
$this->telegram_request('sendMessage', 'POST', $telegram_params);
}
/**
* Send a vCard file via Telegram
*
* @return void
*/
private function send_vcard_via_telegram($record, $handler)
{
$vcard_contents = $this->generate_vcard($record);
$vcard_file = 'contact.vcf';
file_put_contents($vcard_file, $vcard_contents);
$telegram_params = array(
'document' => new CURLFile(realpath($vcard_file), 'text/vcard', 'contact.vcf'),
'caption' => 'Sample vCard file'
);
$this->telegram_request('sendDocument', 'POST', $telegram_params);
unlink($vcard_file);
}
/**
* Generate a sample vCard file
*
* @return string vCard file contents
*/
private function generate_vcard($record)
{
$fields = $record->get('fields');
$name = $fields['name']['value'] ?? '';
$data['name'] = $name . ' TZ LEAD';
$data['phone'] = $fields['phone']['value'] ?? '';
$data['email'] = $fields['email']['value'] ?? '';
$card = "BEGIN:VCARD\r\n";
$card .= "VERSION:3.0\r\n";
$card .= "CLASS:PUBLIC\r\n";
$card .= "PRODID:-//class_vCard from WhatsAPI//NONSGML Version 1//EN\r\n";
$card .= "REV:" . date('Y-m-d H:i:s') . "\r\n";
$card .= "FN:" . $data['name'] ." " . "\r\n";
$card .= "N:"
. $data['name'] . ";"
. ''. "\r\n";
$card .= "TITLE:" . ' ' . "\r\n";
$card .= "ORG:" . ' ' . "\r\n";
$card .= "EMAIL;type=INTERNET,pref:" . $data['email'] . "\r\n";
$card .= "TEL;type=WORK,voice:" . $data['phone'] . "\r\n";
$card .= "END:VCARD\r\n";
return $card;
}
}