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
echo.py
Go to the documentation of this file.
1"""
2echo.py
3
4This example shows how to create a commands with an argument that repeats
5whatever the user says.
6"""
7
8from talkomatic import Bot, CommandParameter, RoomType, User
9from talkomatic.commands import INFINITE_ARGS
10
11bot = Bot()
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 public room, we join it, and say "Hello, world!"
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
24user_messages = {}
26 # we concatenate all the user messages into one message to send
27 message = ""
28 for user_message in user_messages.values():
29 message += f"{user_message}\n"
30 await bot.send_message(message)
31
32@bot.command( # this is a decorator that turns the echo function into a command
33 name = "echo", # the name of the command
34 description = "This command repeats your message!", # the description of the command
35 parameters = [ # the parameters of the command
36 CommandParameter( # we want a parameter that takes any number of words in the message
37 "message", # the name of the parameter
38 "The message to repeat.", # the description of the parameter
39 str, # the type of the parameter
40 positional = True, # the parameter is positional
41 required = True, # the parameter is required
42 number_of_args = INFINITE_ARGS # the parameter can take any number of words
43 )
44 ]
45)
46async def echo(user: User, message) -> None:
47 if not isinstance(message, list): # is the user sending a message in the /echo
48 if user.id in user_messages: # if
49 del user_messages[user.id]
50 else:
51 # for each user, we store the message that'll be sent
52 user_messages[user.id] = f"{user.username} / {user.location} said: {' '.join(message)}"
53
54 # update the user messages
56
57@bot.on_user_leave
58async def on_user_leave(user: User) -> None:
59 if user.id in user_messages: # if they're in one of our user messages dictionary
60 del user_messages[user.id] # when a user leaves, we delete them from the user messages
61
62 # update the user messages
64
65# we can even make a "hidden" command that doesn't show up in the /help command
66@bot.command(name = "ping", hidden = True)
67async def ping(user: User) -> None:
68 # we can make little easter eggs with this hidden command feature!
69 await bot.send_message("Pong!")
70
71
72bot.run("Echo Bot", "do /help", create_help_command = True) # we let the bot create the help command automatically
Definition echo.py:1
None on_connect()
Definition echo.py:15
None on_user_leave(User user)
Definition echo.py:58
None ping(User user)
Definition echo.py:67
update_user_messages()
Definition echo.py:25