python - 创建 twitch irc 机器人(登录身份验证失败)

标签 python python-2.7 bots irc twitch

我正在使用 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 机器人的步骤!

第一种方法:

  1. 您必须创建一个帐户才能 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/

相关文章:

python - C 库的 python 包装的可维护性

python - 如何正确使用for循环从python中的csv绘制多行?

python - 使用 python2.7 和 nltk 将代词替换为其先行词

python - 在 Python 2.7 中导入 urllib 或 urllib 2 失败,出现 ImportError : cannot import name iskeyword

python - 用于 twitch 的原始 Python IRC 聊天机器人

python - "Account"对象没有属性获取火币Python API

python - lxml:拆分属性?

python - 具有构建提升功能的 Artifactory PyPi repo 布局

ios - Carthage 无法为 Xcode 机器人构建 obj-c 文件的 Nimble : cannot import <Nimble/Nimble-Swift. h>

python - 这个打印语法是什么? (打印右移)