我正在使用 Python 2.7,我正在尝试为 twitch 创建 IRC 机器人,但我遇到了问题。我在其他 IRC(例如“webchat.freenode.net”)中创建了机器人,一切正常..我的 twitch 代码:
import time
import socket
HOST = "irc.twitch.tv"
PORT = 6667
BOTNICK = "thebot"
PASSWORD = "oauth:nph788dap10fu6ozlzv1b32fzm4r8q"
CHAN = "#fordotis10"
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((HOST, PORT))
irc.setblocking(False)
time.sleep(1)
irc.send("PASS "+PASSWORD+"\r\n")
time.sleep(1)
irc.send("USER "+BOTNICK+" "+BOTNICK+" "+BOTNICK+" :I AM BOT!\r\n")
time.sleep(1)
irc.send("NICK "+BOTNICK+"\r\n")
time.sleep(1)
irc.send("JOIN "+CHAN+"\r\n")
text= ""
while 1:
try:
text = irc.recv(1024)
print text
except Exception:
pass
if text.find("PING")!=-1:
irc.send("PONG "+text.split()[1]+"\r\n")
使用此代码,我收到以下错误:
:tmi.twitch.tv 注意 * :登录验证失败
我错过了什么?
最佳答案
终于,我发现了我的代码的问题!!所以,我将编写用两种方法创建 twitch 机器人的步骤!
第一种方法:
您必须创建一个帐户才能 twitch
import socket import re HOST = "irc.twitch.tv" PORT = 6667 NICK = "botname" #The account Name PASS = "The password here" #http://www.twitchapps.com/tmi/ CHAN = "#Channel" s = socket.socket() s.connect((HOST, PORT)) s.send("PASS {}\r\n".format(PASS)) s.send("NICK {}\r\n".format(NICK)) s.send("JOIN {}\r\n".format(CHAN)) while True: resp = s.recv(1024) print resp if resp == "PING :tmi.twitch.tv\r\n": s.send("PONG :tmi.twitch.tv\r\n") if resp.find("hi")!=-1: s.send("PRIVMSG "+CHAN+" :HELLO\r\n") resp = ""
第二种方式:
我找到了pytitcherapi,您可以从以下位置阅读文档:http://pytwitcherapi.readthedocs.io/en/latest/
import pytwitcherapi
import time
import webbrowser
import threading
import queue
session = pytwitcherapi.TwitchSession()
url = session.get_auth_url()
session.start_login_server()
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open(url) #OAuth generator
while not session.authorized:
time.sleep(1)
time.sleep(2)
print "Authorized"
session.shutdown_login_server()
channel = session.get_channel("ChannelName")
client = pytwitcherapi.IRCClient(session,channel)
t = threading.Thread(target = client.process_forever)
t.start()
print "connected"
while True:
try:
m = client.messages.get(False)
if m.text == "!ping":
client.send_msg("pong!")
if m.text =="hey":
client.send_msg("Hey You!")
except queue.Empty:
pass
希望这一切对你有帮助!!
关于python - 创建 twitch irc 机器人(登录身份验证失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621457/