Skip to content

Commit

Permalink
Add user friendly commands /total and /summary. Accept free text as w…
Browse files Browse the repository at this point in the history
…ell.

Manual add and remove now sends message to Kafka (to get rid of blocked users).

Signed-off-by: Abhinav Sonkar <xsabhinav@gmail.com>
  • Loading branch information
xsreality committed Apr 23, 2020
1 parent 06845c9 commit ff639e8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Stop receiving updates.

#### `/stats` - On-demand Statistics

#### `/summary` - Get summary of all Indian States

#### `/total` - Get total count across India

To get the current statistics of any Indian State or Total, send the command `/stats`.

The bot will reply with an option to choose a region:
Expand Down
67 changes: 67 additions & 0 deletions covid19-telegram-bot/src/main/java/org/covid19/Covid19Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ public Ability catchAll() {
.privacy(PUBLIC).locality(ALL)
.input(0)
.action(ctx -> {
if (ctx.update().hasMessage() && ctx.update().getMessage().hasText()) {
String userMsg = ctx.update().getMessage().getText();
if ("Summary".equalsIgnoreCase(userMsg)) {
String chatId = getChatId(ctx.update());
userRequestKafkaTemplate.send("user-request", chatId, new UserRequest(chatId, "Summary"));

// send an update to Bot channel
String channelMsg = String.format("User %s (%s) requested stats for %s via text %s",
translateName(ctx.update().getMessage().getChat()), chatId, "Summary", userMsg);
silent.send(channelMsg, CHANNEL_ID);
return;
}
if ("Total".equalsIgnoreCase(userMsg)) {
String chatId = getChatId(ctx.update());
userRequestKafkaTemplate.send("user-request", chatId, new UserRequest(chatId, "Total"));

// send an update to Bot channel
String channelMsg = String.format("User %s (%s) requested stats for %s via text %s",
translateName(ctx.update().getMessage().getChat()), chatId, "Total", userMsg);
silent.send(channelMsg, CHANNEL_ID);
return;
}
}
String msg = "Send /stats to get latest count of any State or Total\n\n" +
"Send /mystate to choose your preferred state and receive updates automatically.";
silent.send(msg, ctx.chatId());
Expand Down Expand Up @@ -202,6 +225,9 @@ public Ability manuallyAdd() {
return;
}
subscribedUsers.add(ctx.firstArg());
// send a message to kafka user-preferences
userPrefsKafkaTemplate.send("user-preferences", String.valueOf(ctx.firstArg()),
new UserPrefs(String.valueOf(ctx.firstArg()), emptyList(), true));
String message = "Manually subscribed user: " + ctx.firstArg();
silent.send(message, ctx.chatId());
})
Expand All @@ -220,6 +246,11 @@ public Ability manuallyRemove() {
return;
}
subscribedUsers.remove(ctx.firstArg());

// send a message to update kafka user-preferences
userPrefsKafkaTemplate.send("user-preferences", String.valueOf(ctx.firstArg()),
new UserPrefs(String.valueOf(ctx.firstArg()), emptyList(), false));

String message = "Manually un-subscribed user: " + ctx.firstArg();
silent.send(message, ctx.chatId());
})
Expand All @@ -240,6 +271,42 @@ public Ability listSubscribedUsers() {
.build();
}

public Ability summary() {
return Ability
.builder().name("summary").info("Get latest summary of all Indian states")
.locality(ALL).privacy(PUBLIC).input(0)
.action(ctx -> {
String chatId = getChatId(ctx.update());
userRequestKafkaTemplate.send("user-request", chatId, new UserRequest(chatId, "Summary"));
})
.post(ctx -> {
// send an update to Bot channel
String chatId = getChatId(ctx.update());
String channelMsg = String.format("User %s (%s) requested stats for %s via /summary",
translateName(ctx.update().getMessage().getChat()), chatId, "Summary");
silent.send(channelMsg, CHANNEL_ID);
})
.build();
}

public Ability total() {
return Ability
.builder().name("total").info("Get total count across India")
.locality(ALL).privacy(PUBLIC).input(0)
.action(ctx -> {
String chatId = getChatId(ctx.update());
userRequestKafkaTemplate.send("user-request", chatId, new UserRequest(chatId, "Total"));
})
.post(ctx -> {
// send an update to Bot channel
String chatId = getChatId(ctx.update());
String channelMsg = String.format("User %s (%s) requested stats for %s via /total",
translateName(ctx.update().getMessage().getChat()), chatId, "Total");
silent.send(channelMsg, CHANNEL_ID);
})
.build();
}

public List<String> subscribedUsers() {
return db.getList(SUBSCRIBED_USERS);
}
Expand Down

0 comments on commit ff639e8

Please sign in to comment.