Skip to content

Instantly share code, notes, and snippets.

@ydenissov
Created October 30, 2022 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ydenissov/d3e1b2f9fb3e4ffa595719142b18cb80 to your computer and use it in GitHub Desktop.
Save ydenissov/d3e1b2f9fb3e4ffa595719142b18cb80 to your computer and use it in GitHub Desktop.
Simple Sync Telegram bot with user without webhooks
<?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