這是我要實現的代碼。簡單地說,它是一個小型聊天機器人,連接到服務器,並將testroom1
與user
和pass
憑證相連接。使用圖書館,事件被稱爲每次有人進入消息進入聊天室,而這個機器人將打印他們的消息,與格式化:在事件中使用條件
import ch
class bot(ch.RoomManager):
for x in range(0, 3):
def onMessage(self, room, user, message):
print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
rooms = ["testroom1"]
username = "user"
password = "pass"
bot.easy_start(rooms,username,password)
然而,關鍵的問題是這樣的:
for x in range(0, 3):
我只想讓「onMessage」函數訪問3次,然後不打印任何更多的消息。這似乎不起作用,並且在我退出之前它會繼續打印消息。
如果可能,我想限制onMessage函數,while循環在我的程序中進一步循環,並在幾種不同的情況下使用它來解析不同時間的消息。
的CH庫和基礎我簡單的代碼的來源是here
在這個問題上的任何見解表示讚賞。
你能提供基類'ch.RoomManager'的代碼嗎?或者其他的'easy_start'定義在哪裏?也有可能你打算把你的for循環放在方法'onMessage'中? – stoebelj
代碼太長,所以我在這裏粘貼了RoomManager:https://pastebin.com/aJzYbFhc 由於或者沒有循環,onMessage方法會持續運行。我想使用onMessage在倒計時的最後幾秒鐘內打印郵件,然後在完全獨立的情況下再次使用它來處理命令的郵件。 – chatbottest
這是可以找到onMessage的完整ch.py:https://pastebin.com/8ukS4VR1 – chatbottest