# Making a Python Twitch Bot

**URL:** <https://discuss.dev.twitch.com/t/making-a-python-twitch-bot/9250>\
**Category:** Chat and IRC\
**Created:** [February 14, 2017, 10:52am UTC](https://discuss.dev.twitch.com/t/making-a-python-twitch-bot/9250 "2017-02-14T10:52:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thewoodenmanikin](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@thewoodenmanikin](https://discuss.dev.twitch.com/u/thewoodenmanikin)\
**Post date:** [February 14, 2017, 10:52am UTC](https://discuss.dev.twitch.com/t/making-a-python-twitch-bot/9250/1 "2017-02-14T10:52:50Z")

</div>

Hi, i’m making a twitchbot and i encountered an issue with this code

# bot.py

def chat(sock, msg):

```
sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg))

```

import cfg  
import socket  
import re

# Network Stuff

s = socket.socket()  
s.connect((cfg.TWITCH, cfg.PORT))  
s.send(“PASS {}\r\n”.format(cfg.OAUTH).encode(“utf-8”))  
s.send(“NICK {}\r\n”.format(cfg.BOT).encode(“utf-8”))  
s.send(“JOIN {}\r\n”.format(cfg.HOST).encode(“utf-8”))

CHAT\_MSG=re.compile(r"^:\w+!\w+@\[w+.tmi.twitch.tv](http://w+.tmi.twitch.tv) PRIVMSG #\w+ :")

while True:  
response = s.recv(1024).decode(“utf-8”)  
if response == “PING :tmi.twitch.tv\r\n”:  
s.send(“PONG :tmi.twitch.tv\r\n”.encode(“utf-8”))  
else:  
username = re.search(r"\w+", response).group(0)  
message = CHAT\_MSG.sub("", response)  
print(username + ": " + message)  
for cfg.info in cfg.info:  
if re.match(cfg.info, message):  
s.send(s, “,message”)#Command Function Goes Here  
break

it says it connects to the channel but doesn’t send/recieve any information, i would really appreciate your input and help

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [February 14, 2017, 2:02pm UTC](https://discuss.dev.twitch.com/t/making-a-python-twitch-bot/9250/2 "2017-02-14T14:02:24Z")

</div>

JOIN’s cfg.HOST missing #? Is it all lowercase?

---

<div class="post-metadata">

**Author:** ![OG\_SBSilver](https://avatars.discourse-cdn.com/v4/letter/o/e495f1/32.png) [@OG\_SBSilver](https://discuss.dev.twitch.com/u/OG_SBSilver)\
**Post date:** [March 11, 2017, 6:21am UTC](https://discuss.dev.twitch.com/t/making-a-python-twitch-bot/9250/3 "2017-03-11T06:21:53Z")

</div>

Sadly your Python code lost its formatting but I believe I found your “cannot send” issue.

Twitch IRC chat does need the “\r\n” at the end of each message string and I do not see that in your code for sending messages to the chat. You have it for the PONG reply.

As for receiving, did you check your code as 3ventic suggested?

Finally, are you using Python 2.7 or Python 3.x?

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [April 10, 2017, 6:22am UTC](https://discuss.dev.twitch.com/t/making-a-python-twitch-bot/9250/4 "2017-04-10T06:22:10Z")

</div>

Closed on April 10, 2017, 6:22am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
