com.pengrad.telegrambot.request.SendMessage Java Examples

The following examples show how to use com.pengrad.telegrambot.request.SendMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TelegramService.java    From Telephoto with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessageToAll(final List<IncomingMessage> incomingMessageList) {
    final Set<Prefs.UserPrefs> subscribers = PrefsController.instance.getPrefs().getEventListeners();
    es.submit(new Runnable() {
        @Override
        public void run() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(botService.getString(com.rai220.securityalarmbot.R.string.incoming_messages));
            for (IncomingMessage message : incomingMessageList) {
                stringBuilder.append(message.getPhone());
                if (!Strings.isNullOrEmpty(message.getName())) {
                    stringBuilder.append(" (").append(message.getName()).append(")");
                }
                stringBuilder.append(": \"").append(message.getMessage()).append("\"\n");
            }

            for (final Prefs.UserPrefs user : subscribers) {
                bot.execute(new SendMessage(user.lastChatId, stringBuilder.toString()));
            }
        }
    });
}
 
Example #2
Source File: TelegramService.java    From Telephoto with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyToOthers(final int userId, final String message) {
    Prefs prefs = PrefsController.instance.getPrefs();
    final Prefs.UserPrefs userChange = prefs.getUser(userId);
    final Set<Prefs.UserPrefs> subscribers = prefs.getEventListeners();
    es.submit(new Runnable() {
        @Override
        public void run() {
            String userName = "null";
            if (userChange != null) {
                userName = userChange.isNick ? "@".concat(userChange.userName) : userChange.userName;
            }
            for (final Prefs.UserPrefs user : subscribers) {
                if (user.id != userId) {
                    bot.execute(new SendMessage(user.lastChatId, userName + " " + message));
                }
            }
        }
    });
}
 
Example #3
Source File: TelegramService.java    From Telephoto with Apache License 2.0 6 votes vote down vote up
@Override
public void sendLocation(final Long chatId, final Location location) {
    if (location != null) {
        es.submit(new Runnable() {
            @Override
            public void run() {
                float latitude = (float) location.getLatitude();
                float longitude = (float) location.getLongitude();
                DateTime date = new DateTime(location.getTime());
                if (date.isBefore(DateTime.now().minusMinutes(60))) {
                    bot.execute(new SendMessage(chatId, botService.getString(R.string.last_date_location) +
                            date.toString(Constants.DATE_TIME_PATTERN)));
                }
                bot.execute(new SendLocation(chatId, latitude, longitude));
            }
        });
    }
}
 
Example #4
Source File: TelegramBotTest.java    From java-telegram-bot-api with Apache License 2.0 6 votes vote down vote up
@Test
public void loginButton() {
    String text = "login";
    String url = "http://pengrad.herokuapp.com/hello";
    SendResponse response = bot.execute(
            new SendMessage(chatId, "Login button").replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton[]{
                    new InlineKeyboardButton(text).loginUrl(new LoginUrl(url)
                            .forwardText("forwarded login")
                            .botUsername("pengrad_test_bot")
                            .requestWriteAccess(true))
            })));
    assertTrue(response.isOk());
    InlineKeyboardButton button = response.message().replyMarkup().inlineKeyboard()[0][0];
    assertEquals(text, button.text());
    assertEquals(url, button.url());
}
 
Example #5
Source File: TelegramService.java    From Telephoto with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessage(final Long id, final String message) {
    es.submit(new Runnable() {
        @Override
        public void run() {
            bot.execute(new SendMessage(id, message));
        }
    });
}
 
Example #6
Source File: TelegramService.java    From Telephoto with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessage(final Long id, final String message, final Keyboard keyboard) {
    es.submit(new Runnable() {
        @Override
        public void run() {
            bot.execute(new SendMessage(id, message).replyMarkup(keyboard));
        }
    });
}
 
Example #7
Source File: TelegramService.java    From Telephoto with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessageToAll(final String message) {
    final Set<Prefs.UserPrefs> subscribers = PrefsController.instance.getPrefs().getEventListeners();
    if (!subscribers.isEmpty()) {
        es.submit(new Runnable() {
            @Override
            public void run() {
                for (final Prefs.UserPrefs user : subscribers) {
                    bot.execute(new SendMessage(user.lastChatId, message));
                }
            }
        });
    }
}
 
Example #8
Source File: TelegramService.java    From Telephoto with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessageToAll(final String message, final Keyboard keyboard) {
    final Set<Prefs.UserPrefs> subscribers = PrefsController.instance.getPrefs().getEventListeners();
    es.submit(new Runnable() {
        @Override
        public void run() {
            for (final Prefs.UserPrefs user : subscribers) {
                bot.execute(new SendMessage(user.lastChatId, message).replyMarkup(keyboard));
            }
        }
    });
}
 
