I am currently trying to validate user text submissions by querying them in Firestore first before sending them a reply. This is done using Firebase Admin SDK and https://github.com/rubenlagus/TelegramBots. Problem is that when accessing the methods that interact with Firestore (verifyValidUserAndOrganisation), the method doesn't execute yet the object is not null when I checked in debugger.
I am not sure what is the error and if there is, please suggest any other ways to validate with an asynchronous Firestore database read.
Function in question:
@NotNull
private Predicate<Update> validUserAndOrganisation() {
return upd -> {
String message = upd.getMessage().getText().trim();
// Need to check both user telegram id and organisation code valid
// Then proceed with allowing next reply, else need to throw message and stop ability
if (message.length() == 6) { // Only proceed if length of text is valid for an org. code
if (isValidOrganisationCode(message)) { // Additional check for text being valid organisation code regex to reduce unnecessary Firestore reads.
// Check firebase
try {
Verification verification = firebaseDB.verifyValidUserAndOrganisation(upd.getChatMember().getFrom().getId(), message); // <---- Here
if (verification.getSuccess()) {
return true;
} else {
silent.send(verification.getReason(), getChatId(upd)); // Will submit false afterwards to signify failing the check
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (message.length() > 0 && !message.equals("/submit")) {
silent.send("Please enter a valid organisation code", getChatId(upd));
}
return false;
};
}
Error logs:
[MyCelsiusBot Telegram Executor] INFO org.telegram.abilitybots.api.bot.BaseAbilityBot - [MyCelsiusBot] New update [41791280] received at 2022-02-07T00:31:52.469791300 08:00[Asia/Singapore]
[MyCelsiusBot Telegram Executor] INFO org.telegram.abilitybots.api.bot.BaseAbilityBot - Update(updateId=41791280, message=Message(messageId=163, from=User(id=5003829958, firstName=Kez, isBot=false, lastName=null, userName=Kzeezee, languageCode=en, canJoinGroups=null, canReadAllGroupMessages=null, supportInlineQueries=null), date=1644165113, chat=Chat(id=5003829958, type=private, title=null, firstName=Kez, lastName=null, userName=Kzeezee, allMembersAreAdministrators=null, photo=null, description=null, inviteLink=null, pinnedMessage=null, stickerSetName=null, canSetStickerSet=null, permissions=null, slowModeDelay=null, bio=null, linkedChatId=null, location=null, messageAutoDeleteTime=null, allowSavingContent=null, hasPrivateForwards=null), forwardFrom=null, forwardFromChat=null, forwardDate=null, text=Hhhhhh, entities=null, captionEntities=null, audio=null, document=null, photo=null, sticker=null, video=null, contact=null, location=null, venue=null, animation=null, pinnedMessage=null, newChatMembers=[], leftChatMember=null, newChatTitle=null, newChatPhoto=null, deleteChatPhoto=null, groupchatCreated=null, replyToMessage=null, voice=null, caption=null, superGroupCreated=null, channelChatCreated=null, migrateToChatId=null, migrateFromChatId=null, editDate=null, game=null, forwardFromMessageId=null, invoice=null, successfulPayment=null, videoNote=null, authorSignature=null, forwardSignature=null, mediaGroupId=null, connectedWebsite=null, passportData=null, forwardSenderName=null, poll=null, replyMarkup=null, dice=null, viaBot=null, senderChat=null, proximityAlertTriggered=null, messageAutoDeleteTimerChanged=null, voiceChatStarted=null, voiceChatEnded=null, voiceChatParticipantsInvited=null, voiceChatScheduled=null, isAutomaticForward=null, canBeForwarded=null), inlineQuery=null, chosenInlineQuery=null, callbackQuery=null, editedMessage=null, channelPost=null, editedChannelPost=null, shippingQuery=null, preCheckoutQuery=null, poll=null, pollAnswer=null, myChatMember=null, chatMember=null, chatJoinRequest=null)
java.lang.NullPointerException
at bot.MyCelsiusTelegramBot.lambda$validUserAndOrganisation$12(MyCelsiusTelegramBot.java:145)
at org.telegram.abilitybots.api.objects.Reply.lambda$isOkFor$1(Reply.java:97)
at java.base/java.util.stream.ReduceOps$1ReducingSink.accept(ReduceOps.java:80)
at java.base/java.util.AbstractList$RandomAccessSpliterator.forEachRemaining(AbstractList.java:720)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:563)
at org.telegram.abilitybots.api.objects.Reply.isOkFor(Reply.java:98)
at org.telegram.abilitybots.api.bot.BaseAbilityBot.lambda$filterReply$22(BaseAbilityBot.java:663)
at org.telegram.abilitybots.api.bot.BaseAbilityBot.runSilently(BaseAbilityBot.java:674)
at org.telegram.abilitybots.api.bot.BaseAbilityBot.lambda$filterReply$23(BaseAbilityBot.java:663)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
at java.base/java.util.AbstractList$RandomAccessSpliterator.forEachRemaining(AbstractList.java:720)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:553)
at org.telegram.abilitybots.api.bot.BaseAbilityBot.filterReply(BaseAbilityBot.java:669)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:411)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.telegram.abilitybots.api.bot.BaseAbilityBot.onUpdateReceived(BaseAbilityBot.java:234)
at org.telegram.abilitybots.api.bot.AbilityBot.onUpdateReceived(AbilityBot.java:55)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:317)
[MyCelsiusBot Telegram Executor] INFO org.telegram.abilitybots.api.bot.BaseAbilityBot - [MyCelsiusBot] Processing of update [41791280] ended at 2022-02-07T00:31:52.547348100 08:00[Asia/Singapore]
---> Processing time: [77 ms] <---
I also have the entire project at https://github.com/Kzeezee/MyCelsius if information is insufficient.
CodePudding user response:
With the help of @LukeWoodward, I did find my mistake and that upd.getChatMember()
was the one responsible for giving me the null pointer exception.
upd.getChatMember()
will be null if the messages are coming from direct DMs in Telegram and not from group chats. For that, you need to use upd.getMessage().getFrom().getId()
for getting the user's id who direct messaged your bot.