| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentMail\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentMail\App\Models\Settings; |
| 6 |
use FluentMail\App\Services\NotificationHelper; |
| 7 |
use FluentMail\App\Services\Notification\Manager as NotificationManager; |
| 8 |
use FluentMail\Includes\Request\Request; |
| 9 |
use FluentMail\Includes\Support\Arr; |
| 10 |
|
| 11 |
class TelegramController extends Controller |
| 12 |
{ |
| 13 |
public function issuePinCode(Request $request) |
| 14 |
{ |
| 15 |
$this->verify(); |
| 16 |
|
| 17 |
$formData = $request->get('settings', []); |
| 18 |
|
| 19 |
$userEmail = sanitize_email(Arr::get($formData, 'user_email')); |
| 20 |
|
| 21 |
if (!is_email($userEmail)) { |
| 22 |
return $this->sendError([ |
| 23 |
'message' => __('Please provide a valid email address.', 'fluent-smtp') |
| 24 |
], 422); |
| 25 |
} |
| 26 |
|
| 27 |
$payload = [ |
| 28 |
'admin_email' => $userEmail, |
| 29 |
'smtp_url' => admin_url('options-general.php?page=fluent-mail#/'), |
| 30 |
'site_url' => site_url(), |
| 31 |
'site_title' => fluentMailSiteTitle(), |
| 32 |
'site_lang' => get_bloginfo('language'), |
| 33 |
]; |
| 34 |
|
| 35 |
|
| 36 |
$activationData = NotificationHelper::issueTelegramPinCode($payload); |
| 37 |
|
| 38 |
if (is_wp_error($activationData)) { |
| 39 |
return $this->sendError([ |
| 40 |
'message' => $activationData->get_error_message(), |
| 41 |
'errors' => $activationData->get_error_data(), |
| 42 |
], 422); |
| 43 |
} |
| 44 |
|
| 45 |
return $this->sendSuccess([ |
| 46 |
'message' => __('Now activate the connection from your Telegram account.', 'fluent-smtp'), |
| 47 |
'site_token' => Arr::get($activationData, 'site_token'), |
| 48 |
'site_pin' => Arr::get($activationData, 'site_pin'), |
| 49 |
]); |
| 50 |
} |
| 51 |
|
| 52 |
public function confirmConnection(Request $request) |
| 53 |
{ |
| 54 |
$this->verify(); |
| 55 |
|
| 56 |
$siteToken = $request->get('site_token', ''); |
| 57 |
|
| 58 |
if (empty($siteToken)) { |
| 59 |
return $this->sendError([ |
| 60 |
'message' => __('Please provide the site token.', 'fluent-smtp') |
| 61 |
], 422); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
$connectionInfo = NotificationHelper::getTelegramConnectionInfo($siteToken); |
| 66 |
|
| 67 |
if (is_wp_error($connectionInfo)) { |
| 68 |
return $this->sendError([ |
| 69 |
'message' => $connectionInfo->get_error_message(), |
| 70 |
'errors' => $connectionInfo->get_error_data(), |
| 71 |
], 422); |
| 72 |
} |
| 73 |
|
| 74 |
NotificationHelper::updateChannelSettings('telegram', [ |
| 75 |
'status' => 'yes', |
| 76 |
'token' => $siteToken |
| 77 |
]); |
| 78 |
|
| 79 |
return $this->sendSuccess([ |
| 80 |
'success' => true, |
| 81 |
'message' => __('Connection successful.', 'fluent-smtp'), |
| 82 |
]); |
| 83 |
} |
| 84 |
|
| 85 |
public function getTelegramConnectionInfo(Request $request) |
| 86 |
{ |
| 87 |
$this->verify(); |
| 88 |
|
| 89 |
$settings = (new Settings())->notificationSettings(); |
| 90 |
|
| 91 |
if (Arr::get($settings, 'telegram.status') != 'yes') { |
| 92 |
return $this->sendSuccess([ |
| 93 |
'message' => __('Telegram notifications are not enabled.', 'fluent-smtp'), |
| 94 |
'telegram_notify_status' => 'no' |
| 95 |
], 200); |
| 96 |
} |
| 97 |
|
| 98 |
$siteToken = Arr::get($settings, 'telegram.token'); |
| 99 |
|
| 100 |
$connectionInfo = NotificationHelper::getTelegramConnectionInfo($siteToken); |
| 101 |
|
| 102 |
if (is_wp_error($connectionInfo)) { |
| 103 |
return $this->sendSuccess([ |
| 104 |
'telegram_notify_status' => 'failed', |
| 105 |
'message' => $connectionInfo->get_error_message(), |
| 106 |
'errors' => $connectionInfo->get_error_data(), |
| 107 |
]); |
| 108 |
} |
| 109 |
|
| 110 |
return $this->sendSuccess([ |
| 111 |
'telegram_notify_status' => 'yes', |
| 112 |
'telegram_receiver' => Arr::get($connectionInfo, 'telegram_receiver', []), |
| 113 |
]); |
| 114 |
} |
| 115 |
|
| 116 |
public function sendTestMessage(Request $request) |
| 117 |
{ |
| 118 |
$this->verify(); |
| 119 |
|
| 120 |
// Let's update the notification status |
| 121 |
$settings = (new Settings())->notificationSettings(); |
| 122 |
|
| 123 |
if (Arr::get($settings, 'telegram.status') != 'yes') { |
| 124 |
return $this->sendError([ |
| 125 |
'message' => __('Telegram notifications are not enabled.', 'fluent-smtp') |
| 126 |
], 422); |
| 127 |
} |
| 128 |
|
| 129 |
$result = NotificationHelper::sendTestTelegramMessage(Arr::get($settings, 'telegram.token')); |
| 130 |
|
| 131 |
if (is_wp_error($result)) { |
| 132 |
return $this->sendError([ |
| 133 |
'message' => $result->get_error_message(), |
| 134 |
'errors' => $result->get_error_data(), |
| 135 |
], 422); |
| 136 |
} |
| 137 |
|
| 138 |
return $this->sendSuccess([ |
| 139 |
'message' => __('Test message sent.', 'fluent-smtp') |
| 140 |
]); |
| 141 |
} |
| 142 |
|
| 143 |
public function disconnect() |
| 144 |
{ |
| 145 |
$this->verify(); |
| 146 |
|
| 147 |
$settings = (new Settings())->notificationSettings(); |
| 148 |
|
| 149 |
$token = Arr::get($settings, 'telegram.token'); |
| 150 |
|
| 151 |
// Only call disconnect API if we have a token |
| 152 |
if ($token) { |
| 153 |
NotificationHelper::disconnectTelegram($token); |
| 154 |
} |
| 155 |
|
| 156 |
NotificationHelper::updateChannelSettings('telegram', [ |
| 157 |
'status' => 'no', |
| 158 |
'token' => '' |
| 159 |
]); |
| 160 |
|
| 161 |
return $this->sendSuccess([ |
| 162 |
'message' => __('Telegram has been disconnected.', 'fluent-smtp') |
| 163 |
]); |
| 164 |
} |
| 165 |
} |
| 166 |
|