英文:
Telegram bot only for send messages
问题
以下是翻译好的内容:
public class TelegramBot extends TelegramLongPollingBot {
@Override
public void onUpdateReceived(Update update) {
// 当机器人接收到消息时的处理
// 我并不需要这个方法,但是必须实现它
}
public synchronized void sendMsg(String msg) {
// 发送消息的处理
// 这里是我需要的代码部分
}
@Override
public String getBotUsername() {
// 必须实现
return "我的机器人用户名";
}
@Override
public String getBotToken() {
// 必须实现
return "我的令牌";
}
}
英文:
I want a Telegram bot notify me when a user do some action in my website. I have been testing Telegram bots to send messages and using getUpdates via polling to receive, and everything works perfectly. I have read than the polling method consume more CPU than webhooks (because it is constantly checking for new messages), but this is more complicated to implement, so I discard webhooks.
Actually, I don't need to use the polling or webhooks, because what I want is to send messages, but I have to implement the getUpdates method compulsory. Is there any way to use only the send message function and avoid the receive messages? Like a only-read bot or a telegram channel.
Thanks!
EDIT. Here is my code in Java:
public class TelegramBot extends TelegramLongPollingBot {
@Override
public void onUpdateReceived(Update update) {
// Stuff when the bot receive a message
// I don't need this method, but it is compulsory to implement it
}
public synchronized void sendMsg(String msg) {
// Stuff to send a message
// Here goes the code I only need
}
@Override
public String getBotUsername() {
// Compulsory to implement
return "my_bot_user_name";
}
@Override
public String getBotToken() {
// Compulsory to implement
return "my_token";
}
}
答案1
得分: 1
可以在不实际执行轮询的情况下配置和启动聊天机器人。
updater = Updater('token', use_context=True)
dp = updater.dispatcher
updater.idle()
updater.bot.send_message(chat_id='YYYY', text='你好')
在这种情况下,没有轮询,但会向聊天发送一条消息。
注意:
- 您需要知道聊天 ID
- 仅在需要保持聊天机器人运行(否则您只需发送消息并关闭应用程序)时才需要使用 updater.idle()
英文:
It is possible to configure and start the chatbot without actually performing the polling
updater = Updater('token', use_context=True)
dp = updater.dispatcher
updater.idle()
updater.bot.send_message(chat_id='YYYY', text='hoi')
In this case there is no polling but a message is sent to the chat.
Notice:
- you need to know the chat id
- updater.idle() is needed only if you need keep running the chatbot (otherwise you can just send the message(s) and shutdown the application)
答案2
得分: 0
我已经使用Jersey WS(jersey-client
和jersey-common
包)解决了所有问题。
这是我的代码现在的样子,只在需要的时候调用它,而且完全不进行轮询:
private static int sendMessage(String message) {
HttpResponse<String> response = null;
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.version(HttpClient.Version.HTTP_2)
.build();
UriBuilder builder = UriBuilder
.fromUri("https://api.telegram.org")
.path("/{token}/sendMessage")
.queryParam("chat_id", CHAT_ID)
.queryParam("text", message)
.queryParam("parse_mode", "html");
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(builder.build("bot" + TELEGRAM_TOKEN))
.timeout(Duration.ofSeconds(5))
.build();
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
catch(IOException | InterruptedException ex) {
LOGGER.warn("Error sending message to Telegram Bot");
}
return (response != null) ? response.statusCode() : -1;
}
英文:
I have solved all using Jersey WS (jersey-client
and jersey-common
packages)
This is how my code looks now, just calling it when I need it and not polling at all:
private static int sendMessage(String message) {
HttpResponse<String> response = null;
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.version(HttpClient.Version.HTTP_2)
.build();
UriBuilder builder = UriBuilder
.fromUri("https://api.telegram.org")
.path("/{token}/sendMessage")
.queryParam("chat_id", CHAT_ID)
.queryParam("text", message)
.queryParam("parse_mode", "html");
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(builder.build("bot" + TELEGRAM_TOKEN))
.timeout(Duration.ofSeconds(5))
.build();
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
catch(IOException | InterruptedException ex) {
LOGGER.warn("Error sending message to Telegram Bot");
}
return (response != null) ? response.statusCode() : -1;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论