PluginProbe ʕ •ᴥ•ʔ
Event Tickets with Ticket Scanner / 3.1.2
Event Tickets with Ticket Scanner v3.1.2
3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.4 trunk 2.6.0 2.7.0 2.7.1 2.7.10 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3
event-tickets-with-ticket-scanner / sasoEventtickets_Messenger.php
event-tickets-with-ticket-scanner Last commit date
3rd 1 week ago css 1 week ago img 1 week ago includes 1 week ago js 1 week ago languages 1 week ago ticket 1 week ago vendors 1 week ago SASO_EVENTTICKETS.php 1 week ago backend.js 1 week ago changelog-features.json 1 week ago changelog.txt 1 week ago db.php 1 week ago index.php 1 week ago init_file.php 1 week ago order_details.js 1 week ago pwa-sw.js 1 week ago readme.txt 1 week ago saso-eventtickets-validator.js 1 week ago sasoEventtickets_AdminSettings.php 1 week ago sasoEventtickets_Authtoken.php 1 week ago sasoEventtickets_Base.php 1 week ago sasoEventtickets_Core.php 1 week ago sasoEventtickets_Frontend.php 1 week ago sasoEventtickets_Messenger.php 1 week ago sasoEventtickets_Options.php 1 week ago sasoEventtickets_PDF.php 1 week ago sasoEventtickets_Seating.php 1 week ago sasoEventtickets_Ticket.php 1 week ago sasoEventtickets_TicketBadge.php 1 week ago sasoEventtickets_TicketDesigner.php 1 week ago sasoEventtickets_TicketQR.php 1 week ago ticket_events.js 1 week ago ticket_scanner.js 1 week ago validator.js 1 week ago version-notices.json 1 week ago vollstart-cross-promo.php 1 week ago wc_backend.js 1 week ago wc_frontend.js 1 week ago woocommerce-hooks.php 1 week ago
sasoEventtickets_Messenger.php
143 lines
1 <?php
2 /* this contains the class for the Messengers
3 * @package Event Tickets with Ticket Scanner
4 * @author Vollstart
5 *
6 * For Plugin Name: Event Tickets with Ticket Scanner
7 */
8 include_once(plugin_dir_path(__FILE__)."init_file.php");
9 class sasoEventtickets_Messenger {
10 private $MAIN;
11
12 public static function Instance() {
13 static $inst = null;
14 if ($inst === null) {
15 $inst = new sasoEventtickets_Messenger();
16 }
17 return $inst;
18 }
19
20 public function __construct() {
21 global $sasoEventtickets;
22 $this->MAIN = $sasoEventtickets;
23 }
24
25 /*
26 public function initHandlers() {
27 // ??? should all teh options be set and handled here?
28 // should we use the action hook to execute the sending?
29 // add_action('sasoeventtickets_sendFileViaMessenger', array($this, 'handleRequest')); ???
30 }
31 */
32
33 public function sendMessage($message, $phoneNumber, $type) {
34 $this->checkType($type);
35 switch ($type) {
36 case 'whatsapp':
37 return $this->sendWhatsAppMessage($message, $phoneNumber);
38 case 'telegram':
39 return $this->sendTelegramMessage($message, $phoneNumber);
40 default:
41 throw new Exception('#2001 Unsupported messenger type');
42 }
43 }
44 public function sendFile($filepath, $message, $phoneNumber, $type) {
45 $this->checkType($type);
46 switch ($type) {
47 case 'whatsapp':
48 return $this->sendWhatsAppMessageFile($filepath, $message, $phoneNumber);
49 case 'telegram':
50 return $this->sendTelegramMessageFile($filepath, $message, $phoneNumber);
51 default:
52 throw new Exception('#2002 Unsupported messenger type');
53 }
54 }
55
56 private function sendWhatsAppMessage($message, $phoneNumber) {
57
58 // maybe this https://medium.com/@wassenger/send-automated-messages-on-whatsapp-using-php-26cad1781c2c
59 // for now the code below is created by code generator
60
61
62 // WhatsApp API URL
63 $url = "https://api.whatsapp.com/send?phone=$phoneNumber&text=" . urlencode($message);
64
65 // Use file_get_contents to send the message
66 $response = file_get_contents($url);
67
68 // Check for errors
69 if ($response === FALSE) {
70 throw new Exception('#2003 Error sending WhatsApp message');
71 }
72
73 return $response;
74 }
75 private function sendTelegramMessage($message, $chatId) {
76 // code genereted by code generator
77 // Telegram API URL
78 $url = "https://api.telegram.org/bot" . $this->get_TELEGRAM_BOT_TOKEN() . "/sendMessage?chat_id=$chatId&text=" . urlencode($message);
79 // Use file_get_contents to send the message
80 $response = file_get_contents($url);
81 // Check for errors
82 if ($response === FALSE) {
83 throw new Exception('#2004 Error sending Telegram message');
84 }
85 return $response;
86 }
87
88 private function sendWhatsAppMessageFile($filepath, $message, $phoneNumber) {
89 //TODO: Implement WhatsApp file sending
90 //??
91 }
92
93 private function sendTelegramMessageFile($filepath, $message, $chatId) {
94 // Telegram API URL
95 $url = "https://api.telegram.org/bot" . $this->get_TELEGRAM_BOT_TOKEN() . "/sendDocument";
96
97 // Prepare the POST fields
98 $postFields = [
99 'chat_id' => $chatId,
100 'caption' => $message,
101 'document' => new CURLFile(realpath($filepath))
102 ];
103
104 // Initialize cURL
105 $ch = curl_init();
106 curl_setopt($ch, CURLOPT_URL, $url);
107 curl_setopt($ch, CURLOPT_POST, true);
108 curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
109 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
110
111 // Execute the request
112 $response = curl_exec($ch);
113
114 // Check for errors
115 if ($response === FALSE) {
116 throw new Exception('#2005 Error sending Telegram message with file');
117 }
118
119 // Close cURL
120 curl_close($ch);
121
122 return $response;
123 }
124
125 private function checkType($type) {
126 if ($type === null) {
127 throw new InvalidArgumentException('#2006 Messenger type cannot be null');
128 }
129 if (!in_array($type, ['whatsapp', 'telegram'])) {
130 throw new InvalidArgumentException('#2007 Invalid messenger type');
131 }
132 }
133
134 private function get_TELEGRAM_BOT_TOKEN() {
135 //TODO: Implement a secure way to get the Telegram bot token - the options are readable from other plugins, so encrypt it
136 }
137 private function get_WHATSAPP_APIKEY() {
138 //TODO: Implement a secure way to get the whatsapp apikey - the options are readable from other plugins, so encrypt it
139 }
140
141 }
142
143 ?>