Telegram 机器人向特定人员发送私人消息

Car*_*mel 1 java bots telegram telegram-bot

我使用java构建了一个电报机器人,它基本上获取用户的名字,它可以是1或10甚至40(我将其限制为50)取决于你现在想放多少,这个机器人的目的是稍后当你完成时您的特定用户,您编写消息发送给他们

现在一切都很完美,ID 名称以及消息都已保存,尽管我如何将其发送给所有用户,如果可能的话,我只是将其作为私人消息插入...通过命令

我希望我能足够理解

这就是我的代码:

    class Bot extends TelegramLongPollingBot {

public int counter = 0;
public ArrayList names = new ArrayList(50);
public SendMessage mainMessage = new SendMessage();
public String sgMsg = "";
public StringBuilder stringBuilder = new StringBuilder();

public String msg;
public void onUpdateReceived(Update update) {

    String command = update.getMessage().getText();
    SendMessage sysMsg = new SendMessage();
    sysMsg.setChatId(update.getMessage().getChatId());



    String firstCdletter;
    firstCdletter = Character.toString(command.charAt(0));


    if (command.equals("/start")) {
        sysMsg.setText("Enter the user's id, to finish send: Ok");
        try {
            execute(sysMsg);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
        counter = 0;
        names.clear();
        sgMsg = "";
    }else if (firstCdletter.equals("@")) {

        String user = command;
        names.add(counter);
        counter++;

    }else if(command.equals("/ok")){
        sysMsg.setText("Good, now write your message you want to deliver");
        try {
            execute(sysMsg);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }


    }else if(command.equals("/done")){
        msg = stringBuilder.toString();
    }else{
        sgMsg = update.getMessage().getText();
        stringBuilder.append(sgMsg + " ");
    }

}
Run Code Online (Sandbox Code Playgroud)

感谢大家的时间和帮助

Sea*_*ean 5

不幸的是,您无法通过 发送消息@username,您可以使用的唯一标识是 UID(看起来像109780439)。

顺便说一句,机器人必须先与该用户聊天,否则您将收到400 Error