| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Http\Traits;
- use Illuminate\Support\Facades\Http;
- use App\Models\User;
- //use App\Models\OrderPayment;
- use Telegram\Bot\Api;
- trait TelegramTrait
- {
- /**
- * 로그인 알림
- */
- protected function sendMessageToLogin(User $user)
- {
- $this->sendMessage(4,
- sprintf("[로그인]\n이메일 : %s\n이름 : %s", $user->email, $user->name)
- );
- }
- /**
- * 회원가입 알림
- */
- protected function sendMessageToRegister(User $user)
- {
- $this->sendMessage(4,
- sprintf("[회원가입]\n이메일 : %s\n이름 : %s\n생년월일 : %s", $user->email, $user->name, $user->birthday)
- );
- }
- /**
- * 주문 알림
- */
- // protected function sendMessageToOrder(OrderPayment $orderPayment)
- // {
- // $this->sendMessage(1,
- // sprintf("[상품 주문]\n주문 ID: %d\n주문자 : %s\n가격 : %s", $orderPayment->orderID, $orderPayment->buyer_name, number_format($orderPayment->order_price) . '원')
- // );
- // }
- /**
- * 텔레그램 알림 메시지
- */
- protected function sendMessage(int $room, string $msg): bool
- {
- $token = match ($room) {
- // G2A Order Bot Token
- 1 => config('g2a_order_bot_token'),
- // G2A Product Bot Token
- 2 => config('g2a_product_bot_token'),
- // G2A Error Bot Token
- 3 => config('g2a_error_bot_token'),
- // Main Bot Token
- 4 => config('main_bot_token')
- };
- if (!$token) {
- return false;
- }
- $api = new Api($token);
- if (!$api->getAccessToken()) {
- return false;
- }
- $chatID = null;
- $updates = $api->getUpdates();
- if($updates) {
- $chatID = last($updates)?->getChat()?->get('id');
- }
- if(!$chatID) {
- $chatID = 650107127;
- }
- $url = sprintf('https://api.telegram.org/bot%s/sendMessage?chat_id=%d&text=%s', $token, $chatID, $msg);
- $response = Http::timeout(60)->retry(3, 100)->get($url);
- unset($token, $api, $chatID, $url);
- return $response->ok();
- }
- }
|