· 7 years ago · May 09, 2018, 06:18 PM
1import sys
2import irc.bot
3import requests
4import RPi.GPIO as GPIO # Import the GPIO Library
5import time
6
7class TwitchBot(irc.bot.SingleServerIRCBot):
8 def __init__(self, username, client_id, token, channel):
9 self.client_id = client_id
10 self.token = token
11 self.channel = '#' + channel
12
13 GPIO.setmode(GPIO.BCM)
14 GPIO.setwarnings(False)
15
16 GPIO.setup(7, GPIO.OUT) #Links vooruit
17 GPIO.setup(8, GPIO.OUT) #Links achteruit
18 GPIO.setup(9, GPIO.OUT) #Rechts achteruit
19 GPIO.setup(10, GPIO.OUT) #Rechts vooruit
20
21 # Get the channel id, we will need this for v5 API calls
22 url = 'https://api.twitch.tv/kraken/users?login=' + channel
23 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
24 r = requests.get(url, headers=headers).json()
25 print r
26 self.channel_id = r['users'][0]['_id']
27
28 # Create IRC bot connection
29 server = 'irc.chat.twitch.tv'
30 port = 6667
31 print 'Connecting to ' + server + ' on port ' + str(port) + '...'
32 irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)
33
34
35 def on_welcome(self, c, e):
36 print 'Joining ' + self.channel
37 # You must request specific capabilities before you can use them
38 c.cap('REQ', ':twitch.tv/membership')
39 c.cap('REQ', ':twitch.tv/tags')
40 c.cap('REQ', ':twitch.tv/commands')
41 c.join(self.channel)
42
43 def on_pubmsg(self, c, e):
44 # If a chat message starts with an exclamation point, try to run it as a command
45 if e.arguments[0][:1] == '!':
46 cmd = e.arguments[0].split(' ')[0][1:]
47 print 'Received command: ' + cmd
48 self.do_command(e, cmd.lower())
49 return
50
51 def do_command(self, e, cmd):
52 c = self.connection
53
54 # Poll the API to get current game.
55 if cmd in ["f", "forward"]:
56 c.privmsg(self.channel, "Going forward...")
57 GPIO.output(7, 1)
58 GPIO.output(10, 1)
59 time.sleep(1)
60 GPIO.output(7, 0)
61 GPIO.output(10, 0)
62
63 elif cmd in ["b", "back", "backward", "backwards"]:
64 c.privmsg(self.channel, "Going backward...")
65 GPIO.output(8, 1)
66 GPIO.output(9, 1)
67 time.sleep(1)
68 GPIO.output(8, 0)
69 GPIO.output(9, 0)
70 elif cmd in ["l", "left"]:
71 c.privmsg(self.channel, "Turning left...")
72 GPIO.output(10, 1)
73 GPIO.output(7, 0)
74 time.sleep(1)
75 GPIO.output(10, 0)
76 elif cmd in ["r", "right"]:
77 c.privmsg(self.channel, "Turning right...")
78 GPIO.output(7, 1)
79 GPIO.output(10, 0)
80 time.sleep(1)
81 GPIO.output(7, 0)
82 # The command was not recognized
83 else:
84 c.privmsg(self.channel, "Did not understand command: " + cmd + ", try [!left, !right, !forward, !back] or shorthand [!l,!r,!f,!b]")
85
86def main():
87 if len(sys.argv) != 5:
88 print("Usage: twitchbot <username> <client id> <token> <channel>")
89 sys.exit(1)
90
91 username = sys.argv[1]
92 client_id = sys.argv[2]
93 token = sys.argv[3]
94 channel = sys.argv[4]
95
96 bot = TwitchBot(username, client_id, token, channel)
97 bot.start()
98
99if __name__ == "__main__":
100 main()