From 7c7d4de2f68a22b63920dd4662ab530cc1b6ecfe Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 21:06:28 +0300 Subject: improve login flow --- src/main/java/com/juick/api/TelegramBotHook.java | 54 +++++++++++++++--------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java index a9a35683..80ae0649 100644 --- a/src/main/java/com/juick/api/TelegramBotHook.java +++ b/src/main/java/com/juick/api/TelegramBotHook.java @@ -146,34 +146,48 @@ public class TelegramBotHook { public void doPost(HttpServletRequest request) throws IOException { try (BufferedReader reader = request.getReader()) { Message message = BotUtils.parseUpdate(reader).message(); - logger.info("got telegram msg: " + message.toString()); + User user_from = getUser(message.chat().id()).orElse(new User()); + logger.info(String.format("got telegram msg %s from juick user %d", message.toString(), user_from.getUID())); List chats = getChats(); if (!chats.contains(message.chat().id())) { addChat(message.chat().id()); logger.info("added chat with " + message.from().firstName()); createTelegramUser(message.from().id(), message.from().username()); - bot.execute(new SendMessage(message.chat().id(), - "You are subscribed to all Juick messages. " + - "Create or link an existing Juick account to control " + - "what do you want to receive").replyMarkup( - new InlineKeyboardMarkup( - new InlineKeyboardButton[]{ - new InlineKeyboardButton("LOGIN").url("http://juick.com/signup?type=durov&hash=" + - UserQueries.getSignUpHashByTelegramID(jdbc, message.from().id().longValue())) - })), new Callback() { - @Override - public void onResponse(SendMessage request, SendResponse response) { - logger.info("got response: " + response.message().toString()); - } - - @Override - public void onFailure(SendMessage request, IOException e) { - logger.log(Level.WARNING, "telegram failure", e); - } - }); + telegramSignupNotify(message.from().id().longValue(), UserQueries.getSignUpHashByTelegramID(jdbc, message.from().id().longValue())); + } else { + if (user_from.getUID() == 0) { + telegramSignupNotify(message.from().id().longValue(), UserQueries.getSignUpHashByTelegramID(jdbc, message.from().id().longValue())); + } else if (message.text().equalsIgnoreCase("/login")) { + String msg = String.format("Hi, %s!\nTap to log in", user_from.getUName()); + String msgUrl = "http://juick.com/login?" + UserQueries.getHashByUID(jdbc, user_from.getUID()); + telegramNotify(message.from().id().longValue(), msg, msgUrl); + } } } } + + private void telegramSignupNotify(Long telegramId, String hash) { + bot.execute(new SendMessage(telegramId, + "You are subscribed to all Juick messages. " + + "Create or link an existing Juick account to control " + + "what do you want to receive").replyMarkup( + new InlineKeyboardMarkup( + new InlineKeyboardButton[]{ + new InlineKeyboardButton("SIGNUP").url("http://juick.com/signup?type=durov&hash=" + + hash) + })), new Callback() { + @Override + public void onResponse(SendMessage request, SendResponse response) { + logger.info("got response: " + response.message().toString()); + } + + @Override + public void onFailure(SendMessage request, IOException e) { + logger.log(Level.WARNING, "telegram failure", e); + } + }); + } + private boolean createTelegramUser(long tgID, String tgName) { return jdbc.update("INSERT INTO telegram(tg_id, tg_name, loginhash) VALUES(?,?,?)", tgID, tgName, UUID.randomUUID().toString()) > 0; -- cgit v1.2.3