72 lines
2.4 KiB
Python
Raw Normal View History

2025-02-01 14:58:18 +00:00
import asyncio
2025-02-10 13:16:55 +00:00
import logging
logging.basicConfig(level=logging.DEBUG)
2025-02-01 14:58:18 +00:00
from snekbot.bot import Bot
class ExampleBot(Bot):
2025-02-10 13:16:55 +00:00
async def on_join(self, channel_uid):
print(f"I joined!")
await self.send_message(
channel_uid,
f"Hello, i'm actively part of the conversation in channel {channel_uid} now, you don't have to mention me anymore. "
)
2025-02-01 14:58:18 +00:00
2025-02-10 13:16:55 +00:00
async def on_leave(self, channel_uid):
print(f"I left!!")
await self.send_message(
channel_uid,
"I stop actively being part of the conversation now. Bye!"
)
2025-02-01 14:58:18 +00:00
2025-02-10 13:16:55 +00:00
async def on_ping(self,username, user_nick, channel_uid, message):
print(f"Ping from {user_nick} in channel {channel_uid}: {message}")
2025-02-01 14:58:18 +00:00
await self.send_message(
2025-02-10 13:16:55 +00:00
channel_uid,
"pong " + message
2025-02-01 14:58:18 +00:00
)
async def on_own_message(self, data):
print(f"Received my own message: {data.message}")
2025-02-10 13:16:55 +00:00
async def on_mention(self, username, user_nick, channel_uid, message):
2025-02-01 14:58:18 +00:00
2025-02-10 13:16:55 +00:00
message = message[len(self.username) + 2 :]
print(f"Mention from {user_nick}: {message}")
2025-02-01 14:58:18 +00:00
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```"
)
2025-02-10 13:16:55 +00:00
await self.send_message(channel_uid, result)
else:
await self.send_message(channel_uid, f'Hey {user_nick}, Thanks for mentioning me "{message}".')
2025-02-01 14:58:18 +00:00
2025-02-10 13:16:55 +00:00
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()
2025-02-01 14:58:18 +00:00
result = None
2025-02-10 13:16:55 +00:00
if "hello" in message:
result = f"Hi @{sender_nick}"
2025-02-01 14:58:18 +00:00
elif "bye" in message:
2025-02-10 13:16:55 +00:00
result = f"Bye @{sender_nick}"
2025-02-01 14:58:18 +00:00
if result:
2025-02-10 13:16:55 +00:00
await self.send_message(channel_uid, result)
2025-02-01 14:58:18 +00:00
2025-02-10 13:16:55 +00:00
bot = ExampleBot(url="ws://snek.molodetz.nl/rpc.ws", username="example", password="example")
2025-02-01 14:58:18 +00:00
asyncio.run(bot.run())