Python IRC Bot, distinguish from channel messages and private messages
<p>I'm coding a simple IRC bot in Python. Actually, It can connect to a specific channel, read messages and send response, but it can't distinguish between channel messages and private messages. </p>

<p><strong>Example</strong>:<br>
John, connected to the same channel, send a private message to the Bot's chat, like "!say hello";
Bot have to send "hello" to the same private chat, only to John.<br>
Instead, when bot read "!say hello" in channel's board, have to send "hello" to the channel.</p>

<p><strong>My code:</strong>  </p>

<pre><code>ircmsg = connection.ircsock.recv(2048)
ircmsg_clean = ircmsg.strip(str.encode('\n\r'))
if ircmsg.find(str.encode("!say")) != -1:
    try:
        parts = ircmsg_clean.split()
        content = parts[4]
        connection.sendMsg(channel, content)
    except IndexError:
        connection.sendMsg(channel, "Invalid syntax")
</code></pre>

<p><strong>Connection file:</strong></p>

<pre><code>def sendmsg(channel, msg):
ircsock.send(str.encode("PRIVMSG " + channel +" :" + msg + "\n"))
</code></pre>

<p>I know how to send a message to a specific user:</p>

<pre><code>def sendPrivateMsg(username, msg):
ircsock.send(str.encode("PRIVMSG " + username + " :" + msg + "\n"))
</code></pre>

<p>But have to know from where the message came, channel or user, and send appropriate response.</p>

<p>Sorry for my bad english.</p>

<p>The source of the message is included in the protocol message, now you are just not using it. </p>

<p>In the part where you do this</p>

<pre><code>parts = ircmsg_clean.split()
</code></pre>

<p>you get it in to the list <code>parts</code>.</p>

<p>If I remember correctly, and understand the RFQ right, the irc message you receive from the server looks something like this:</p>

<blockquote>
  <p>:John!something@something.org PRIVMSG BotName :Are you receiving this message ?</p>
</blockquote>

<p>so splitting that would result in this:</p>

<pre><code>[':John!something@something.org', 'PRIVMSG', 'BotName', ':Are', 'you', 'receiving', 'this', 'message', '?']
</code></pre>

<p>The sender of the message is that the first element <code>parts</code>, so <code>parts[0]</code>. You would need to strip away the extra ':'.</p>

<p>The target of the message is at <code>parts[2]</code>, and that can be your name or the channel name, and you then need to compare the target to your own name to know if it is a message to you or to a channel.</p>

<p>If that is true, you can then do something like this:</p>

<pre><code>source = parts[0].strip(':')
content = ' '.join(parts[3:]).strip(':')
connection.sendMsg(source, content)
</code></pre>

<p>Notice that the actual content of the message is also splitted, so you need to rejoin that into a string if you are doing it like that. If you are hitting that <code>IndexError</code> it means that you received a message that had only one word, since the first word is at index 3, not 4.</p>

python networking bots irc channel