Move example bots internally
This commit is contained in:
parent
dfb5079aec
commit
6272ac9475
46
README.md
46
README.md
@ -25,14 +25,9 @@ $ pip install xbotlib
|
|||||||
Put the following in a `echo.py` file.
|
Put the following in a `echo.py` file.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from xbotlib import Bot
|
from xbotlib import EchoBot
|
||||||
|
|
||||||
class EchoBot(Bot):
|
EchotBot()
|
||||||
def react(self, message):
|
|
||||||
if message.type == "chat":
|
|
||||||
self.reply(message.body, to=message.sender)
|
|
||||||
|
|
||||||
MyBot()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
And then `python echo.py`. You will be asked a few questions like which account
|
And then `python echo.py`. You will be asked a few questions like which account
|
||||||
@ -40,19 +35,40 @@ details your bot will be using.
|
|||||||
|
|
||||||
This will generate a `bot.conf` file in the same working directory for further use.
|
This will generate a `bot.conf` file in the same working directory for further use.
|
||||||
|
|
||||||
## More Examples
|
Here's the code for the `EchoBot`.
|
||||||
|
|
||||||
- **[EchoBot](./examples/echo.py)**: Sends back what you sent it
|
```python
|
||||||
- **[WhisperBot](./examples/whisper.py)**: Pseudo-anonymous whispering in group chats
|
class EchoBot(Bot):
|
||||||
|
"""Gives back what you sent it.
|
||||||
|
|
||||||
See the [examples](./examples/) directoy for all listings.
|
Just direct message the bot and see if you get back what you sent. It also
|
||||||
|
works in group chats but in this case you need to summon the bot using its
|
||||||
|
nickname Usually like so.
|
||||||
|
|
||||||
|
echobot:foo
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def react(self, message):
|
||||||
|
"""Send back what we get."""
|
||||||
|
if message.type == "chat":
|
||||||
|
self.reply(message.body, to=message.sender)
|
||||||
|
```
|
||||||
|
|
||||||
|
## All examples
|
||||||
|
|
||||||
|
- **EchoBot**: Sends back what you sent it
|
||||||
|
- **WhisperBot**: Pseudo-anonymous whispering in group chats
|
||||||
|
|
||||||
|
See [xbotlib.py](./xbotlib.py) for all example bots.
|
||||||
|
|
||||||
## API Reference
|
## API Reference
|
||||||
|
|
||||||
Your bot always sub-classes the `Bot` class provided from `xbotlib`. All
|
When writing your own bot, you always sub-classes the `Bot` class provided from
|
||||||
underling functions can be extended. For example, if you want to enable more
|
`xbotlib`. All underling functions can be extended. For example, if you want to
|
||||||
plugins or add different functionality. If something feels awkward then please
|
enable more plugins or add different functionality. If something feels awkward
|
||||||
raise a ticket for that. Seamlessness is still a bitch but we're trying anyway.
|
then please raise a ticket for that. Seamlessness is still a bitch but we're
|
||||||
|
trying anyway.
|
||||||
|
|
||||||
> Bot.react(message)
|
> Bot.react(message)
|
||||||
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
from xbotlib import Bot
|
|
||||||
|
|
||||||
|
|
||||||
class EchoBot(Bot):
|
|
||||||
"""Gives back what you sent it.
|
|
||||||
|
|
||||||
Just direct message the bot and see if you get back what you sent.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def react(self, message):
|
|
||||||
"""Send back what we get."""
|
|
||||||
if message.type == "chat":
|
|
||||||
self.reply(message.body, to=message.sender)
|
|
||||||
|
|
||||||
|
|
||||||
EchoBot()
|
|
@ -1,30 +0,0 @@
|
|||||||
from xbotlib import Bot
|
|
||||||
|
|
||||||
|
|
||||||
class WhisperBot(Bot):
|
|
||||||
"""Pseudo-anonymous whispering in group chats.
|
|
||||||
|
|
||||||
In order to activate this bot you can invite it to your group chat. Once
|
|
||||||
invited, you can directly message the bot outside of the group chat and
|
|
||||||
tell it you want it to whisper your message into the group chat. The bot
|
|
||||||
will then do this on your behalf and not reveal your identity. This is nice
|
|
||||||
when you want to communicate with the group somewhat anonymously.
|
|
||||||
|
|
||||||
The bot accepts messages in the following form.
|
|
||||||
|
|
||||||
whisper:<room>:<message>
|
|
||||||
|
|
||||||
So, I might write it like so.
|
|
||||||
|
|
||||||
whisper:myroom@muc.foo.com:i love the music of avril lavigne
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def react(self, message):
|
|
||||||
"""Receive direct messages and pass them to group chats."""
|
|
||||||
if message.type == "chat" and "whisper" in message.body:
|
|
||||||
_, room, whisper = message.body.split(":")
|
|
||||||
self.reply(f"*whispers* {whisper}", room=room)
|
|
||||||
|
|
||||||
|
|
||||||
WhisperBot()
|
|
43
xbotlib.py
43
xbotlib.py
@ -176,3 +176,46 @@ class Bot(ClientXMPP):
|
|||||||
def react(self, message):
|
def react(self, message):
|
||||||
message = "You need to write your own `react` implementation"
|
message = "You need to write your own `react` implementation"
|
||||||
raise NotImplementedError(message)
|
raise NotImplementedError(message)
|
||||||
|
|
||||||
|
|
||||||
|
class EchoBot(Bot):
|
||||||
|
"""Gives back what you sent it.
|
||||||
|
|
||||||
|
Just direct message the bot and see if you get back what you sent. It also
|
||||||
|
works in group chats but in this case you need to summon the bot using its
|
||||||
|
nickname Usually like so.
|
||||||
|
|
||||||
|
echobot:foo
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def react(self, message):
|
||||||
|
"""Send back what we get."""
|
||||||
|
if message.type == "chat":
|
||||||
|
self.reply(message.body, to=message.sender)
|
||||||
|
|
||||||
|
|
||||||
|
class WhisperBot(Bot):
|
||||||
|
"""Pseudo-anonymous whispering in group chats.
|
||||||
|
|
||||||
|
In order to activate this bot you can invite it to your group chat. Once
|
||||||
|
invited, you can directly message the bot outside of the group chat and
|
||||||
|
tell it you want it to whisper your message into the group chat. The bot
|
||||||
|
will then do this on your behalf and not reveal your identity. This is nice
|
||||||
|
when you want to communicate with the group somewhat anonymously.
|
||||||
|
|
||||||
|
The bot accepts messages in the following form.
|
||||||
|
|
||||||
|
whisper:<room>:<message>
|
||||||
|
|
||||||
|
So, I might write it like so.
|
||||||
|
|
||||||
|
whisper:myroom@muc.foo.com:i love the music of avril lavigne
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def react(self, message):
|
||||||
|
"""Receive direct messages and pass them to group chats."""
|
||||||
|
if message.type == "chat" and "whisper" in message.body:
|
||||||
|
_, room, whisper = message.body.split(":")
|
||||||
|
self.reply(f"*whispers* {whisper}", room=room)
|
||||||
|
Reference in New Issue
Block a user