Hi everyone!
I’m trying to create a Python bot using irc.bot.SingleServerIRCBot
library, found on GitHub with a “blink” example. I’ll try to explain step by step waht i’ve done.
I read about oauth token and i’ve created a specific account for this scope, linked it in TMI to get oauth token and i registered the application on dev.twitch.tv to get the ClientID using my bot’s account.
Then i’ve tried to use the blink file to see if all was correct, and it was not
chatbot.py:
import sys, irc.bot, requests
class TwitchBot(irc.bot.SingleServerIRCBot):
def __init__(self, username, client_id, token, channel):
self.client_id = client_id
self.token = token
self.channel = '#' + channel
# Get the channel id, we will need this for v5 API calls
url = 'https://api.twitch.tv/kraken/users?login=' + channel
headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json',
'Authorization': 'oauth:' + token}
r = requests.get(url, headers=headers).json()
self.channel_id = r['users'][0]['_id']
# Create IRC bot connection
server = 'irc.chat.twitch.tv'
port = 6667
print('Connecting to ' + server + ' on port ' + str(port) + '...')
irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:' + token)], username, username)
def on_welcome(self, c, e):
print('Joining ' + self.channel)
# You must request specific capabilities before you can use them
c.cap('REQ', ':twitch.tv/membership')
c.cap('REQ', ':twitch.tv/tags')
c.cap('REQ', ':twitch.tv/commands')
c.join(self.channel)
print('Joined ' + self.channel)
c.privmsg(self.channel, "Connected!")
def on_pubmsg(self, c, e):
# If a chat message starts with an exclamation point, try to run it as a command
if e.arguments[0][:1] == '!':
cmd = e.arguments[0].split(' ')[0][1:]
print('Received command: ' + cmd)
self.do_command(e, cmd)
return
def do_command(self, e, cmd):
c = self.connection
# Poll the API to get current game.
if cmd == "game":
url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id
headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
r = requests.get(url, headers=headers).json()
c.privmsg(self.channel, str(r['display_name']) + ' is currently playing ' + str(r['game']))
# Poll the API the get the current status of the stream
elif cmd == "title":
url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id
headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
r = requests.get(url, headers=headers).json()
c.privmsg(self.channel, str(r['display_name']) + ' channel title is currently ' + str(r['status']))
# Provide basic information to viewers for specific commands
elif cmd == "raffle":
message = "This is an example bot, replace this text with your raffle text."
c.privmsg(self.channel, message)
elif cmd == "schedule":
message = "This is an example bot, replace this text with your schedule text."
c.privmsg(self.channel, message)
# The command was not recognized
else:
c.privmsg(self.channel, "Did not understand command: " + cmd)
def main():
if len(sys.argv) != 5:
print('Usage: twitchbot <username> <client id> <token> <channel>')
sys.exit(1)
username = sys.argv[1]
client_id = sys.argv[2]
token = sys.argv[3]
channel = sys.argv[4]
bot = TwitchBot(username, client_id, token, channel)
bot.start()
if __name__ == "__main__":
main()
then i’ve executed it using:
py chatbot.py <UserName of my bot's account> <ClientID of the application registered> <oAuth Token> <UserName of my channel>
The program connects with my channel and it should send Connected!
but i can see it only if i refesh the page, so i’ve supposed that something with oauth token was wrong and i’ve done various try and this is the only way which the bot connects so i’ve tried to write any commands but the program doesn’t receive feed by this.
If someone can help me to understand what i wrong thank you forward.