如何让我的机器人在没有更新的情况下发送消息? [英] How do make my bot send message without an update?

查看:32
本文介绍了如何让我的机器人在没有更新的情况下发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 java bot 中创建一个电报机器人,但我遇到了问题,我看到机器人在没有用户更新的情况下发送文本\广告,我想知道我该怎么做.它只在发送消息时发送消息用户向它发送消息.我需要知道如何让我的机器人在没有 onUpdateReceived 的情况下发送一些消息.(对不起,我的英语)

i am creating a telegram bot in java bot but i have problem,I have seen bots that send text\ad without an update from a user ,I want to know how i can do it .It is only sending messages when the user sends a message to it.i need to know how can i make my bot send some message without a onUpdateReceived.(sorry for my english)

onUpdateReceived(Update update) 仅在用户发送命令时发送消息

onUpdateReceived(Update update) only sends message when the user sends a command

谢谢.

推荐答案

这个棘手的方法对我有用.

This tricky way worked for me.

Telegram bot 需要知道该机器人与之交谈的特定群组或个人聊天的 chatID.

Telegram bot needs to know chatID of that certain group or personal chat that bot talks with.

首先,您必须获得 Long 的 chatId.

First you have to get chatId which is Long.

在 BotFather 的帮助下在电报中创建/chatid 命令.

Create /chatid command with help of BotFather in telegram.

并在 Java 中使用该命令,如下所示:

And use that command in java like this:

public class TelegramBot extends TelegramLongPollingBot {
    public Long chatId = null;

    public void onUpdateReceived(Update update) {

        String input = update.getMessage().getText();
        SendMessage output = new SendMessage();

        if (input.equals("/chatid")) {
            chatId = update.getMessage().getChatId();
            System.out.println("chatId = " + chatId);
            output.setText("chatid is  = " + chatId);
        }

        output.setChatId(update.getMessage().getChatId());
        try {
            execute(output);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }

    }

    public void sayImNotRobot() {
    SendMessage message = new SendMessage();

    message.enableMarkdown(true);
    message.setChatId((long) chatIdLike123456); //Write chatID manually here
    message.setText("Im not robot");
    try {
        execute(message);
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }

}
    
    public String getBotUsername() {
        return "your bot name here";
    }

    public String getBotToken() {
        return "your bot token here";
    }
}

在主类中使用该方法将 chatId 打印到控制台并将其(从控制台结果)复制粘贴到我在 TelegramBot 类中编写的 chatIdLike123456.

In main class use that method to print chatId to console and copy paste it (from console result) to chatIdLike123456 that i wrote in TelegramBot class.

 public class Main {
        public static void main(String[] args) {
            ApiContextInitializer.init();
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
            try {
                telegramBotsApi.registerBot(new TelegramBot());
                TelegramBot bot = new TelegramBot();
                bot.sayImNotRobot(); //now, you can call this method whenever you want
 
  
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

这篇关于如何让我的机器人在没有更新的情况下发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