Example #9
Source File: TelegramBotManager.java    From teamcity-telegram-plugin with Apache License 2.0 5 votes vote down vote up
private void addUpdatesListener(TelegramBot bot) {
  bot.setUpdatesListener(updates -> {
    for (Update update: updates) {
      Message message = update.message();
      Long chatId = message.chat().id();
      SendMessage msg = new SendMessage(chatId,
          "Hello! Your chat id is '" + chatId + "'.\n" +
              "If you want to receive notifications about Teamcity events " +
              "please add this chat id in your Teamcity settings");
      bot.execute(msg);
    }
    return UpdatesListener.CONFIRMED_UPDATES_ALL;
  });
}
 
Example #10
Source File: AqivnBot.java    From java-telegram-bot-api with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    AqivnBot bot = new AqivnBot();
    bot.getWebPage("http://www.aqivn.org/en/")
            .map(bot::documentToAqiString)
            .subscribe(str -> {
                System.out.println(str);
                bot.bot.execute(new SendMessage(51314083, str));
            });


}
 
Example #11
Source File: TelegramBotTest.java    From java-telegram-bot-api with Apache License 2.0 5 votes vote down vote up
@Test
public void sendMessage() {
    SendResponse sendResponse = bot.execute(new SendMessage(chatId, "reply this message").replyMarkup(new ForceReply()));
    MessageTest.checkTextMessage(sendResponse.message());
    assertNotNull(sendResponse.message().from());

    sendResponse = bot.execute(new SendMessage(chatId, "remove keyboard")
            .replyMarkup(new ReplyKeyboardRemove())
            .disableNotification(true)
            .replyToMessageId(8087)
    );
    MessageTest.checkTextMessage(sendResponse.message());
    assertNotNull(sendResponse.message().replyToMessage());

    sendResponse = bot.execute(new SendMessage(chatId, "message with keyboard")
            .parseMode(ParseMode.HTML)
            .disableWebPagePreview(false)
            .replyMarkup(new ReplyKeyboardMarkup(new KeyboardButton[]{
                    new KeyboardButton("contact").requestContact(true),
                    new KeyboardButton("location").requestLocation(true)})
                    .oneTimeKeyboard(true)
                    .resizeKeyboard(true)
                    .selective(true)));
    MessageTest.checkTextMessage(sendResponse.message());

    sendResponse = bot.execute(new SendMessage(chatId, "simple buttons")
            .replyMarkup(new ReplyKeyboardMarkup(new String[]{"ok", "cancel"})));
    MessageTest.checkTextMessage(sendResponse.message());
}
 
Example #12
Source File: TelegramBotTest.java    From java-telegram-bot-api with Apache License 2.0 5 votes vote down vote up
@Test
public void sendMessageToChannel() {
    String url = "https://google.com/";
    SendMessage request = new SendMessage(channelName, "channel message [GG](" + url + ")").parseMode(ParseMode.Markdown);
    SendResponse sendResponse = bot.execute(request);
    Message message = sendResponse.message();
    MessageTest.checkTextMessage(message);
    assertEquals(url, message.entities()[0].url());
}
 
Example #13
Source File: TelegramBotTest.java    From java-telegram-bot-api with Apache License 2.0 5 votes vote down vote up
@Test
public void sendMessageToChannelId() {
    SendMessage request = new SendMessage(channelId, "channel by id message");
    SendResponse sendResponse = bot.execute(request);
    Message message = sendResponse.message();
    MessageTest.checkTextMessage(message);
}
 
Example #14
Source File: TelegramBotManager.java    From teamcity-telegram-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Send message to client
 * @param chatId client identifier
 * @param message text to send
 */
public synchronized void sendMessage(long chatId, @NotNull String message) {
  if (bot != null) {
    bot.execute(new SendMessage(chatId, message));
  }
}
 
Example #15
Source File: AqivnBot.java    From java-telegram-bot-api with Apache License 2.0 4 votes vote down vote up
@Override
void onWebhookUpdate(Update update) {
    bot.execute(new SendMessage(update.message().chat().id(), getAqi()).replyMarkup(simpleKeyboard()));
}
 
Example #16
Source File: TelegramBotTest.java    From java-telegram-bot-api with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteMessage() {
    Message message = bot.execute(new SendMessage(chatId, "message for delete")).message();
    BaseResponse response = bot.execute(new DeleteMessage(chatId, message.messageId()));
    assertTrue(response.isOk());
}