Raw source file available here .
import asyncio
import logging
logging.basicConfig(level=logging.DEBUG)
from snekbot.bot import Bot
class ExampleBot(Bot):
async def on_join(self, channel_uid):
await super().on_join(channel_uid)
print(f"I joined!")
channel = await self.get_channel(channel_uid)
await self.send_message(
channel_uid,
f"Hello, i'm actively part of the conversation in channel {channel['name']} now, you don't have to mention me anymore. ",
)
async def on_leave(self, channel_uid):
await super().on_leave(channel_uid)
print(f"I left!!")
await self.send_message(
channel_uid, "I stop actively being part of the conversation now. Bye!"
)
async def on_ping(self, username, user_nick, channel_uid, message):
channel = await self.get_channel(channel_uid)
print(f"Ping from {user_nick} in channel {channel['name']}: {message}")
await self.send_message(channel_uid, "pong " + message)
async def on_own_message(self, channel_uid, message):
channel = await self.get_channel(channel_uid)
print(f"Received my own message in channel {channel['name']}: {message}")
async def on_mention(self, username, user_nick, channel_uid, message):
message = message[len(self.username) + 2 :]
print(f"Mention from {user_nick}: {message}")
if "source" in message:
with open(__file__) as f:
result = f.read()
result = result.replace(f'"{self.username}"', '"example username"')
result = result.replace(self.password, "example password")
result = (
"This is the actual source code running me now. Fresh from the bakery:\n\n```python\n"
+ result
+ "\n```"
)
await self.send_message(channel_uid, result)
else:
await self.send_message(
channel_uid, f'Hey {user_nick}, Thanks for mentioning me "{message}".'
)
async def on_message(self, sender_username, sender_nick, channel_uid, message):
print(f"Message from {sender_nick}: {message}")
if not self.has_joined(channel_uid):
print(f"Probably not for me since i'm not mentioned and not joined yet")
return
message = message.lower()
result = None
if "hello" in message:
result = f"Hi @{sender_nick}"
elif "bye" in message:
result = f"Bye @{sender_nick}"
if result:
await self.send_message(channel_uid, result)
bot = ExampleBot(
url="ws://snek.molodetz.nl/rpc.ws", username="example", password="xxxxxx"
)
asyncio.run(bot.run())