talkomatic.py 0.1.2
An easy-to-use Python versatile wrapper for the Talkomatic Bot interface ensuring best practices for making bots, and handling the REST API.
 
Loading...
Searching...
No Matches
parrot.py
Go to the documentation of this file.
1"""
2parrot.py
3
4This example shows how to make a bot repeating what the other users say.
5"""
6
7
8from talkomatic import Bot, RoomType # import the Bot class from the talkomatic package
9
10
11bot = Bot() # create a new Bot instance
12
13# with this decorator, the function will be called when the bot finishes connecting to the server
14@bot.on_connect
15async def on_connect() -> None:
16 # we find the first non-full room, and we join it
17 for room in bot.rooms:
18 if not room.is_full and room.room_type == RoomType.PUBLIC:
19 await bot.join_room(room)
20 return
21 print("We didn't find any non-full rooms to join. :(") # :(
22 await bot.disconnect()
23
24# this function will execute when:
25@bot.on_room_join # - when the bot joins a room
26@bot.on_user_message # - when a user sends a message
27@bot.on_user_join # - when a user joins the room
28@bot.on_user_leave # - when a user leaves the room
29async def send_parrot_message(*args) -> None: # we use *args here since we don't care about the arguments
30 bot_message = "" # this variable will store a string of the message the bot will send
31 for member in bot.current_room.users: # for each user in the bot's current room (including the bot itself)
32 if member == bot.user: # if the user is the bot itself, we skip it
33 continue
34 member_message = bot.get_user_message(member) # we get the member's message
35 bot_message += f"{member.username}: {member_message.strip()}\n" # we add the member's message to the bot's message
36
37 await bot.send_message(bot_message) # we send the bot's message!
38
39# we're ready to go! let's run the bot with a username and location
40bot.run("Parrot", "Bot")
None send_parrot_message(*args)
Definition parrot.py:29
None on_connect()
Definition parrot.py:15