![]() ![]() ![]() First, we need to pick the username out of the string. Once we've determined that this is a chat message, we can strip out most of the text, since we only need one copy of the username and their message. # Make sure you prefix the quotes with an 'r'! PRIVMSG #\w :") Import Python's regular expressions module: # bot.py import reĪnd define the pattern we're looking for. IRC's formatting of a chat message is rather complicated, so we'll use a regular expression to pick out messages that match this pattern. We do, however, want to see chat messages, which look like this: PRIVMSG #channel :message We don't need to know who's joining or leaving the channel, and it's not very important who the other moderators are. Not all messages from the server are worthy of the bot's attention. ![]() Keyword arguments: sock - the socket over which to send the message msg - the message to be sent """ nd("PRIVMSG #".format(user, secs)) # bot.pyĭef chat(sock, msg): """ Send a chat message to the server. I've provided docstrings in compliance with PEP 257 which will provide brief explanation of the functions. Sockets provide a very clear send-and-receive interface for network communications, so these functions won't be very complicated. We use sockets as the network abstraction for this bot. Still, the code will end up much cleaner if we define some utility functions first. Twitch uses IRC as its chat protocol, which makes most communication trivial. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |