-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
26 lines (23 loc) · 995 Bytes
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Import the tower class, and 'time.sleep'
from time import sleep
from belltower import RingingRoomTower
# Create a new tower, and tell it to join tower ID 765432918
tower = RingingRoomTower(765432918)
# Register a function to be called when a chat message is posted
@tower.on_chat
def on_chat(user, message):
print(f"{user} says '{message}'")
# If the message is 'hello' in any capitalisation, send 'Hello <user>'.
# The first argument is the name to put next to the chat message
if message.lower() == "hello":
tower.chat("RR ChatBot", f"Hello, {user}!")
# The 'with' block makes sure that 'tower' has a chance to gracefully shut
# down the connection if the program crashes
with tower:
# Wait until the tower is loaded
tower.wait_loaded()
# Go into an infinite loop. It doesn't matter what the main thread does,
# but if it leaves the `with` block then the Tower's connection will
# close and become unusable
while True:
sleep(1000)