Answer the question
In order to leave comments, you need to log in
How to make Telegram bot work for each user separately (JAVA)?
Hello. I wrote a telegram bot, it logs in to the electronic diary website and issues a schedule, dz, and so on.
My problem is that the bot runs on the same instance for all users. Please tell me what needs to be done so that a new instance of the bot is created for each user. Thank you.
Here is part of my bot code:
public void onUpdateReceived(Update update) {
if(message != null && message.hasText()){
if(message.getText().equals("/start")){
sendMsg(message, area.toString());
flag = true;
flagTwo = true;
}
result = message.getText();
if (message.getText().equals(eq()) && flag) {
GetAutorization get = new GetAutorization();
String mes = message.getText();
prefs.put("pid", mes);
get.getArea();
parseGet = new JParseGetAuthorization();
parseGet.jParse(get.getResponse());
ArrayList<School> school = parseGet.getSchool();
StringBuilder builder = new StringBuilder();
for(int i = 0; i < school.size(); i++){
School schools = school.get(i);
builder.append(schools.getId()).append(": ").append(schools.getName()).append("\n");
}
sendMsg(message,builder.toString());
flag = false;
}
if(message.getText().equals(eq(parseGet)) && flagTwo){
GetAutorization get = new GetAutorization();
String mes = message.getText();
prefs.put("cn", mes);
get.getCity();
parseGet = new JParseGetAuthorization();
parseGet.jParse(get.getResponse());
ArrayList<School> school = parseGet.getSchool();
StringBuilder builder = new StringBuilder();
for(int i = 0; i < school.size(); i++){
School schools = school.get(i);
builder.append(schools.getId()).append(": ").append(schools.getName()).append("\n");
}
sendMsg(message,builder.toString());
flagTwo =false;
}
private void sendMsg(Message message, String str) {
SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(message.getChatId().toString());
sendMessage.setText(str);
try{
execute(sendMessage);
}catch (TelegramApiException ex){
ex.printStackTrace();
}
}
Answer the question
In order to leave comments, you need to log in
I faced a similar problem in the development of my bot. Fixed this problem like this:
Created a field Map userMap; and the Set of States in a separate State class (can be implemented through enum or in another convenient way)
when the user accesses the bot, it enters it in this field, when a new request comes to the bot, it checks if there is a user in the userMap. If there is a user in the list - continue with his State, if not - start from the starting state
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage()) {
Message inMessage = update.getMessage();
long chatId = inMessage.getChatId();
botState = switchBot(inMessage.getFrom());
if (botState == State.main) {
if (inMessage.hasText()) {
...
} else if (botState == State.newOrder) {//заявка записаться
String positiveAnswer = "Спасибо, мы Вам перезвоним";
String messageAdminsText = "";
...
} else if (botState == State.dev) { //команды разработчика
if (inMessage.hasText() && inMessage.getFrom().getUserName().equals(Const.DEV)) {
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question