Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to associate user replies with messages #1222

Open
guoxiaopang opened this issue May 18, 2023 · 3 comments
Open

How to associate user replies with messages #1222

guoxiaopang opened this issue May 18, 2023 · 3 comments

Comments

@guoxiaopang
Copy link

For example:
Bot: Please enter your recharge amount?
user: 20
But I got this 20. It's just an ordinary piece of news. It's not related to what the robot said

@guoxiaopang
Copy link
Author

i used
implementation 'org.telegram:telegrambots:6.5.0'
not Ability

@centralhardware
Copy link

centralhardware commented May 25, 2023

you need implement FSM. some thing like this

Map<Long, UserState> fsm = new HashMap<>();
---
Send message Please enter your recharge amount?
fsm.put(chatId, WAITING_RECHARGE)
--- and waiting for user answer
if (fsm.get(chatId) == WAITING_RECHARGE){
   
}

@kenkoro
Copy link

kenkoro commented Jun 22, 2023

@centralhardware or @rubenlagus, suppose I have an ability to add a new book (or something in String format), here's an example:

public class AbilityAddBook implements AbilityExtension {
    private final BookBot extension;

    public AbilityAddBook(BookBot extension) {
        this.extension = extension;
    }

    public Ability addBook() {
        return Ability.builder()
                .name("addbook")
                .info("add a new book")
                .locality(ALL)
                .privacy(PUBLIC)
                .action(ctx -> extension.responseHandler().replyToAddBook(ctx.chatId()))
                .build();
    }
}

Also in ResponseHandler.class:

public void replyToAddBook(final long chatId) {
    try {
        sender.execute(buildAddBookMessage(chatId));
        chatStates.put(chatId, AWAITING_ANSWER);
        // What's next?
    } catch (TelegramApiException te) {
        LOG.error(ERROR_ON_REPLY + te.getMessage());
    }
}

private SendMessage buildAddBookMessage(long chatId) {
    final var message = new SendMessage();
    message.setText(ADD_BOOK_MSG);
    message.enableMarkdown(true);
    message.setChatId(chatId);
    message.setReplyMarkup(KeyboardFactory.withBackButton());

    return message;
}

So, I want to actually store (you can also say capture) user's reply (which book he/she wants to add) in a variable (for now, not in DB). How should I do that? Would you be kind enough to write detailed example of it? Because, when I actually try to use Reply.class, eg. add the .reply() to AbilityAddBook.class, it comes to overlapping the other commands like /start or /help. If you don't mind, can you explain this as well?

P.S.: repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants