Search in sources :

Example 6 with SendMessage

use of com.pengrad.telegrambot.request.SendMessage in project anton-pavlovich-bot by wyvie.

the class AbstractKarmaCommand method sendMessage.

private void sendMessage(long chatId, String message) {
    SendMessage sendMessage = new SendMessage(chatId, message);
    telegramBot.execute(sendMessage);
}
Also used : SendMessage(com.pengrad.telegrambot.request.SendMessage)

Example 7 with SendMessage

use of com.pengrad.telegrambot.request.SendMessage in project anton-pavlovich-bot by wyvie.

the class ApiController method sendMessage.

@RequestMapping(path = "/sendMessage", method = RequestMethod.POST)
@ResponseStatus(code = HttpStatus.OK)
public void sendMessage(@RequestParam("chat-id") long chatId, @RequestParam("message") String message) {
    SendMessage sendMessage = new SendMessage(chatId, message);
    telegramBot.execute(sendMessage);
}
Also used : SendMessage(com.pengrad.telegrambot.request.SendMessage) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with SendMessage

use of com.pengrad.telegrambot.request.SendMessage in project cryptonomica by Cryptonomica.

the class TelegramWebhookServlet method doPost.

/*
     * to receive information using Telegram webhook ( Update object)
     * Telegram: "Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url,
     * containing a JSON-serialized Update. // see: Update object description on https://core.telegram.org/bots/api#update
     * In case of an unsuccessful request, we will give up after a reasonable amount of attempts"
     * https://core.telegram.org/bots/api#setwebhook
     *
     * */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    TelegramBot cryptonomicaBot = TelegramBotFactory.getCryptonomicaBot();
    // see:
    // https://github.com/pengrad/java-telegram-bot-api#webhook
    java.io.Reader reader = request.getReader();
    // or from java.io.Reader
    Update update = BotUtils.parseUpdate(reader);
    Message message = update.message();
    String messageText = message.text();
    Integer message_id = message.messageId();
    Chat chat = message.chat();
    Long chat_id = chat.id();
    String textToSend = null;
    Keyboard replyMarkup = null;
    if (messageText.equalsIgnoreCase("/start")) {
        textToSend = "Hi, I'm Cryptonomica Bot.\n" + "Nice to meet you, " + message.from().firstName() + " " + message.from().lastName() + ". " + "if you need more info, type: /moreInfo \n" + "if you want to start from the beginning, type: /start \n";
        // replyMarkup = new ForceReply();
        replyMarkup = new ReplyKeyboardRemove();
    } else if (messageText.equalsIgnoreCase("/moreInfo")) {
        textToSend = "please visit my web-app: [Cryptonomica.net](https://cryptonomica.net) ";
        // <<< needed!
        replyMarkup = new ReplyKeyboardRemove();
    } else if (message.chat().id() != TelegramBotFactory.getCryptonomicaAdminsChatId()) {
        textToSend = "really, " + message.text() + "?\n";
        // <<< needed!
        replyMarkup = new ReplyKeyboardRemove();
    }
    if (textToSend != null) {
        LOG.warning("sending message: " + textToSend + " to chat: " + message.chat().id() + " in respond to message '" + messageText + "' from " + message.from().username());
        // see message params on
        // https://core.telegram.org/bots/api#message
        SendMessage sendMessageRequest = new SendMessage(// chat id
        message.chat().id(), textToSend).parseMode(ParseMode.Markdown).disableWebPagePreview(false).disableNotification(// ?
        false).replyToMessageId(message.messageId()).replyMarkup(replyMarkup);
        LOG.warning(CollectionsService.mapToString(sendMessageRequest.getParameters()));
        SendResponse sendResponse = cryptonomicaBot.execute(sendMessageRequest);
        LOG.warning(sendResponse.toString());
    }
    ServletUtils.sendJsonResponse(response, "{}");
