Всем привет, не могу понять почему телеграм бот не хочет отвечать в беседы, только в лс работает нормально, вот код
public void onUpdateReceived(Update update) {
if (update.getMessage().getText().equals("/films")){
try {
SendMessage outMessage = new SendMessage();
outMessage.setChatId(update.getMessage().getChatId().toString());
outMessage.setText("Таблица фильмов доступна по ссылке: ***");
execute(outMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
И всё работает нормально для лс, но не для бесед. Кто-то знает почему?
Код ниже например, отвечал и в беседы
public void onUpdateReceived(Update update) {
try {
if (update.hasMessage() && update.getMessage().hasText()) {
Message inMessage = update.getMessage();
SendMessage outMessage = new SendMessage();
outMessage.setChatId(String.valueOf(inMessage.getChatId()));
outMessage.setText("Таблица фильмов доступна по ссылке: ***);
execute(outMessage);
}
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
Вот какая-то ошибка, только еще не понял что её триггерит
13:52:04.167 [x Telegram Executor] ERROR org.telegram.telegrambots.updatesreceivers.DefaultBotSession - Cannot invoke "org.telegram.telegrambots.meta.api.objects.Message.getText()" because the return value of "org.telegram.telegrambots.meta.api.objects.Update.getMessage()" is null
java.lang.NullPointerException: Cannot invoke "org.telegram.telegrambots.meta.api.objects.Message.getText()" because the return value of "org.telegram.telegrambots.meta.api.objects.Update.getMessage()" is null
at x.bot.x.onUpdateReceived(x.java:13)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:317)
Вот текущий код:
package x.bot;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
public class x extends TelegramLongPollingBot {
public void onUpdateReceived(Update update){
if (update.hasMessage() && "/films".equals(update.getMessage().getText())){
try{
SendMessage outMessage = new SendMessage();
outMessage.setChatId(update.getMessage().getChatId().toString());
outMessage.setText("Таблица фильмов доступна по ссылке: ***");
execute(outMessage);
}
catch (TelegramApiException e){
e.printStackTrace();
}
}
}
public String getBotToken() {
return "***";
}
public String getBotUsername() {
return "x";
}
}
Main:
package x.bot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
try {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(new x());
}
catch (TelegramApiException e){
e.printStackTrace();
}
}
}
Бот в личные сообщения отвечает на /films нормально: В беседах же на команду /films не отзывается вообще: При этом бот админ, команда создана, privacy mode выключен
Ошибок нет
Добавил
System.out.println("update received: " + update.toString());
Вот что вышло
1 это лс
update received: Update(updateId=213901835, message=Message(messageId=461, from=User(id=426678678, firstName=₭єҝσċƙ, isBot=false, lastName=null, userName=kekock, languageCode=ru, canJoinGroups=null, canReadAllGroupMessages=null, supportInlineQueries=null), date=1628432492, chat=Chat(id=426678678, type=private, title=null, firstName=₭єҝσċƙ, lastName=null, userName=kekock, 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), forwardFrom=null, forwardFromChat=null, forwardDate=null, text=/films, entities=[MessageEntity(type=bot_command, offset=0, length=6, url=null, user=null, language=null, text=/films)], 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), inlineQuery=null, chosenInlineQuery=null, callbackQuery=null, editedMessage=null, channelPost=null, editedChannelPost=null, shippingQuery=null, preCheckoutQuery=null, poll=null, pollAnswer=null, myChatMember=null, chatMember=null)
2 это беседа-чат
update received: Update(updateId=213901836, message=Message(messageId=152, from=User(id=426678678, firstName=₭єҝσċƙ, isBot=false, lastName=null, userName=kekock, languageCode=ru, canJoinGroups=null, canReadAllGroupMessages=null, supportInlineQueries=null), date=1628432590, chat=Chat(id=-1001575358066, type=supergroup, title=мы и бот, firstName=null, lastName=null, userName=null, 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), forwardFrom=null, forwardFromChat=null, forwardDate=null, text=/films@x, entities=[MessageEntity(type=bot_command, offset=0, length=23, url=null, user=null, language=null, text=/films@x)], 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), inlineQuery=null, chosenInlineQuery=null, callbackQuery=null, editedMessage=null, channelPost=null, editedChannelPost=null, shippingQuery=null, preCheckoutQuery=null, poll=null, pollAnswer=null, myChatMember=null, chatMember=null)
/
, то бот такие команды не увидит. А чтобы увидел, нужно изменить privacy mode в настройках бота (через бота для создания ботов BotFather)