Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate chatbot to use the Helix API #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $ pip install irc
To run the chatbot, you will need to provide an OAuth access token with the chat_login scope. You can reference an authentication sample to accomplish this, or simply use the [Twitch Chat OAuth Password Generator](http://twitchapps.com/tmi/).

```sh
$ python chatbot.py <username> <client id> <token> <channel>
$ python3 chatbot.py <username> <client id> <token> <channel>
```
* Username - The username of the chatbot
* Client ID - Your registered application's Client ID to allow API calls by the bot
Expand Down
59 changes: 38 additions & 21 deletions chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,62 @@
http://aws.amazon.com/apache2.0/

or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Modifications 2021 by RikiRC

Adaptation to Helix API by TotallyMonica
'''

import sys
import irc.bot
import requests
import sys, irc.bot, requests

class TwitchBot(irc.bot.SingleServerIRCBot):
def __init__(self, username, client_id, token, channel):
def __init__(self, username, client_id, client_secret, token, channel):
self.client_id = client_id
self.token = token
self.channel = '#' + channel
self.client_secret = client_secret
self.token = token.removeprefix("oauth:")
self.channel = '#' + channel.lower()

# 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'}
body = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'client_credentials'
}
keys = requests.post('https://id.twitch.tv/oauth2/token', body).json()
url = 'https://api.twitch.tv/helix/users?login=' + channel
headers = {
'Client-ID': client_id,
'Authorization': 'Bearer ' + keys['access_token']
}

r = requests.get(url, headers=headers).json()
self.channel_id = r['users'][0]['_id']

self.channel_id = r['data'][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)
url = 'https://api.twitch.tv/helix/users?login=' + channel
print('Connecting to ' + server + ' on port ' + str(port) + '...')
irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+self.token)], username, username)


def on_welcome(self, c, e):
print 'Joining ' + self.channel
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
print('Received command: ' + cmd)
self.do_command(e, cmd)
return

Expand All @@ -57,14 +73,14 @@ def do_command(self, e, cmd):
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, r['display_name'] + ' is currently playing ' + r['game'])
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, r['display_name'] + ' channel title is currently ' + r['status'])
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":
Expand All @@ -80,15 +96,16 @@ def do_command(self, e, cmd):

def main():
if len(sys.argv) != 5:
print("Usage: twitchbot <username> <client id> <token> <channel>")
print('Usage: twitchbot <username> <client id> <client secret> <token> <channel>')
sys.exit(1)

username = sys.argv[1]
client_id = sys.argv[2]
token = sys.argv[3]
channel = sys.argv[4]
username = sys.argv[1]
client_id = sys.argv[2]
client_secret = sys.argv[3]
token = sys.argv[3]
channel = sys.argv[4]

bot = TwitchBot(username, client_id, token, channel)
bot = TwitchBot(username, client_id, client_secret, token, channel)
bot.start()

if __name__ == "__main__":
Expand Down