/*
        // this works faster:
        //
        // check url key:
        final String CryptonomicaBotTelegramUrlKey = ServletUtils.getUrlKey(request);
        final String TELEGRAM_URL_KEY = ApiKeysUtils.getApiKey("CryptonomicaBotTelegramUrlKey");
        if (!TELEGRAM_URL_KEY.equalsIgnoreCase(CryptonomicaBotTelegramUrlKey)) {
            throw new ServletException("CryptonomicaBotTelegramUrlKey is invalid");
        }

        // Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update
        // ( https://core.telegram.org/bots/api#setwebhook )
        // Update is an object that represents an incoming update
        // ( https://core.telegram.org/bots/api#update )
        JSONObject updateJsonObject = ServletUtils.getJsonObjectFromRequestWithIOUtils(request);
        JSONObject message = updateJsonObject.getJSONObject("message");
        // message_id represented by int:
        int message_id = message.getInt("message_id");
        // the actual UTF-8 text of the message, 0-4096 characters.
        String text = message.getString("text");
        JSONObject chat = message.getJSONObject("chat");
        // chat_id represented by int:
        int chat_id = chat.getInt("id");
        JSONObject from = message.getJSONObject("from");

        LOG.warning("message: " + message.toString());
        LOG.warning("message_id: " + message_id);
        LOG.warning("message text: " + text);
        LOG.warning("chat: " + chat.toString());
        LOG.warning("chat_id: " + chat_id);
        LOG.warning("from: " + from.toString());

        // see:
        // https://core.telegram.org/bots/api#sendmessage
        Map<String, String> parameterMap = new HashMap<>();
        String sendMessageText = "really, " + text + "?";
        parameterMap.put("text", sendMessageText);
        parameterMap.put("chat_id", Integer.toString(chat_id));
        parameterMap.put("reply_to_message_id", Integer.toString(message_id));

        HTTPResponse httpResponse = CryptonomicaBot.sendMessageWithParameterMap(parameterMap);
        LOG.warning("httpResponse: " + httpResponse);

        ServletUtils.sendJsonResponse(response, "O.K.");
       */
}
Also used : SendMessage(com.pengrad.telegrambot.request.SendMessage) Message(com.pengrad.telegrambot.model.Message) Keyboard(com.pengrad.telegrambot.model.request.Keyboard) SendResponse(com.pengrad.telegrambot.response.SendResponse) TelegramBot(com.pengrad.telegrambot.TelegramBot) Update(com.pengrad.telegrambot.model.Update) Chat(com.pengrad.telegrambot.model.Chat) ReplyKeyboardRemove(com.pengrad.telegrambot.model.request.ReplyKeyboardRemove) SendMessage(com.pengrad.telegrambot.request.SendMessage)

Example 9 with SendMessage

use of com.pengrad.telegrambot.request.SendMessage in project anton-pavlovich-bot by wyvie.

the class AbstractKarmaCommand method processCommand.

void processCommand(Message message) {
    long userId = message.from().id();
    Message replied = message.replyToMessage();
    if (replied == null) {
        // sendMessage(message.chat().id(), ERROR_NOT_REPLY);
        return;
    }
    if (message.from().id().equals(replied.from().id())) {
        // sendMessage(message.chat().id(), ERROR_YOURSELF);
        return;
    }
    if (!canUserUpdateNow(userId)) {
        sendMessage(message.chat().id(), ERROR_TOO_EARLY);
        return;
    }
    processKarma(replied.from());
    updateLastSetKarma(message.from().id());
    int newKarma = getUserKarma(replied.from().id());
    String username = replied.from().username();
    if (username == null || "".equals(username.trim())) {
        String firstName = replied.from().firstName() == null ? "" : replied.from().firstName();
        String lastName = replied.from().lastName() == null ? "" : replied.from().lastName();
        username = (firstName + " " + lastName).trim();
    } else
        username = "@" + username.trim();
    sendMessage(message.chat().id(), INFO_NEW_KARMA.replaceAll("%USER%", username).replaceAll("%KARMA%", Integer.toString(newKarma)));
}
Also used : SendMessage(com.pengrad.telegrambot.request.SendMessage) Message(com.pengrad.telegrambot.model.Message)

Example 10 with SendMessage

use of com.pengrad.telegrambot.request.SendMessage in project anton-pavlovich-bot by wyvie.

the class AbstractRemindCommand method sendMessage.

SendResponse sendMessage(Object id, String messageText, @Nullable ParseMode parseMode) {
    String chatId;
    if (id instanceof User) {
        chatId = ((User) id).id().toString();
    } else if (id instanceof Chat) {
        chatId = ((Chat) id).id().toString();
    } else {
        logger.error("!!! Unknown type of id.");
        return null;
    }
    logger.debug(">>> Sending message: " + messageText + " message size: " + messageText.length() + " to chat id: " + chatId);
    SendMessage sendMessage = new SendMessage(chatId, messageText);
    if (parseMode != null) {
        sendMessage = sendMessage.parseMode(parseMode).disableWebPagePreview(true);
    }
    return telegramBot.execute(sendMessage);
}
Also used : User(com.pengrad.telegrambot.model.User) Chat(com.pengrad.telegrambot.model.Chat) ByteString(okio.ByteString) SendMessage(com.pengrad.telegrambot.request.SendMessage)

Aggregations

SendMessage (com.pengrad.telegrambot.request.SendMessage)13 SendResponse (com.pengrad.telegrambot.response.SendResponse)4 Message (com.pengrad.telegrambot.model.Message)3 Chat (com.pengrad.telegrambot.model.Chat)2 TelegramBot (com.pengrad.telegrambot.TelegramBot)1 Update (com.pengrad.telegrambot.model.Update)1 User (com.pengrad.telegrambot.model.User)1 Keyboard (com.pengrad.telegrambot.model.request.Keyboard)1 ReplyKeyboardRemove (com.pengrad.telegrambot.model.request.ReplyKeyboardRemove)1 Optional (java.util.Optional)1 ByteString (okio.ByteString)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 Restaurant (org.wyvie.chehov.bot.commands.dailymenu.restaurant.Restaurant)1