Update wiki pages

This commit is contained in:
Anton Petrov 2020-04-06 15:29:38 +03:00
parent 00f68950aa
commit 2083b29ae5
3 changed files with 34 additions and 30 deletions

View File

@ -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; import org.telegram.abilitybots.api.toggle.BareboneToggle;
public class YourAwesomeBot extends AbilityBot { public class YourAwesomeBot extends AbilityBot {
private static final BareboneToggle toggle = new BareboneToggle();
public YourAwesomeBot(String token, String username) { public YourAwesomeBot(String token, String username) {
BareboneToggle toggle = new BareboneToggle();
super(token, username, toggle); 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; import org.telegram.abilitybots.api.toggle.CustomToggle;
public class YourAwesomeBot extends AbilityBot { public class YourAwesomeBot extends AbilityBot {
public YourAwesomeBot(String token, String username) {
CustomToggle toggle = new CustomToggle() private static final CustomToggle toggle = new CustomToggle()
.turnOff("ban") .turnOff("ban")
.toggle("promote", "upgrade"); .toggle("promote", "upgrade");
public YourAwesomeBot(String token, String username) {
super(token, username, toggle); super(token, username, toggle);
} }

View File

@ -54,21 +54,20 @@ public Ability saysHelloWorld() {
The test for this ability would be: The test for this ability would be:
```java ```java
@Test @Test
public void canSayHelloWorld() { public void canSayHelloWorld() {
Update upd = new Update(); Update upd = new Update();
// Create a new EndUser - EndUser is a class similar to Telegram User, but contains // Create a new User - User is a class similar to Telegram User
// some utility methods like fullName() and shortName() for ease of use User user = new User(USER_ID, "Abbas", false, "Abou Daya", "addo37", null);
EndUser endUser = EndUser.endUser(USER_ID, "Abbas", "Abou Daya", "addo37");
// This is the context that you're used to, it is the necessary conumer item for the ability // 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 // We consume a context in the lamda declaration, so we pass the context to the action logic
bot.saysHelloWorld().action().accept(context); bot.saysHelloWorld().action().accept(context);
// We verify that the sender was called only ONCE and sent Hello World to CHAT_ID // We verify that the silent sender was called only ONCE and sent Hello World to CHAT_ID
// The sender here is a mock! // The silent sender here is a mock!
Mockito.verify(sender, times(1)).send("Hello World!", CHAT_ID); Mockito.verify(silent, times(1)).send("Hello World!", CHAT_ID);
} }
``` ```
@ -85,10 +84,10 @@ import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.telegram.abilitybots.api.db.DBContext; import org.telegram.abilitybots.api.db.DBContext;
import org.telegram.abilitybots.api.db.MapDBContext; 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.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.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -98,36 +97,36 @@ public class ExampleBotTest {
// Your bot handle here // Your bot handle here
private ExampleBot bot; private ExampleBot bot;
// Your sender here // Your sender here. Also you can create MessageSender
private MessageSender sender; private SilentSender silent;
@Before @Before
public void setUp() { public void setUp() {
// Create your bot // Create your bot
bot = new ExampleBot(); bot = new ExampleBot();
// Create a new sender as a mock // Create a new sender as a mock
sender = mock(MessageSender.class); silent = mock(SilentSender.class);
// Set your bot sender to the mocked sender // 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 // 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 // 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 @Test
public void canSayHelloWorld() { public void canSayHelloWorld() {
Update upd = new Update(); Update upd = new Update();
// Create a new EndUser - EndUser is a class similar to Telegram User, but contains // Create a new User - User is a class similar to Telegram User
// some utility methods like fullName() and shortName() for ease of use User user = new User(USER_ID, "Abbas", false, "Abou Daya", "addo37", null);
EndUser endUser = EndUser.endUser(USER_ID, "Abbas", "Abou Daya", "addo37");
// This is the context that you're used to, it is the necessary conumer item for the ability // 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 // We consume a context in the lamda declaration, so we pass the context to the action logic
bot.saysHelloWorld().action().accept(context); bot.saysHelloWorld().action().accept(context);
// We verify that the sender was called only ONCE and sent Hello World to CHAT_ID // We verify that the silent sender was called only ONCE and sent Hello World to CHAT_ID
// The sender here is a mock! // The silent sender here is a mock!
Mockito.verify(sender, times(1)).send("Hello World!", CHAT_ID); Mockito.verify(silent, times(1)).send("Hello World!", CHAT_ID);
} }
} }
``` ```
@ -178,11 +177,13 @@ public class ExampleBotTest {
private DBContext db; private DBContext db;
private MessageSender sender; private MessageSender sender;
@Before @Before
public void setUp() { public void setUp() {
bot = new ExampleBot(db); bot = new ExampleBot(db);
sender = mock(MessageSender.class); sender = mock(MessageSender.class);
bot.silent = new SilentSender(sender); SilentSender silent = new SilentSender(sender);
// Create setter in your bot
bot.setSilentSender(silent);
... ...
} }

View File

@ -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 // 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 // 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"); 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 // Get and increment counter, put it back in the map
Integer counter = countMap.compute(String.valueOf(userId), (id, count) -> count == null ? 1 : ++count); 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 // 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()); silent.send(message, ctx.chatId());
}) })
.build(); .build();