commit
cd0ec5959f
@ -9,8 +9,10 @@ The barebone toggle is used to **turn off** all the default abilities that come
|
||||
import org.telegram.abilitybots.api.toggle.BareboneToggle;
|
||||
|
||||
public class YourAwesomeBot extends AbilityBot {
|
||||
|
||||
private static final BareboneToggle toggle = new BareboneToggle();
|
||||
|
||||
public YourAwesomeBot(String token, String username) {
|
||||
BareboneToggle toggle = new BareboneToggle();
|
||||
super(token, username, toggle);
|
||||
}
|
||||
|
||||
@ -25,11 +27,12 @@ The custom toggle allows you to customize or turn off the names of the abilities
|
||||
import org.telegram.abilitybots.api.toggle.CustomToggle;
|
||||
|
||||
public class YourAwesomeBot extends AbilityBot {
|
||||
public YourAwesomeBot(String token, String username) {
|
||||
CustomToggle toggle = new CustomToggle()
|
||||
|
||||
private static final CustomToggle toggle = new CustomToggle()
|
||||
.turnOff("ban")
|
||||
.toggle("promote", "upgrade");
|
||||
|
||||
|
||||
public YourAwesomeBot(String token, String username) {
|
||||
super(token, username, toggle);
|
||||
}
|
||||
|
||||
|
@ -54,21 +54,20 @@ public Ability saysHelloWorld() {
|
||||
The test for this ability would be:
|
||||
|
||||
```java
|
||||
@Test
|
||||
@Test
|
||||
public void canSayHelloWorld() {
|
||||
Update upd = new Update();
|
||||
// Create a new EndUser - EndUser is a class similar to Telegram User, but contains
|
||||
// some utility methods like fullName() and shortName() for ease of use
|
||||
EndUser endUser = EndUser.endUser(USER_ID, "Abbas", "Abou Daya", "addo37");
|
||||
// Create a new User - User is a class similar to Telegram User
|
||||
User user = new User(USER_ID, "Abbas", false, "Abou Daya", "addo37", null);
|
||||
// This is the context that you're used to, it is the necessary conumer item for the ability
|
||||
MessageContext context = MessageContext.newContext(upd, endUser, CHAT_ID);
|
||||
MessageContext context = MessageContext.newContext(upd, user, CHAT_ID);
|
||||
|
||||
// We consume a context in the lamda declaration, so we pass the context to the action logic
|
||||
bot.saysHelloWorld().action().accept(context);
|
||||
|
||||
// We verify that the sender was called only ONCE and sent Hello World to CHAT_ID
|
||||
// The sender here is a mock!
|
||||
Mockito.verify(sender, times(1)).send("Hello World!", CHAT_ID);
|
||||
// We verify that the silent sender was called only ONCE and sent Hello World to CHAT_ID
|
||||
// The silent sender here is a mock!
|
||||
Mockito.verify(silent, times(1)).send("Hello World!", CHAT_ID);
|
||||
}
|
||||
```
|
||||
|
||||
@ -85,10 +84,10 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.telegram.abilitybots.api.db.DBContext;
|
||||
import org.telegram.abilitybots.api.db.MapDBContext;
|
||||
import org.telegram.abilitybots.api.objects.EndUser;
|
||||
import org.telegram.abilitybots.api.objects.MessageContext;
|
||||
import org.telegram.abilitybots.api.sender.MessageSender;
|
||||
import org.telegram.abilitybots.api.sender.SilentSender;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.api.objects.User;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ -98,36 +97,36 @@ public class ExampleBotTest {
|
||||
|
||||
// Your bot handle here
|
||||
private ExampleBot bot;
|
||||
// Your sender here
|
||||
private MessageSender sender;
|
||||
// Your sender here. Also you can create MessageSender
|
||||
private SilentSender silent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Create your bot
|
||||
bot = new ExampleBot();
|
||||
// Create a new sender as a mock
|
||||
sender = mock(MessageSender.class);
|
||||
// Set your bot sender to the mocked sender
|
||||
silent = mock(SilentSender.class);
|
||||
// Set your bot silent sender to the mocked sender
|
||||
// THIS is the line that prevents your bot from communicating with Telegram servers when it's running its own abilities
|
||||
// All method calls will go through the mocked interface -> which would do nothing except logging the fact that you've called this function with the specific arguments
|
||||
bot.sender = sender;
|
||||
// Create setter in your bot
|
||||
bot.setSilentSender(silent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canSayHelloWorld() {
|
||||
Update upd = new Update();
|
||||
// Create a new EndUser - EndUser is a class similar to Telegram User, but contains
|
||||
// some utility methods like fullName() and shortName() for ease of use
|
||||
EndUser endUser = EndUser.endUser(USER_ID, "Abbas", "Abou Daya", "addo37");
|
||||
// Create a new User - User is a class similar to Telegram User
|
||||
User user = new User(USER_ID, "Abbas", false, "Abou Daya", "addo37", null);
|
||||
// This is the context that you're used to, it is the necessary conumer item for the ability
|
||||
MessageContext context = MessageContext.newContext(upd, endUser, CHAT_ID);
|
||||
MessageContext context = MessageContext.newContext(upd, user, CHAT_ID);
|
||||
|
||||
// We consume a context in the lamda declaration, so we pass the context to the action logic
|
||||
bot.saysHelloWorld().action().accept(context);
|
||||
|
||||
// We verify that the sender was called only ONCE and sent Hello World to CHAT_ID
|
||||
// The sender here is a mock!
|
||||
Mockito.verify(sender, times(1)).send("Hello World!", CHAT_ID);
|
||||
// We verify that the silent sender was called only ONCE and sent Hello World to CHAT_ID
|
||||
// The silent sender here is a mock!
|
||||
Mockito.verify(silent, times(1)).send("Hello World!", CHAT_ID);
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -178,11 +177,13 @@ public class ExampleBotTest {
|
||||
private DBContext db;
|
||||
private MessageSender sender;
|
||||
|
||||
@Before
|
||||
@Before
|
||||
public void setUp() {
|
||||
bot = new ExampleBot(db);
|
||||
sender = mock(MessageSender.class);
|
||||
bot.silent = new SilentSender(sender);
|
||||
SilentSender silent = new SilentSender(sender);
|
||||
// Create setter in your bot
|
||||
bot.setSilentSender(silent);
|
||||
...
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ We'll be introducing an ability that maintains a special counter for every user.
|
||||
// db.getMap takes in a string, this must be unique and the same everytime you want to call the exact same map
|
||||
// TODO: Using integer as a key in this db map is not recommended, it won't be serialized/deserialized properly if you ever decide to recover/backup db
|
||||
Map<String, Integer> countMap = db.getMap("COUNTERS");
|
||||
int userId = ctx.user().id();
|
||||
int userId = ctx.user().getId();
|
||||
|
||||
// Get and increment counter, put it back in the map
|
||||
Integer counter = countMap.compute(String.valueOf(userId), (id, count) -> count == null ? 1 : ++count);
|
||||
@ -41,7 +41,7 @@ We'll be introducing an ability that maintains a special counter for every user.
|
||||
*/
|
||||
|
||||
// Send formatted will enable markdown
|
||||
String message = String.format("%s, your count is now *%d*!", ctx.user().shortName(), counter);
|
||||
String message = String.format("%s, your count is now *%d*!", ctx.user().getUserName(), counter);
|
||||
silent.send(message, ctx.chatId());
|
||||
})
|
||||
.build();
|
||||
|
Loading…
Reference in New Issue
Block a user