twitch
ツイッチチャット(IRC)ボット
サーチ…
備考
ツイッチチャットは、簡単なIRCチャットです。深刻な開発には、最も包括的かつ一般的なリソースを含む複数のドキュメントがあります。http : //ircdocs.horse/
接続、ハンドシェイク
IRCは、基本的な、平文ベースのTCPプロトコルです。 Twitchへの接続は、認証に違いがある通常のIRCサービスと同じように動作します:
接続開始>ハンドシェイク>使用法
ハンドシェイクは定期的に正しいことを得るために最も難しい部分です:
サーバーとの接続を確立した後 、 PASSとNICKを提供する必要がありますPASSはOAuthトークン( ここで生成できます )であり、 USERはこのOAuthトークンのユーザー名です。
ハンドシェイクは、(次のようにある< 、クライアントからサーバに送信される>サーバからクライアントに送信されます):
< PASS oauth:your_oauth_token
< NICK your_username
> :tmi.twitch.tv 001 your_username :connected to TMI
> :tmi.twitch.tv 002 your_username :your host is TMI
> :tmi.twitch.tv 003 your_username :this server is pretty new
> :tmi.twitch.tv 004 your_username tmi.twitch.tv 0.0.1 w n
> :tmi.twitch.tv 375 your_username :- tmi.twitch.tv Message of the day -
> :tmi.twitch.tv 372 your_username :- not much to say here
> :tmi.twitch.tv 376 your_username :End of /MOTD command
これらのMODE 、 376または422いずれかを受け取ったら、あなたはいいですね、twitchサーバーに次のようなコマンドを送ることができます:
> JOIN :#gamesdonequick
> PRIVMSG #gamesdonequick :Hello world!
クライアント/サーバーコマンドのガイド全体については、 こちらを参照してください 。
ツィッチ特有の機能
ツイッチでは標準的なIRCサービスが使用されていますが、IRCサービスでは、ツイッチのウェブサイトのチャンネルのアクティビティに関連するイベントがあります。ここでの例としては、スローモードの有効化または無効化、ストリーマのチャットでの加入者専用モードの有効化、ホスティング活動、ビット/応援活動などがあります。
これらのツィッチ固有の機能の詳細は、 ここで見つけることができるツイッチIRCのGitHubドキュメントに記載されています 。
Python
ここでは簡単なPythonのコマンドラインプログラムがあります。このプログラムはTwitchチャンネルにボットとして接続し、いくつかの簡単なコマンドに応答します。
依存関係:
-
ircPython lib (pip install ircまたはeasy_install irc)
出典: https : //gist.github.com/jessewebb/65b554b5be784dd7c8d1
import logging
import sys
from irc.bot import SingleServerIRCBot
# config
HOST = 'irc.twitch.tv'
PORT = 6667
USERNAME = 'nickname'
PASSWORD = 'oauth:twitch_token' # http://www.twitchapps.com/tmi/
CHANNEL = '#channel'
def _get_logger():
logger_name = 'vbot'
logger_level = logging.DEBUG
log_line_format = '%(asctime)s | %(name)s - %(levelname)s : %(message)s'
log_line_date_format = '%Y-%m-%dT%H:%M:%SZ'
logger_ = logging.getLogger(logger_name)
logger_.setLevel(logger_level)
logging_handler = logging.StreamHandler(stream=sys.stdout)
logging_handler.setLevel(logger_level)
logging_formatter = logging.Formatter(log_line_format, datefmt=log_line_date_format)
logging_handler.setFormatter(logging_formatter)
logger_.addHandler(logging_handler)
return logger_
logger = _get_logger()
class VBot(SingleServerIRCBot):
VERSION = '1.0.0'
def __init__(self, host, port, nickname, password, channel):
logger.debug('VBot.__init__ (VERSION = %r)', self.VERSION)
SingleServerIRCBot.__init__(self, [(host, port, password)], nickname, nickname)
self.channel = channel
self.viewers = []
def on_welcome(self, connection, event):
logger.debug('VBot.on_welcome')
connection.join(self.channel)
connection.privmsg(event.target, 'Hello world!')
def on_join(self, connection, event):
logger.debug('VBot.on_join')
nickname = self._parse_nickname_from_twitch_user_id(event.source)
self.viewers.append(nickname)
if nickname.lower() == connection.get_nickname().lower():
connection.privmsg(event.target, 'Hello world!')
def on_part(self, connection, event):
logger.debug('VBot.on_part')
nickname = self._parse_nickname_from_twitch_user_id(event.source)
self.viewers.remove(nickname)
def on_pubmsg(self, connection, event):
logger.debug('VBot.on_pubmsg')
message = event.arguments[0]
logger.debug('message = %r', message)
# Respond to messages starting with !
if message.startswith("!"):
self.do_command(event, message[1:])
def do_command(self, event, message):
message_parts = message.split()
command = message_parts[0]
logger.debug('VBot.do_command (command = %r)', command)
if command == "version":
version_message = 'Version: %s' % self.VERSION
self.connection.privmsg(event.target, version_message)
if command == "count_viewers":
num_viewers = len(self.viewers)
num_viewers_message = 'Viewer count: %d' % num_viewers
self.connection.privmsg(event.target, num_viewers_message)
elif command == 'exit':
self.die(msg="")
else:
logger.error('Unrecognized command: %r', command)
@staticmethod
def _parse_nickname_from_twitch_user_id(user_id):
# [email protected]
return user_id.split('!', 1)[0]
def main():
my_bot = VBot(HOST, PORT, USERNAME, PASSWORD, CHANNEL)
my_bot.start()
if __name__ == '__main__':
main()