我的电报机器人有问题。我使用activeMQ来独立于聊天消息(超过100000条消息)发送新闻消息。我看到新闻消息成功地从队列中排队和出队,但是没有一个用户收到任何新闻。在测试(400个用户)时,它可以正常工作,但不能在生产中使用。
向Telegram发送消息后,我有分析响应的代码:
private boolean analizeResponse(List<NameValuePair> payload, HttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
boolean result = response.getStatusLine().getStatusCode() == 200;
log.info("Response status Code is: " + response.getStatusLine().getStatusCode());
if (entity != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
if (response.getStatusLine().getStatusCode() == 403 && content.toString().contains("Forbidden: bot was blocked by the user")) {
new TelegramUserDao().unsubscribeUser(payload.stream().filter(pair -> pair.getName().equals("chat_id")).collect(Collectors.toList()).get(0).getValue());
}
}
return result;
}
,在activemq消费者中,我使用Client_Acknowledge会话:
connection = factory.createConnection();
connection.start();
connection.setExceptionListener(this);
session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination destination = session.createQueue(queueName);
consumer = session.createConsumer(destination);
...
result = telegramSender.sendMessage(telegramBody);
}
if (result) {
sent++;
message.acknowledge();
} else {
noSent++;
}
int count = message.getIntProperty(DELIVERY_COUNT);
if (count >= MAX_REDELIVERY) {
message.acknowledge();
log.warn("news : " + news.getId() + " isn't send to user : " + newsLetter.getUserId());
}
telegramSender.sendMessage(telegramBody);我在analizeResponse方法中获得的方法返回值。 ActiveMQ队列每秒减少一次,我认为电报中有200个状态代码,或者我错了。