Created
October 30, 2022 18:35
-
-
Save ydenissov/d3e1b2f9fb3e4ffa595719142b18cb80 to your computer and use it in GitHub Desktop.
Simple Sync Telegram bot with user without webhooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function sync() { | |
$user_id = auth()->user()->id; | |
$user = User::findOrFail($user_id); | |
$searched_username = $user->telegram_username; | |
if (!isset($searched_username) || ($searched_username === '')) { | |
return ['success' => 'not', 'error' => 'Empty username']; | |
} | |
$response = Telegram::bot('bot_name')->getUpdates(); | |
$chat_id = 0; | |
foreach ($response as $item) { | |
if (isset($item['message']['chat']['username']) && ($item['message']['chat']['username'] === $searched_username)) { | |
$chat_id = $item['message']['chat']['id']; | |
} | |
} | |
if ($chat_id > 0) { | |
$user->telegram_chat_id = $chat_id; | |
if ($user->save()) { | |
$this->sendMessage($user_id, "✅ Synchronization was successful!\n\nNow I can send you notifications!"); | |
flash()->success('Done'); | |
return ['success' => 'yes']; | |
} else { | |
return ['success' => 'not', 'error' => 'Error when update user']; | |
} | |
} else { | |
return ['success' => 'not', 'error' => 'Send message to bot']; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment