【问题标题】:Telegram bot send private message to a specific person [closed]电报机器人向特定的人发送私人消息[关闭]
【发布时间】:2018-10-07 17:37:36
【问题描述】:

我是用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 + " ");
    }

}

感谢大家的宝贵时间和帮助

【问题讨论】:

    标签: java bots telegram telegram-bot


    【解决方案1】:

    很遗憾,您不能通过@username 发送消息,您唯一可以使用的身份是UID(看起来像109780439)。

    顺便说一句,bot 之前必须与该用户聊天,否则你会得到一个 400 Error

    【讨论】:

    • 好吧,假设所有这些人都得到了该机器人,假设有 20 人,但我希望只有 4 人会收到相同的消息,所以我需要使用他们的 UID 吗?如果是这样,我从哪里获得该 UID?
    • 您可以让用户转发来自该用户的消息
    • 好吧,那么我如何获取他们的用户 ID 或至少将他们的用户名转换为用户 ID,以便我可以向他们发送消息
    • @Sean 我们不能像 whatsapp 一样集成 Telegram 吗?有多少网站会在 whatsapp 上向用户发送消息?就像 OYO 一样,当用户预订房间时,OYO 会发送用户的 whatsapp 的详细信息。如何使用 Telegram?
    猜你喜欢
    • 1970-01-01
    • 2012-09-21
    • 2017-03-19
    • 2018-09-13
    • 2017-05-11
    • 2017-09-01
    • 2021-08-03
    • 2020-08-25
    • 2016-03-11
    相关资源
    最近更新 更多