-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #694 from rubenlagus/dev
Dev
- Loading branch information
Showing
64 changed files
with
2,193 additions
and
968 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ copyright/ | |
|
||
#File System specific files | ||
.DS_STORE | ||
|
||
# Default ignored files | ||
/Bots.iws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Ability Extensions | ||
You have around 100 abilities in your bot and you're looking for a way to refactor that mess into more modular classes. `AbillityExtension` is here to support just that! It's not a secret that AbilityBot uses refactoring backstage to be able to construct all of your abilities and map them accordingly. However, AbilityBot searches initially for all methods that return an `AbilityExtension` type. Then, those extensions will be used to search for declared abilities. Here's an example. | ||
```java | ||
public class MrGoodGuy implements AbilityExtension { | ||
public Ability () { | ||
return Ability.builder() | ||
.name("nice") | ||
.privacy(PUBLIC) | ||
.locality(ALL) | ||
.action(ctx -> silent.send("You're awesome!", ctx.chatId()) | ||
); | ||
} | ||
} | ||
|
||
public class MrBadGuy implements AbilityExtension { | ||
public Ability () { | ||
return Ability.builder() | ||
.name("notnice") | ||
.privacy(PUBLIC) | ||
.locality(ALL) | ||
.action(ctx -> silent.send("You're horrible!", ctx.chatId()) | ||
); | ||
} | ||
} | ||
|
||
public class YourAwesomeBot implements AbilityBot { | ||
|
||
// Constructor for your bot | ||
|
||
public AbilityExtension goodGuy() { | ||
return new MrGoodGuy(); | ||
} | ||
|
||
public AbilityExtension badGuy() { | ||
return new MrBadGuy(); | ||
} | ||
|
||
// Override creatorId | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Ability Toggle | ||
Well, what if you don't like all the default abilities that AbilityBot supplies? Fear not, you are able to disable all of them, even rename them if you will! | ||
|
||
You may pass a custom toggle to your abilitybot constructor to customize how these abilities get registered. | ||
|
||
## The Barebone Toggle | ||
The barebone toggle is used to **turn off** all the default abilities that come with the bot (ban, unban, demote, promte, etc...). To use the barebone toggle, call your super constructor with: | ||
```java | ||
import org.telegram.abilitybots.api.toggle.BareboneToggle; | ||
|
||
public class YourAwesomeBot extends AbilityBot { | ||
public YourAwesomeBot(String token, String username) { | ||
BareboneToggle toggle = new BareboneToggle(); | ||
super(token, username, toggle); | ||
} | ||
|
||
// Override ceatorId() | ||
} | ||
``` | ||
Obviously, you can export this as a parameter that you can pass to your bot's constructor. | ||
|
||
## The Custom Toggle | ||
The custom toggle allows you to customize or turn off the names of the abilities that the abilitybot exposes. | ||
```java | ||
import org.telegram.abilitybots.api.toggle.CustomToggle; | ||
|
||
public class YourAwesomeBot extends AbilityBot { | ||
public YourAwesomeBot(String token, String username) { | ||
CustomToggle toggle = new CustomToggle() | ||
.turnOff("ban") | ||
.toggle("promote", "upgrade"); | ||
|
||
super(token, username, toggle); | ||
} | ||
|
||
// Override ceatorId() | ||
} | ||
``` | ||
|
||
With these changes, the ability "ban" is no longer available and the "promote" ability has been renamed to "upgrade". | ||
## The Default Toggle | ||
The default toggle is automatically used if the user does not specify a toggle. The default toggle allows all the abilities to be effective and unchanged. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# State Machines | ||
|
||
AbilityBot supports state machines using ReplyFlows. Internally, they set and transition the state of the user based on their actions so far. | ||
Developers may declare this flow control in either a bottom-up or a top-down approach. If you're already familiar with what a `Reply` is, consider ReplyFlows as the cherry on top. | ||
|
||
## Usage | ||
A ReplyFlow can not be directly instantiated; it must be built. First, let's create | ||
some basic replies. | ||
```java | ||
Reply saidLeft = Reply.of(upd -> | ||
silent.send("Sir, I have gone left.", getChatId(upd)), | ||
hasMessageWith("left")); | ||
|
||
Reply saidRight = Reply.of(upd -> | ||
silent.send("Sir, I have gone right.", getChatId(upd)), | ||
hasMessageWith("right")); | ||
``` | ||
The first `Reply` effectively replies to any message that has the text "left". Once such a message is received, the | ||
bot replies with "Sir, I have gone left". Likewise, the bot acts for when "right" is encountered. | ||
|
||
What if now, you'd like to protect those two replies behind one more reply? Let's say, the bot first should ask the user to give it directions. | ||
This means that people can't tell your bot to turn left or right UNLESS the bot asks for directions. Let's trigger that when the user sends "wake up" to the bot! | ||
|
||
```java | ||
// We instantiate a ReplyFlow builder with our internal db (DBContext instance) passed | ||
// State is always preserved in the db of the bot and remains even after termination | ||
ReplyFlow.builder(db) | ||
// Just like replies, a ReplyFlow can take an action, here we want to send a | ||
// statement to prompt the user for directions! | ||
.action(upd -> silent.send("Command me to go left or right!", getChatId(upd))) | ||
// We should only trigger this flow when the user says "wake up" | ||
.onlyIf(hasMessageWith("wake up")) | ||
// The next method takes in an object of type Reply. | ||
// Here we chain our replies together | ||
.next(saidLeft) | ||
// We chain one more reply, which is when the user commands your bot to go right | ||
.next(saidRight) | ||
// Finally, we build our ReplyFlow | ||
.build(); | ||
``` | ||
For the sake of completeness, here's the auxiliary method `hasMessageWith`. | ||
```java | ||
private Predicate<Update> hasMessageWith(String msg) { | ||
return upd -> upd.getMessage().getText().equalsIgnoreCase(msg); | ||
} | ||
``` | ||
To run this example in your own AbilityBot, just have a method return that ReplyFlow we just built. Yup, it's that easy, just like how you're used to | ||
building replies and abilities. | ||
## More Complex States | ||
Let's say that your bot becomes naughty when the user asks it to go left. We want the bot to say "I don't know how to go left." when the user commands it to go left. We would also like to chain more commands after this state. Here's | ||
how that's done. | ||
We must create a new ReplyFlow that would be chained to the initial one. Here's what our left flow would look like. | ||
```java | ||
ReplyFlow leftflow = ReplyFlow.builder(db) | ||
.action(upd -> silent.send("I don't know how to go left.", getChatId(upd))) | ||
.onlyIf(hasMessageWith("left")) | ||
.next(saidLeft) | ||
.build(); | ||
``` | ||
And now, saidLeft reply becomes: | ||
```java | ||
Reply saidLeft = Reply.of(upd -> silent.send("Sir, I have gone left.", getChatId(upd)), | ||
hasMessageWith("go left or else")); | ||
``` | ||
Now, after your naughty bot retaliates, the user can say "go left or else" to force the bot to go left. Awesome, our logic now looks like this: | ||
<div align="center"> | ||
|
||
 | ||
<img src="./img/replyflow_diagram.svg"> | ||
|
||
</div> | ||
|
||
## Complete Example | ||
```java | ||
public static class ReplyFlowBot extends AbilityBot { | ||
public class ReplyFlowBot extends AbilityBot { | ||
public ReplyFlowBot(String botToken, String botUsername) { | ||
super(botToken, botUsername); | ||
} | ||
|
||
@Override | ||
public int creatorId() { | ||
return <YOUR ID HERE>; | ||
} | ||
|
||
public ReplyFlow directionFlow() { | ||
Reply saidLeft = Reply.of(upd -> silent.send("Sir, I have gone left.", getChatId(upd)), | ||
hasMessageWith("go left or else")); | ||
|
||
ReplyFlow leftflow = ReplyFlow.builder(db) | ||
.action(upd -> silent.send("I don't know how to go left.", getChatId(upd))) | ||
.onlyIf(hasMessageWith("left")) | ||
.next(saidLeft).build(); | ||
|
||
Reply saidRight = Reply.of(upd -> silent.send("Sir, I have gone right.", getChatId(upd)), | ||
hasMessageWith("right")); | ||
|
||
return ReplyFlow.builder(db) | ||
.action(upd -> silent.send("Command me to go left or right!", getChatId(upd))) | ||
.onlyIf(hasMessageWith("wake up")) | ||
.next(leftflow) | ||
.next(saidRight) | ||
.build(); | ||
} | ||
|
||
@NotNull | ||
private Predicate<Update> hasMessageWith(String msg) { | ||
return upd -> upd.getMessage().getText().equalsIgnoreCase(msg); | ||
} | ||
} | ||
``` | ||
## Inline Declaration | ||
As you can see in the above example, we used a bottom-up approach. We declared the leaf replies before we got to the root reply flow. | ||
If you'd rather have a top-down approach, then you may declare your replies inline to achieve that. | ||
```java | ||
ReplyFlow.builder(db) | ||
.action(upd -> silent.send("Command me to go left or right!", getChatId(upd))) | ||
.onlyIf(hasMessageWith("wake up")) | ||
.next(Reply.of(upd -> | ||
silent.send("Sir, I have gone left.", getChatId(upd)), | ||
hasMessageWith("left"))) | ||
.next(Reply.of(upd -> | ||
silent.send("Sir, I have gone right.", getChatId(upd)), | ||
hasMessageWith("right"))) | ||
.build(); | ||
``` | ||
## State Consistency | ||
Under the hood, AbilityBot will generate integers that represent the state of the instigating user. However, | ||
if you add more replies and reply flows, these integers may no longer be consistent. If you'd like to always have consistent state IDs, you | ||
should always pass a unique ID to the ReplyFlow builder like so: | ||
```java | ||
ReplyFlow.builder(db, <ID HERE>) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.