TelegramBot { public <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequest<T, R> request) { return api.send(request); } TelegramBot(String botToken);   TelegramBot(Builder builder); R execute(BaseRequest<T, R> request); void execute(T request, Callback<T, R> callback); String getFullFilePath(File file); byte[] getFileContent(File file); void setUpdatesListener(UpdatesListener listener); void setUpdatesListener(UpdatesListener listener, GetUpdates request); void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler); void setUpdatesListener(UpdatesListener listener, ExceptionHandler exceptionHandler, GetUpdates request); void removeGetUpdatesListener();  }
@Test public void getMe() { GetMeResponse response = bot.execute(new GetMe()); User user = response.user(); UserTest.checkUser(user); assertTrue(user.isBot()); assertFalse(user.canJoinGroups()); assertTrue(user.canReadAllGroupMessages()); assertTrue(user.supportsInlineQueries()); }
@Test public void answerCallback() { CallbackQuery callbackQuery = BotUtils.parseUpdate(testCallbackQuery).callbackQuery(); assertNotNull(callbackQuery); assertFalse(callbackQuery.id().isEmpty()); UserTest.checkUser(callbackQuery.from(), true); assertEquals(chatId, callbackQuery.from().id()); MessageTest.checkMessage(callbackQuery.message()); assertFalse(callbackQuery.chatInstance().isEmpty()); assertEquals("pengrad_test_game", callbackQuery.gameShortName()); assertNull(callbackQuery.inlineMessageId()); assertNull(callbackQuery.data()); BaseResponse response = bot.execute(new AnswerCallbackQuery(callbackQuery.id()) .text("answer callback") .url("telegram.me/pengrad_test_bot?game=pengrad_test_game") .showAlert(false) .cacheTime(1)); assertFalse(response.isOk()); assertEquals(400, response.errorCode()); assertEquals("Bad Request: query is too old and response timeout expired or query ID is invalid", response.description()); }
@Test public void getChat() throws MalformedURLException, URISyntaxException { Chat chat = bot.execute(new GetChat(groupId)).chat(); ChatTest.checkChat(chat, true); assertEquals(Chat.Type.supergroup, chat.type()); assertTrue(chat.title().contains("Test Bot Group")); assertTrue(chat.description().contains("New desc")); assertEquals(Integer.valueOf(10), chat.slowModeDelay()); assertNotNull(new URL(chat.inviteLink()).toURI()); if (chat.pinnedMessage() != null) MessageTest.checkMessage(chat.pinnedMessage()); assertNull(chat.allMembersAreAdministrators()); assertNull(chat.stickerSetName()); assertNull(chat.canSetStickerSet()); chat = bot.execute(new GetChat(chatId)).chat(); assertNotNull(chat.firstName()); assertNotNull(chat.lastName()); }
@Test public void leaveChat() { BaseResponse response = bot.execute(new LeaveChat(chatId)); assertFalse(response.isOk()); assertEquals(400, response.errorCode()); assertEquals("Bad Request: chat member status can't be changed in private chats", response.description()); }
@Test public void getChatAdministrators() { GetChatAdministratorsResponse response = bot.execute(new GetChatAdministrators(groupId)); for (ChatMember chatMember : response.administrators()) { ChatMemberTest.check(chatMember); if (chatMember.user().firstName().equals("Test Bot")) { assertFalse(chatMember.canBeEdited()); assertTrue(chatMember.canChangeInfo()); assertTrue(chatMember.canDeleteMessages()); assertTrue(chatMember.canInviteUsers()); assertTrue(chatMember.canRestrictMembers()); assertTrue(chatMember.canPinMessages()); assertTrue(chatMember.canPromoteMembers()); } } }
@Test public void getChatMember() { restrictChatMember(); ChatMember chatMember = bot.execute(new GetChatMember(groupId, memberBot)).chatMember(); ChatMemberTest.check(chatMember); assertEquals(ChatMember.Status.restricted, chatMember.status()); assertEquals(Integer.valueOf(0), chatMember.untilDate()); assertNull(chatMember.canPostMessages()); assertNull(chatMember.canEditMessages()); assertTrue(chatMember.isMember()); assertTrue(chatMember.canChangeInfo()); assertTrue(chatMember.canInviteUsers()); assertTrue(chatMember.canPinMessages()); assertTrue(chatMember.canSendPolls()); assertTrue(chatMember.canSendMessages()); assertFalse(chatMember.canSendMediaMessages()); assertFalse(chatMember.canSendOtherMessages()); assertFalse(chatMember.canAddWebPagePreviews()); }
@Test public void getChatMembersCount() { GetChatMembersCountResponse response = bot.execute(new GetChatMembersCount(chatId)); assertEquals(2, response.count()); }
@Test public void getUserProfilePhotos() { int offset = 1; GetUserProfilePhotosResponse response = bot.execute(new GetUserProfilePhotos(chatId).limit(100).offset(offset)); UserProfilePhotos photos = response.photos(); assertEquals(photos.totalCount() - offset, photos.photos().length); for (PhotoSize[] photo : photos.photos()) { PhotoSizeTest.checkPhotos(photo); } if (photos.totalCount() > 1) { photos = bot.execute(new GetUserProfilePhotos(chatId).limit(1)).photos(); assertEquals(1, photos.photos().length); } }
@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("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("ok", "cancel"))); MessageTest.checkTextMessage(sendResponse.message()); }
@Test public void sendMessageToChannel() { String url = "https: 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()); }
@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); }
@Test public void getUpdates() { GetUpdates getUpdates = new GetUpdates() .offset(874205003) .allowedUpdates("") .timeout(0) .limit(100); assertEquals(100, getUpdates.getLimit()); GetUpdatesResponse response = bot.execute(getUpdates); UpdateTest.check(response.updates()); }
@Test public void forwardMessage() { SendResponse response = bot.execute(new ForwardMessage(chatId, chatId, forwardMessageId).disableNotification(true)); Message message = response.message(); MessageTest.checkMessage(message); assertNotNull(message.forwardDate()); assertNotNull(message.forwardSenderName()); assertNull(message.forwardFrom()); User viaBot = message.viaBot(); UserTest.checkUser(viaBot); assertEquals("gif", viaBot.username()); message = bot.execute(new ForwardMessage(chatId, chatId, forwardMessageIdUser)).message(); MessageTest.checkMessage(message); assertNotNull(message.forwardDate()); assertNull(message.forwardSenderName()); assertNotNull(message.forwardFrom()); message = bot.execute(new ForwardMessage(channelName, channelName, 651)).message(); assertNotNull(message.authorSignature()); assertNotNull(message.forwardSignature()); assertEquals(Integer.valueOf(651), message.forwardFromMessageId()); Chat chat = message.forwardFromChat(); assertEquals(channelName, "@" + chat.username()); assertEquals(Chat.Type.channel, chat.type()); assertNull(message.forwardSenderName()); message = bot.execute(new ForwardMessage(chatId, groupId, 352)).message(); assertEquals(MessageEntity.Type.text_mention, message.entities()[0].type()); assertNotNull(message.entities()[0].user()); assertNotNull(message.forwardSenderName()); }
@Test public void sendAudio() { Message message = bot.execute(new SendAudio(chatId, audioFileId)).message(); MessageTest.checkMessage(message); AudioTest.checkAudio(message.audio(), false); message = bot.execute(new SendAudio(chatId, audioFile).thumb(thumbFile)).message(); MessageTest.checkMessage(message); AudioTest.checkAudio(message.audio()); assertEquals(thumbSize, message.audio().thumb().fileSize()); String cap = "http: ParseMode parseMode = ParseMode.HTML; int duration = 100; SendAudio sendAudio = new SendAudio(chatId, audioBytes).thumb(thumbBytes).duration(duration) .caption(cap).parseMode(parseMode).performer(performer).title(title); message = bot.execute(sendAudio).message(); MessageTest.checkMessage(message); Audio audio = message.audio(); AudioTest.checkAudio(audio); assertEquals(cap.replace("<b>", "").replace("</b>", ""), message.caption()); assertEquals((Integer) 100, audio.duration()); assertEquals(performer, audio.performer()); assertEquals(title, audio.title()); assertEquals(thumbSize, audio.thumb().fileSize()); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.url, captionEntity.type()); assertEquals((Integer) 0, captionEntity.offset()); assertEquals((Integer) 12, captionEntity.length()); captionEntity = message.captionEntities()[1]; assertEquals(MessageEntity.Type.bold, captionEntity.type()); assertEquals((Integer) 14, captionEntity.offset()); assertEquals((Integer) 4, captionEntity.length()); assertEquals(MessageEntity.Type.hashtag, message.captionEntities()[2].type()); }
@Test public void underlineStrikethroughMessageEntity() { String cap = "<u>under1</u> <ins>under2</ins> <s>strike1</s> <strike>strike2</strike> <del>strike3</del>"; cap += " <u><del>nested-tag</del></u>"; ParseMode parseMode = ParseMode.HTML; SendAudio sendAudio = new SendAudio(chatId, audioFileId).caption(cap).parseMode(parseMode); Message message = bot.execute(sendAudio).message(); MessageTest.checkMessage(message); String htmlCaption = cap .replace("<u>", "").replace("</u>", "") .replace("<ins>", "").replace("</ins>", "") .replace("<s>", "").replace("</s>", "") .replace("<strike>", "").replace("</strike>", "") .replace("<del>", "").replace("</del>", ""); assertEquals(htmlCaption, message.caption()); assertEquals(7, message.captionEntities().length); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.underline, captionEntity.type()); assertEquals((Integer) 0, captionEntity.offset()); assertEquals((Integer) 6, captionEntity.length()); captionEntity = message.captionEntities()[1]; assertEquals(MessageEntity.Type.underline, captionEntity.type()); assertEquals((Integer) 7, captionEntity.offset()); assertEquals((Integer) 6, captionEntity.length()); captionEntity = message.captionEntities()[2]; assertEquals(MessageEntity.Type.strikethrough, captionEntity.type()); assertEquals((Integer) 14, captionEntity.offset()); assertEquals((Integer) 7, captionEntity.length()); captionEntity = message.captionEntities()[3]; assertEquals(MessageEntity.Type.strikethrough, captionEntity.type()); assertEquals((Integer) 22, captionEntity.offset()); assertEquals((Integer) 7, captionEntity.length()); captionEntity = message.captionEntities()[4]; assertEquals(MessageEntity.Type.strikethrough, captionEntity.type()); assertEquals((Integer) 30, captionEntity.offset()); assertEquals((Integer) 7, captionEntity.length()); captionEntity = message.captionEntities()[5]; assertEquals(MessageEntity.Type.underline, captionEntity.type()); assertEquals((Integer) 38, captionEntity.offset()); assertEquals((Integer) 10, captionEntity.length()); captionEntity = message.captionEntities()[6]; assertEquals(MessageEntity.Type.strikethrough, captionEntity.type()); assertEquals((Integer) 38, captionEntity.offset()); assertEquals((Integer) 10, captionEntity.length()); }
@Test public void underlineStrikethroughMarkdown() { String cap = "__under1__ ~strike1~ __~nested~__"; ParseMode parseMode = ParseMode.MarkdownV2; SendAudio sendAudio = new SendAudio(chatId, audioFileId).caption(cap).parseMode(parseMode); Message message = bot.execute(sendAudio).message(); MessageTest.checkMessage(message); String htmlCaption = cap.replace("__", "").replace("~", ""); assertEquals(htmlCaption, message.caption()); assertEquals(4, message.captionEntities().length); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.underline, captionEntity.type()); assertEquals((Integer) 0, captionEntity.offset()); assertEquals((Integer) 6, captionEntity.length()); captionEntity = message.captionEntities()[1]; assertEquals(MessageEntity.Type.strikethrough, captionEntity.type()); assertEquals((Integer) 7, captionEntity.offset()); assertEquals((Integer) 7, captionEntity.length()); captionEntity = message.captionEntities()[2]; assertEquals(MessageEntity.Type.underline, captionEntity.type()); assertEquals((Integer) 15, captionEntity.offset()); assertEquals((Integer) 6, captionEntity.length()); captionEntity = message.captionEntities()[3]; assertEquals(MessageEntity.Type.strikethrough, captionEntity.type()); assertEquals((Integer) 15, captionEntity.offset()); assertEquals((Integer) 6, captionEntity.length()); }
@Test public void preMessageEntity() { String cap = "```java\n" + "String s = new String();\n" + "```"; ParseMode parseMode = ParseMode.MarkdownV2; SendAudio sendAudio = new SendAudio(chatId, audioFileId).caption(cap).parseMode(parseMode); Message message = bot.execute(sendAudio).message(); MessageTest.checkMessage(message); assertEquals(1, message.captionEntities().length); assertEquals("java", message.captionEntities()[0].language()); }
@Test public void sendDocument() { Message message = bot.execute(new SendDocument(chatId, docFileId)).message(); MessageTest.checkMessage(message); DocumentTest.check(message.document()); message = bot.execute(new SendDocument(chatId, docBytes).thumb(thumbBytes)).message(); MessageTest.checkMessage(message); DocumentTest.check(message.document()); assertEquals(thumbSize, message.document().thumb().fileSize()); String caption = "caption <b>bold</b>", fileName = "my doc.zip"; ParseMode parseMode = ParseMode.HTML; message = bot.execute( new SendDocument(chatId, docFile).fileName(fileName).thumb(thumbFile).caption(caption).parseMode(parseMode)) .message(); MessageTest.checkMessage(message); DocumentTest.check(message.document()); assertEquals(caption.replace("<b>", "").replace("</b>", ""), message.caption()); assertEquals(fileName, message.document().fileName()); assertEquals(thumbSize, message.document().thumb().fileSize()); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.bold, captionEntity.type()); assertEquals((Integer) 8, captionEntity.offset()); assertEquals((Integer) 4, captionEntity.length()); }
@Test public void sendPhoto() { Message message = bot.execute(new SendPhoto(chatId, photoFileId)).message(); MessageTest.checkMessage(message); PhotoSizeTest.checkPhotos(false, message.photo()); message = bot.execute(new SendPhoto(chatId, imageFile)).message(); MessageTest.checkMessage(message); PhotoSizeTest.checkPhotos(message.photo()); String caption = "caption <b>bold</b>"; message = bot.execute(new SendPhoto(channelName, imageBytes).caption(caption).parseMode(ParseMode.HTML)).message(); MessageTest.checkMessage(message); assertEquals(caption.replace("<b>", "").replace("</b>", ""), message.caption()); PhotoSizeTest.checkPhotos(message.photo()); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.bold, captionEntity.type()); assertEquals((Integer) 8, captionEntity.offset()); assertEquals((Integer) 4, captionEntity.length()); }
@Test public void sendSticker() { Message message = bot.execute(new SendSticker(chatId, stickerFileAnim)).message(); MessageTest.checkMessage(message); StickerTest.check(message.sticker(), false, true); assertTrue(message.sticker().isAnimated()); message = bot.execute(new SendSticker(chatId, stickerId)).message(); MessageTest.checkMessage(message); StickerTest.check(message.sticker(), true, false); assertFalse(message.sticker().isAnimated()); message = bot.execute(new SendSticker(chatId, imageFile)).message(); MessageTest.checkMessage(message); StickerTest.check(message.sticker(), false, true); message = bot.execute(new SendSticker(chatId, imageBytes)).message(); MessageTest.checkMessage(message); StickerTest.check(message.sticker(), false, true); }
@Test public void sendVideo() { Message message = bot.execute(new SendVideo(chatId, videoFileId)).message(); MessageTest.checkMessage(message); VideoTest.check(message.video(), false); message = bot.execute(new SendVideo(chatId, videoFile).thumb(thumbFile)).message(); MessageTest.checkMessage(message); VideoTest.check(message.video()); assertNotEquals("telegram should generate thumb", thumbSize, message.video().thumb().fileSize()); String caption = "caption <b>bold</b>"; Integer duration = 100; message = bot.execute( new SendVideo(chatId, videoBytes).thumb(thumbBytes) .caption(caption).parseMode(ParseMode.HTML) .duration(duration).height(1).width(2).supportsStreaming(true)) .message(); MessageTest.checkMessage(message); assertEquals(caption.replace("<b>", "").replace("</b>", ""), message.caption()); Video video = message.video(); VideoTest.check(message.video()); assertEquals(duration, video.duration()); assertEquals((Integer) 120, video.height()); assertEquals((Integer) 400, video.width()); assertEquals("video/mp4", video.mimeType()); assertNotEquals("telegram should generate thumb", thumbSize, video.thumb().fileSize()); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.bold, captionEntity.type()); assertEquals((Integer) 8, captionEntity.offset()); assertEquals((Integer) 4, captionEntity.length()); }
@Test public void sendVoice() { Message message = bot.execute(new SendVoice(chatId, voiceFileId)).message(); MessageTest.checkMessage(message); VoiceTest.check(message.voice(), false); message = bot.execute(new SendVoice(chatId, audioFile)).message(); MessageTest.checkMessage(message); VoiceTest.check(message.voice()); String caption = "caption <b>bold</b>"; Integer duration = 100; message = bot.execute(new SendVoice(chatId, audioBytes).caption(caption).parseMode(ParseMode.HTML).duration(duration)).message(); MessageTest.checkMessage(message); assertEquals(caption.replace("<b>", "").replace("</b>", ""), message.caption()); VoiceTest.check(message.voice()); assertEquals(duration, message.voice().duration()); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.bold, captionEntity.type()); assertEquals((Integer) 8, captionEntity.offset()); assertEquals((Integer) 4, captionEntity.length()); }
@Test public void kickChatMember() { BaseResponse response = bot.execute(new KickChatMember(channelName, chatId).untilDate(123)); assertFalse(response.isOk()); assertEquals(400, response.errorCode()); assertEquals("Bad Request: can't remove chat owner", response.description()); }
@Test public void getWebhookInfo() { GetWebhookInfoResponse response = bot.execute(new GetWebhookInfo()); WebhookInfoTest.check(response.webhookInfo()); }
@Test public void setWebhook() throws IOException { String url = "https: Integer maxConnections = 100; String[] allowedUpdates = {"message", "callback_query"}; BaseResponse response = bot.execute(new SetWebhook().url(url).certificate(new File(certificateFile)) .maxConnections(100).allowedUpdates(allowedUpdates)); assertTrue(response.isOk()); WebhookInfo webhookInfo = bot.execute(new GetWebhookInfo()).webhookInfo(); assertEquals(url, webhookInfo.url()); assertTrue(webhookInfo.hasCustomCertificate()); assertEquals(maxConnections, webhookInfo.maxConnections()); assertArrayEquals(allowedUpdates, webhookInfo.allowedUpdates()); assertNotNull(webhookInfo.lastErrorDate()); assertNotNull(webhookInfo.lastErrorMessage()); response = bot.execute(new SetWebhook().url("https: .certificate(Files.readAllBytes(new File(certificateFile).toPath())).allowedUpdates("")); assertTrue(response.isOk()); response = bot.execute(new SetWebhook()); assertTrue(response.isOk()); }
@Test public void deleteWebhook() { BaseResponse response = bot.execute(new DeleteWebhook()); assertTrue(response.isOk()); }
@Test public void sendGame() { InlineKeyboardButton[] buttons = { new InlineKeyboardButton("inline game").callbackGame("pengrad test game description"), new InlineKeyboardButton("inline ok").callbackData("callback ok"), new InlineKeyboardButton("cancel").callbackData("callback cancel"), new InlineKeyboardButton("url").url(someUrl), new InlineKeyboardButton("switch inline").switchInlineQuery("query"), new InlineKeyboardButton("switch inline current").switchInlineQueryCurrentChat("query"), }; InlineKeyboardButton[][] inlineKeyboard = new InlineKeyboardButton[1][]; inlineKeyboard[0] = buttons; InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup(inlineKeyboard); String desc = "pengrad_test_game"; Message message = bot.execute(new SendGame(chatId, desc).replyMarkup(keyboardMarkup)).message(); MessageTest.checkMessage(message); Game game = message.game(); GameTest.check(game); assertEquals(desc, game.description()); InlineKeyboardButton[] actualButtons = message.replyMarkup().inlineKeyboard()[0]; assertEquals(buttons.length, actualButtons.length); assertNotNull(actualButtons[0].callbackGame()); for (int i = 1; i < buttons.length; i++) { assertEquals(buttons[i].text(), actualButtons[i].text()); assertFalse(buttons[i].isPay()); } assertEquals(buttons[1].callbackData(), actualButtons[1].callbackData()); assertEquals(buttons[2].callbackData(), actualButtons[2].callbackData()); assertEquals(buttons[3].url(), actualButtons[3].url()); assertEquals(buttons[4].switchInlineQuery(), actualButtons[4].switchInlineQuery()); assertEquals(buttons[5].switchInlineQueryCurrentChat(), actualButtons[5].switchInlineQueryCurrentChat()); }
@Test public void setGameScore() { int res = (int) (System.currentTimeMillis() / 1000); BaseResponse response = bot.execute(new SetGameScore(chatId, res, "AgAAAPrwAQCj_Q4D2s-51_8jsuU")); assertTrue(response.isOk()); SendResponse sendResponse = (SendResponse) bot.execute( new SetGameScore(chatId, res + 1, chatId, 8162).force(true).disableEditMessage(true)); GameTest.check(sendResponse.message().game()); }
@Test public void getGameHighScores() { GameHighScore[] scores = bot.execute(new GetGameHighScores(chatId, "AgAAAPrwAQCj_Q4D2s-51_8jsuU")).result(); GameHighScoreTest.check(scores); scores = bot.execute(new GetGameHighScores(chatId, chatId, 8162)).result(); GameHighScoreTest.check(scores); }
@Test public void sendLocation() { Float lat = 21.999998f, lng = 105.2f; Location location = bot.execute(new SendLocation(chatId, lat, lng).livePeriod(60)).message().location(); assertEquals(lat, location.latitude()); assertEquals(lng, location.longitude()); }
@Test public void sendVenue() { Float lat = 21.999998f, lng = 105.2f; String title = "title", address = "addr", frsqrId = "asdfasdf", frsqrType = "frType"; Venue venue = bot.execute(new SendVenue(chatId, lat, lng, title, address) .foursquareId(frsqrId) .foursquareType(frsqrType) ).message().venue(); assertEquals(lat, venue.location().latitude()); assertEquals(lng, venue.location().longitude()); assertEquals(address, venue.address()); assertEquals(title, venue.title()); assertEquals(frsqrId, venue.foursquareId()); assertEquals(frsqrType, venue.foursquareType()); }
@Test public void sendContact() { String phone = "000111", name = "first", lastName = "last", vcard = "ok vcard"; Contact contact = bot.execute(new SendContact(chatId, phone, name).lastName(lastName).vcard(vcard)).message().contact(); assertEquals(phone, contact.phoneNumber()); assertEquals(name, contact.firstName()); assertEquals(lastName, contact.lastName()); assertEquals(vcard, contact.vcard()); assertNull(contact.userId()); }
@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()); }
@Test public void unbanChatMember() { BaseResponse response = bot.execute(new UnbanChatMember(channelName, chatId)); assertFalse(response.isOk()); assertEquals(400, response.errorCode()); assertEquals("Bad Request: can't remove chat owner", response.description()); }
@Test public void sendChatAction() { assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.typing.name())).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.typing)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_photo)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.record_video)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_video)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.record_audio)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_audio)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_document)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.find_location)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.record_video_note)).isOk()); assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_video_note)).isOk()); }
@Test public void sendVideoNote() { SendResponse response = bot.execute(new SendVideoNote(chatId, "DQADAgADmQADYgwpSbum1JrxPsbmAg")); VideoNoteCheck.check(response.message().videoNote()); }
@Test public void sendVideoNoteFile() { SendResponse response = bot.execute(new SendVideoNote(chatId, videoNoteFile).thumb(thumbFile).length(20).duration(30)); VideoNoteCheck.check(response.message().videoNote(), true); assertNotEquals("telegram should generate thumb", thumbSize, response.message().videoNote().thumb().fileSize()); response = bot.execute(new SendVideoNote(chatId, videoNoteBytes).thumb(thumbBytes)); VideoNoteCheck.check(response.message().videoNote(), true); assertNotEquals("telegram should generate thumb", thumbSize, response.message().videoNote().thumb().fileSize()); }
@Test public void setChatAdministratorCustomTitle() { BaseResponse response = bot.execute(new PromoteChatMember(groupId, memberBot).canPromoteMembers(true)); assertTrue(response.isOk()); String customTitle = "aqi " + new Random().nextInt(999999); response = bot.execute(new SetChatAdministratorCustomTitle(groupId, memberBot, customTitle)); assertTrue(response.isOk()); ChatMember member = bot.execute(new GetChatMember(groupId, memberBot)).chatMember(); ChatMemberTest.check(member); assertEquals(customTitle, member.customTitle()); }
@Test public void setChatPermissions() { for (boolean bool : new boolean[]{true, false}) { ChatPermissions setPerms = new ChatPermissions(); setPerms.canSendMessages(bool); setPerms.canSendMediaMessages(bool); setPerms.canSendPolls(bool); setPerms.canSendOtherMessages(bool); setPerms.canAddWebPagePreviews(bool); setPerms.canChangeInfo(bool); setPerms.canInviteUsers(bool); setPerms.canPinMessages(bool); BaseResponse response = bot.execute(new SetChatPermissions(groupId, setPerms)); assertTrue(response.isOk()); Chat chat = bot.execute(new GetChat(groupId)).chat(); ChatPermissions permissions = chat.permissions(); if (bool) { assertTrue(permissions.canSendMessages()); assertTrue(permissions.canSendMediaMessages()); assertTrue(permissions.canSendPolls()); assertTrue(permissions.canSendOtherMessages()); assertTrue(permissions.canAddWebPagePreviews()); assertTrue(permissions.canChangeInfo()); assertTrue(permissions.canInviteUsers()); assertTrue(permissions.canPinMessages()); } else { assertFalse(permissions.canSendMessages()); assertFalse(permissions.canSendMediaMessages()); assertFalse(permissions.canSendPolls()); assertFalse(permissions.canSendOtherMessages()); assertFalse(permissions.canAddWebPagePreviews()); assertFalse(permissions.canChangeInfo()); assertFalse(permissions.canInviteUsers()); assertFalse(permissions.canPinMessages()); } } }
@Test public void exportChatInviteLink() { StringResponse response = bot.execute(new ExportChatInviteLink(groupId)); assertTrue(response.isOk()); assertNotNull(response.result()); }
@Test public void setChatPhoto() throws IOException { BaseResponse response = bot.execute(new SetChatPhoto(groupId, imageFile)); assertTrue(response.isOk()); byte[] bytes = Files.readAllBytes(imageFile.toPath()); response = bot.execute(new SetChatPhoto(groupId, bytes)); assertTrue(response.isOk()); }
@Test public void deleteChatPhoto() { BaseResponse response = bot.execute(new DeleteChatPhoto(groupId)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: CHAT_NOT_MODIFIED", response.description()); } }
@Test public void setChatTitle() { BaseResponse response = bot.execute(new SetChatTitle(groupId, "Test Bot Group " + System.currentTimeMillis())); assertTrue(response.isOk()); }
@Test public void setChatDescription() { BaseResponse response = bot.execute(new SetChatDescription(groupId, "New desc " + System.currentTimeMillis())); assertTrue(response.isOk()); }
@Test public void restrictChatMember() { BaseResponse response = bot.execute( new RestrictChatMember(groupId, memberBot) .untilDate(100) .canSendMessages(false) .canSendMediaMessages(false) .canSendOtherMessages(false) .canAddWebPagePreviews(false)); assertTrue(response.isOk()); ChatPermissions permissions = new ChatPermissions() .canChangeInfo(true) .canInviteUsers(true) .canPinMessages(true) .canSendPolls(true); response = bot.execute(new RestrictChatMember(groupId, memberBot, permissions)); assertTrue(response.isOk()); }
@Test public void pinChatMessage() { BaseResponse response = bot.execute(new PinChatMessage(groupId, 18).disableNotification(false)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: CHAT_NOT_MODIFIED", response.description()); } }
@Test public void unpinChatMessage() { BaseResponse response = bot.execute(new UnpinChatMessage(groupId)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: CHAT_NOT_MODIFIED", response.description()); } }
@Test public void getStickerSet() { GetStickerSetResponse response = bot.execute(new GetStickerSet(stickerSet)); StickerSet stickerSet = response.stickerSet(); for (Sticker sticker : response.stickerSet().stickers()) { StickerTest.check(sticker, true, true); } assertTrue(stickerSet.containsMasks()); assertEquals(TelegramBotTest.stickerSet, stickerSet.name()); assertEquals("test1", stickerSet.title()); assertFalse(stickerSet.isAnimated()); Sticker sticker = stickerSet.stickers()[0]; assertEquals(TelegramBotTest.stickerSet, sticker.setName()); MaskPosition maskPosition = sticker.maskPosition(); assertEquals(MaskPosition.Point.forehead.name(), maskPosition.point()); assertEquals(0f, maskPosition.xShift(), 0); assertEquals(0f, maskPosition.yShift(), 0); assertEquals(1f, maskPosition.scale(), 0); }
@Test public void uploadStickerFile() throws IOException { byte[] bytes = Files.readAllBytes(stickerFile.toPath()); GetFileResponse response = bot.execute(new UploadStickerFile(chatId, bytes)); FileTest.check(response.file(), false); }
@Test public void createNewStickerSet() { BaseResponse response = bot.execute( new CreateNewStickerSet(chatId, "test" + System.currentTimeMillis() + "_by_pengrad_test_bot", "test1", stickerFile, "\uD83D\uDE00") .containsMasks(true) .maskPosition(new MaskPosition(MaskPosition.Point.forehead, 0f, 0f, 1f))); assertTrue(response.isOk()); }
@Test public void addStickerToSet() { BaseResponse response = bot.execute( new AddStickerToSet(chatId, stickerSet, "BQADAgADuAAD7yupS4eB23UmZhGuAg", "\uD83D\uDE15") .maskPosition(new MaskPosition("eyes", 0f, 0f, 1f))); assertTrue(response.isOk()); }
@Test public void createSetAndAddStickerTgs() { String setName = "test" + System.currentTimeMillis() + "_by_pengrad_test_bot"; BaseResponse response = bot.execute( CreateNewStickerSet.tgsSticker(chatId, setName, "test1", "\uD83D\uDE00", stickerFileAnim)); assertTrue(response.isOk()); response = bot.execute( AddStickerToSet.tgsSticker(chatId, setName, "\uD83D\uDE15", stickerFileAnim)); assertTrue(response.isOk()); }
@Test public void setStickerPositionInSet() { GetStickerSetResponse setResponse = bot.execute(new GetStickerSet(stickerSet)); Sticker sticker = setResponse.stickerSet().stickers()[0]; BaseResponse response = bot.execute(new SetStickerPositionInSet(sticker.fileId(), 0)); assertTrue(response.isOk()); }
@Test public void deleteStickerFromSet() { BaseResponse response = bot.execute(new AddStickerToSet(chatId, stickerSet, stickerFile, "\uD83D\uDE15")); assertTrue(response.isOk()); GetStickerSetResponse setResponse = bot.execute(new GetStickerSet(stickerSet)); int size = setResponse.stickerSet().stickers().length; Sticker sticker = setResponse.stickerSet().stickers()[size - 1]; response = bot.execute(new DeleteStickerFromSet(sticker.fileId())); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: STICKERSET_NOT_MODIFIED", response.description()); } }
@Test public void setStickerSetThumb() { String thumbFile = "CAACAgIAAxkBAAJ0ll6DO4bNCynpfZmS6g-YcGY2zrP5AAIBAAPANk8TGC5zMKs_LVEYBA"; BaseResponse response = bot.execute(new SetStickerSetThumb(stickerSetAnim, chatId, thumbFile)); assertTrue(response.isOk()); StickerSet set = bot.execute(new GetStickerSet(stickerSetAnim)).stickerSet(); assertTrue(set.isAnimated()); PhotoSize thumb = set.thumb(); PhotoSizeTest.checkPhotos(thumb); assertEquals(Integer.valueOf(100), thumb.width()); assertEquals(Integer.valueOf(100), thumb.height()); assertEquals(Integer.valueOf(8244), thumb.fileSize()); response = bot.execute(new SetStickerSetThumb(stickerSetAnim, chatId)); assertTrue(response.isOk()); }
@Test public void promoteChatMember() { BaseResponse response = bot.execute( new PromoteChatMember(groupId, memberBot) .canChangeInfo(false) .canPostMessages(false) .canEditMessages(false) .canDeleteMessages(false) .canInviteUsers(false) .canRestrictMembers(false) .canPinMessages(false) .canPromoteMembers(false)); assertTrue(response.isOk()); }
@Test public void editMessageLiveLocation() { BaseResponse response = bot.execute(new EditMessageLiveLocation(chatId, 10009, 21, 105) .replyMarkup(new InlineKeyboardMarkup())); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: message can't be edited", response.description()); } String buttonText = "btn_" + System.currentTimeMillis(); response = bot.execute( new EditMessageLiveLocation("AgAAAPrwAQCj_Q4D2s-51_8jsuU", 21, 102) .replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton(buttonText).callbackGame(buttonText))) ); assertTrue(response.isOk()); }
@Test public void stopMessageLiveLocation() { BaseResponse response = bot.execute(new StopMessageLiveLocation(chatId, 10009)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: message can't be edited", response.description()); } response = bot.execute(new StopMessageLiveLocation("AgAAAPrwAQCj_Q4D2s-51_8jsuU")); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message", response.description()); } }
@Test public void setChatStickerSet() { BaseResponse response = bot.execute(new SetChatStickerSet(groupId, "PengradTest")); assertFalse(response.isOk()); assertEquals(400, response.errorCode()); }
@Test public void deleteChatStickerSet() { BaseResponse response = bot.execute(new DeleteChatStickerSet(groupId)); assertFalse(response.isOk()); assertEquals(400, response.errorCode()); }
@Test public void sendMediaGroup() { MessagesResponse response = bot.execute(new SendMediaGroup(chatId, new InputMediaPhoto(photoFileId), new InputMediaPhoto(imageFile).caption("some caption <b>bold</b>").parseMode(ParseMode.HTML), new InputMediaPhoto(imageBytes), new InputMediaVideo(videoFileId), new InputMediaVideo(videoFile), new InputMediaVideo(videoBytes).caption("my video <b>bold</b>").parseMode(ParseMode.HTML) .duration(10).width(11).height(12).supportsStreaming(true) )); assertTrue(response.isOk()); assertEquals(6, response.messages().length); String mediaGroupId = response.messages()[0].mediaGroupId(); assertNotNull(mediaGroupId); int messagesWithCaption = 0; for (Message message : response.messages()) { assertEquals(mediaGroupId, message.mediaGroupId()); if (message.caption() != null) { assertEquals(MessageEntity.Type.bold, message.captionEntities()[0].type()); messagesWithCaption++; } } assertEquals(2, messagesWithCaption); }
@Test public void editMessageMedia() { int messageId = 13541; SendResponse response; response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaDocument(docFile).thumb(thumbFile))); assertEquals((Integer) 14, response.message().document().fileSize()); assertEquals(thumbSize, response.message().document().thumb().fileSize()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaDocument(docBytes).thumb(thumbBytes))); assertEquals((Integer) 14, response.message().document().fileSize()); assertEquals(thumbSize, response.message().document().thumb().fileSize()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaDocument(docFileId))); MessageTest.checkMessage(response.message()); DocumentTest.check(response.message().document()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAnimation(gifFile))); assertEquals(Integer.valueOf(1), response.message().animation().duration()); Integer durationAnim = 17, width = 21, height = 22; response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAnimation(gifBytes).duration(durationAnim).width(width).height(height) )); Animation animation = response.message().animation(); assertEquals(Integer.valueOf(1), animation.duration()); assertEquals(width, animation.width()); assertEquals(height, animation.height()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAnimation(gifFileId))); assertTrue(response.isOk()); AnimationCheck.check(response.message().animation()); assertEquals(Integer.valueOf(3), response.message().animation().duration()); assertNotEquals(gifFileId, response.message().animation().fileId()); assertNotNull(response.message().document()); assertEquals((Integer) 57527, response.message().document().fileSize()); assertEquals("video/mp4", response.message().document().mimeType()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAudio(audioFileId))); assertEquals((Integer) 10286, response.message().audio().fileSize()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAudio(audioFile))); assertEquals((Integer) 10286, response.message().audio().fileSize()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAudio(audioBytes))); assertEquals((Integer) 10286, response.message().audio().fileSize()); Integer duration = 34; String performer = "some performer", title = "just a title"; response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaAudio(audioFile).duration(duration).performer(performer).title(title) )); Audio audio = response.message().audio(); assertEquals((Integer) 10286, audio.fileSize()); assertEquals(duration, audio.duration()); assertEquals(performer, audio.performer()); assertEquals(title, audio.title()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaPhoto(photoFileId).thumb(thumbFile))); assertNotNull(response.message().photo()); response = (SendResponse) bot.execute(new EditMessageMedia(chatId, messageId, new InputMediaVideo(videoFileId).thumb(thumbFile))); assertNotNull(response.message().video()); }
@Test public void sendAnimation() { Integer width = 340, height = 240; String caption = "gif *file*", captionCheck = "gif file"; SendResponse response = bot.execute(new SendAnimation(chatId, gifFile) .duration(222).width(width).height(height).thumb(thumbFile) .caption(caption).parseMode(ParseMode.Markdown)); assertTrue(response.isOk()); Animation animation = response.message().animation(); assertEquals((Integer) 1, animation.duration()); assertEquals(width, animation.width()); assertEquals(height, animation.height()); assertNotEquals("telegram should generate thumb", thumbSize, animation.thumb().fileSize()); assertEquals(captionCheck, response.message().caption()); assertEquals(MessageEntity.Type.bold, response.message().captionEntities()[0].type()); response = bot.execute(new SendAnimation(chatId, gifBytes).thumb(thumbBytes)); animation = response.message().animation(); assertEquals((Integer) 1, animation.duration()); assertEquals((Integer) 160, animation.width()); assertEquals((Integer) 160, animation.height()); assertNotEquals("telegram should generate thumb", thumbSize, animation.thumb().fileSize()); response = bot.execute(new SendAnimation(chatId, gifFileId)); animation = response.message().animation(); assertEquals((Integer) 3, animation.duration()); assertEquals((Integer) 128, animation.width()); assertEquals((Integer) 128, animation.height()); }
@Test public void setPassportDataErrors() { BaseResponse response = bot.execute(new SetPassportDataErrors(chatId, new PassportElementErrorDataField("personal_details", "first_name", "TueU2/SswOD5wgQ6uXQ62mJrr0Jdf30r/QQ/jyETHFM=", "error in page 1") )); System.out.println(response); assertTrue(response.isOk()); }
@Test public void sendPoll() { String question = "Question ?"; String[] answers = {"Answer 1", "Answer 2"}; SendResponse sendResponse = bot.execute( new SendPoll(groupId, question, answers) .isAnonymous(false) .type(Poll.Type.quiz) .allowsMultipleAnswers(false) .correctOptionId(1) .isClosed(false) .explanation("Some __explanation__ of poll") .explanationParseMode(ParseMode.MarkdownV2) .openPeriod(500) ); Poll poll = sendResponse.message().poll(); assertFalse(poll.id().isEmpty()); assertEquals(question, poll.question()); assertEquals(answers.length, poll.options().length); for (int i = 0; i < answers.length; i++) { PollOption option = poll.options()[i]; assertEquals(answers[i], option.text()); assertEquals(Integer.valueOf(0), option.voterCount()); } assertFalse(poll.isAnonymous()); assertEquals(poll.type(), Poll.Type.quiz); assertFalse(poll.allowsMultipleAnswers()); assertEquals(poll.totalVoterCount(), Integer.valueOf(0)); assertEquals(poll.correctOptionId(), Integer.valueOf(1)); assertFalse(poll.isClosed()); assertEquals("Some explanation of poll", poll.explanation()); assertEquals(1, poll.explanationEntities().length); assertEquals(MessageEntity.Type.underline, poll.explanationEntities()[0].type()); assertTrue(poll.openPeriod() >= 495 && poll.openPeriod() <= 500); }
@Test public void sendPollWithKeyboard() { String question = "Question ?"; String[] answers = {"Answer 1", "Answer 2"}; long closeDate = System.currentTimeMillis() / 1000 + 500; SendResponse sendResponse = bot.execute( new SendPoll(chatId, question, answers) .type("regular") .allowsMultipleAnswers(true) .replyMarkup(new ReplyKeyboardMarkup( new KeyboardButton("all polls").requestPoll(new KeyboardButtonPollType()), new KeyboardButton("quiz").requestPoll(new KeyboardButtonPollType(Poll.Type.quiz)), new KeyboardButton("regular").requestPoll(new KeyboardButtonPollType("regular")))) .closeDate(closeDate) ); Poll poll = sendResponse.message().poll(); assertEquals(question, poll.question()); assertEquals(answers.length, poll.options().length); assertTrue(poll.isAnonymous()); assertEquals(poll.totalVoterCount(), Integer.valueOf(0)); assertEquals(poll.type(), Poll.Type.regular); assertTrue(poll.allowsMultipleAnswers()); assertEquals(closeDate, poll.closeDate().longValue()); }
@Test public void editMessageText() { String text = "Update " + System.currentTimeMillis(); BaseResponse response = bot.execute(new EditMessageText(chatId, 925, text) .parseMode(ParseMode.Markdown) .disableWebPagePreview(true) .replyMarkup(new InlineKeyboardMarkup())); assertTrue(response.isOk()); assertNotNull(((SendResponse) response).message().editDate()); response = bot.execute(new EditMessageText(channelName, 306, text)); assertTrue(response.isOk()); response = bot.execute(new EditMessageText("AgAAAN3wAQCj_Q4DjX4ok5VEUZU", text)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: MESSAGE_ID_INVALID", response.description()); } }
@Test public void stopPoll() { String question = "Question ?"; String[] answers = {"Answer 1", "Answer 2"}; SendResponse sendResponse = bot.execute(new SendPoll(groupId, question, answers)); Integer messageId = sendResponse.message().messageId(); PollResponse response = bot.execute(new StopPoll(groupId, messageId)); Poll poll = response.poll(); assertTrue(poll.isClosed()); assertEquals(question, poll.question()); assertEquals(answers.length, poll.options().length); for (int i = 0; i < answers.length; i++) { PollOption option = poll.options()[i]; assertEquals(answers[i], option.text()); assertEquals(Integer.valueOf(0), option.voterCount()); } }
@Test public void testAsyncCallback() throws Exception { CountDownLatch latch = new CountDownLatch(1); bot.execute(new GetMe(), new Callback<GetMe, GetMeResponse>() { @Override public void onResponse(GetMe request, GetMeResponse response) { latch.countDown(); } @Override public void onFailure(GetMe request, IOException e) { throw new RuntimeException(e); } }); assertTrue(latch.await(5, TimeUnit.SECONDS)); }
@Test public void loginButton() { String text = "login"; String url = "http: SendResponse response = bot.execute( new SendMessage(chatId, "Login button").replyMarkup(new InlineKeyboardMarkup( 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()); }
@Test public void multipartNonAscii() { String caption = "хорошо"; Message message = bot.execute( new SendPhoto(chatId, imageFile).fileName("файл.txt").caption(caption) ).message(); assertEquals(caption, message.caption()); MessageTest.checkMessage(message); PhotoSizeTest.checkPhotos(message.photo()); }
@Test public void sendDice() { SendResponse response = bot.execute(new SendDice(chatId)); Dice dice = response.message().dice(); assertNotNull(dice); assertTrue(dice.value() >= 1 && dice.value() <= 6); assertEquals("🎲", dice.emoji()); response = bot.execute(new SendDice(chatId).darts()); dice = response.message().dice(); assertNotNull(dice); assertTrue(dice.value() >= 1 && dice.value() <= 6); assertEquals("🎯", dice.emoji()); response = bot.execute(new SendDice(chatId).basketball()); dice = response.message().dice(); assertNotNull(dice); assertTrue(dice.value() >= 1 && dice.value() <= 5); assertEquals("🏀", dice.emoji()); }
@Test public void setMyCommands() { BotCommand[] commands = new BotCommand[]{ new BotCommand("c1", "desc1"), new BotCommand("c2", "desc2"), new BotCommand("c3", "desc3"), }; BaseResponse response = bot.execute(new SetMyCommands(commands)); assertTrue(response.isOk()); GetMyCommandsResponse commandsResponse = bot.execute(new GetMyCommands()); assertTrue(commandsResponse.isOk()); assertArrayEquals(commandsResponse.commands(), commands); }
@Test public void editMessageCaption() { String text = "Update " + System.currentTimeMillis() + " <b>bold</b>"; SendResponse sendResponse = (SendResponse) bot.execute(new EditMessageCaption(chatId, 8124) .caption(text) .parseMode(ParseMode.HTML) .replyMarkup(new InlineKeyboardMarkup())); assertTrue(sendResponse.isOk()); Message message = sendResponse.message(); assertEquals(text.replace("<b>", "").replace("</b>", ""), message.caption()); MessageEntity captionEntity = message.captionEntities()[0]; assertEquals(MessageEntity.Type.bold, captionEntity.type()); assertEquals((Integer) 21, captionEntity.offset()); assertEquals((Integer) 4, captionEntity.length()); BaseResponse response = bot.execute(new EditMessageCaption(channelName, 511).caption(text)); assertTrue(response.isOk()); response = bot.execute(new EditMessageCaption("AgAAAPrwAQCj_Q4D2s-51_8jsuU").caption(text)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: MESSAGE_ID_INVALID", response.description()); } }
@Test public void editMessageReplyMarkup() { String text = "Update" + System.currentTimeMillis(); InlineKeyboardMarkup keyboard = new InlineKeyboardMarkup(new InlineKeyboardButton(text).url("https: InlineKeyboardMarkup gameKeyboard = new InlineKeyboardMarkup(new InlineKeyboardButton(text).callbackGame("test game")); BaseResponse response = bot.execute(new EditMessageReplyMarkup(chatId, 8124).replyMarkup(keyboard)); assertTrue(response.isOk()); response = bot.execute(new EditMessageReplyMarkup(channelName, 511).replyMarkup(keyboard)); assertTrue(response.isOk()); response = bot.execute(new EditMessageReplyMarkup("AgAAAPrwAQCj_Q4D2s-51_8jsuU").replyMarkup(gameKeyboard)); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: MESSAGE_ID_INVALID", response.description()); } }
@Test public void answerInline() { InlineQuery inlineQuery = BotUtils.parseUpdate(testInlineQuery).inlineQuery(); String inlineQueryId = inlineQuery.id(); assertFalse(inlineQueryId.isEmpty()); UserTest.checkUser(inlineQuery.from(), true); assertEquals(Integer.valueOf(12345), inlineQuery.from().id()); assertEquals("if", inlineQuery.query()); assertEquals("offset", inlineQuery.offset()); assertNull(inlineQuery.location()); InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup( new InlineKeyboardButton("inline game").callbackGame("pengrad test game description"), new InlineKeyboardButton("inline ok").callbackData("callback ok"), new InlineKeyboardButton("cancel").callbackData("callback cancel"), new InlineKeyboardButton("url").url(someUrl), new InlineKeyboardButton("switch inline").switchInlineQuery("query"), new InlineKeyboardButton("switch inline current").switchInlineQueryCurrentChat("query")); InlineQueryResult<?>[] results = new InlineQueryResult[]{ new InlineQueryResultArticle("1", "title", new InputTextMessageContent("message").disableWebPagePreview(false).parseMode(ParseMode.HTML)) .url(someUrl).hideUrl(true).description("desc").thumbUrl(someUrl).thumbHeight(100).thumbWidth(100), new InlineQueryResultArticle("2", "title", new InputContactMessageContent("123123123", "na,e").lastName("lastName").vcard("qr vcard")), new InlineQueryResultArticle("3", "title", new InputLocationMessageContent(50f, 50f).livePeriod(60)), new InlineQueryResultArticle("4", "title", new InputVenueMessageContent(50f, 50f, "title", "address").foursquareId("sqrId").foursquareType("frType")), new InlineQueryResultArticle("5", "title", "message"), new InlineQueryResultAudio("6", someUrl, "title").caption("cap <b>bold</b>").parseMode(ParseMode.HTML).performer("perf").audioDuration(100), new InlineQueryResultContact("7", "123123123", "name").lastName("lastName").vcard("tt vcard") .thumbUrl(someUrl).thumbHeight(100).thumbWidth(100), new InlineQueryResultDocument("8", someUrl, "title", "application/pdf").caption("cap <b>bold</b>").parseMode(ParseMode.HTML).description("desc") .thumbUrl(someUrl).thumbHeight(100).thumbWidth(100), new InlineQueryResultGame("9", "pengrad_test_game").replyMarkup(keyboardMarkup), new InlineQueryResultGif("10", someUrl, someUrl).caption("cap <b>bold</b>").parseMode(ParseMode.HTML).title("title") .thumbMimeType("image/gif") .gifHeight(100).gifWidth(100).gifDuration(100), new InlineQueryResultLocation("11", 50f, 50f, "title").livePeriod(60) .thumbUrl(someUrl).thumbHeight(100).thumbWidth(100), new InlineQueryResultMpeg4Gif("12", someUrl, someUrl).caption("cap <b>bold</b>").parseMode(ParseMode.HTML).title("title") .thumbMimeType("image/gif") .mpeg4Height(100).mpeg4Width(100).mpeg4Duration(100), new InlineQueryResultPhoto("13", someUrl, someUrl).photoWidth(100).photoHeight(100).title("title") .description("desc").caption("cap <b>bold</b>").parseMode(ParseMode.HTML), new InlineQueryResultVenue("14", 54f, 55f, "title", "address").foursquareId("frsqrId").foursquareType("frType") .thumbUrl(someUrl).thumbHeight(100).thumbWidth(100), new InlineQueryResultVideo("15", someUrl, VIDEO_MIME_TYPE, "text", someUrl, "title").caption("cap <b>bold</b>").parseMode(ParseMode.HTML) .videoWidth(100).videoHeight(100).videoDuration(100).description("desc"), new InlineQueryResultVoice("16", someUrl, "title").caption("cap <b>bold</b>").parseMode(ParseMode.HTML).voiceDuration(100), new InlineQueryResultCachedAudio("17", audioFileId).caption("cap <b>bold</b>").parseMode(ParseMode.HTML), new InlineQueryResultCachedDocument("18", stickerId, "title").caption("cap <b>bold</b>").parseMode(ParseMode.HTML).description("desc"), new InlineQueryResultCachedGif("19", gifFileId).caption("cap <b>bold</b>").parseMode(ParseMode.HTML).title("title"), new InlineQueryResultCachedMpeg4Gif("21", gifFileId).caption("cap <b>bold</b>").parseMode(ParseMode.HTML).title("title"), new InlineQueryResultCachedPhoto("22", photoFileId).caption("cap <b>bold</b>").parseMode(ParseMode.HTML).description("desc").title("title"), new InlineQueryResultCachedSticker("23", stickerId), new InlineQueryResultCachedVideo("24", videoFileId, "title").caption("cap <b>bold</b>").parseMode(ParseMode.HTML).description("desc"), new InlineQueryResultCachedVoice("25", voiceFileId, "title").caption("cap <b>bold</b>").parseMode(ParseMode.HTML), }; BaseResponse response = bot.execute(new AnswerInlineQuery(inlineQueryId, results) .cacheTime(100) .isPersonal(true) .nextOffset("offset") .switchPmText("go pm") .switchPmParameter("my_pm_parameter") ); if (!response.isOk()) { assertEquals(400, response.errorCode()); assertEquals("Bad Request: query is too old and response timeout expired or query ID is invalid", response.description()); } }