Search code examples
pythonregextwitch

In Python, how can I filter just the user and message in Twitch API's WHISPER command using regex?


I have this working well for PRIVMSG in main chat, however Twitch's WHISPER command is driving me a bit nuts - it includes a ton of extra information.

As an example, for PRIVMSG I have this working:

 CHAT_MSG=re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")

However, WHISPER returns this:

badges: @badges=;color=;display-name=;emotes=;message-id=34;thread-id=5575526_123681740;turbo=0;user-id=5575526;user-type= :teonnyn!teonnyn@teonnyn.tmi.twitch.tv WHISPER blastweb :Hello Bot

While PRIVMSG returns this:

teonnyn!teonnyn@teonnyn.tmi.twitch.tv PRIVMSG #blastweb :Hello Bot

PRIVMSG - the public connection, uses this to parse the chat from public:

        username = re.search(r"\w+", channelResponse).group(0)
        message = CHAT_MSG.sub("", channelResponse)
        print(username + ": " + message)

The same in WHISPER just returns the full "badges+" block of API information. What would the best way be to parse out all the extra information and get just the username and message for WHISPER?

I'm ultimately trying to reach just: teonnyn: Hello Bot


Solution

  • Following regex returns two matches - username and the message:

    user-type=\s+:(\w+)!.*:([\S\s]+)

    REGEX DEMO


    Here is working IDEONE DEMO:

    >>> import re
    >>> s = "badges: @badges=;color=;display-name=;emotes=;message-id=34;thread-id=5575526_123681740;turbo=0;user-id=5575526;user-type= :teonnyn!teonnyn@teonnyn.tmi.twitch.tv WHISPER blastweb :Hello Bot"
    >>> re.findall(r'user-type=\s+:(\w+)!.*:([\S\s]+)', s)
    [('teonnyn', 'Hello Bot')]