Created
February 22, 2019 21:41
-
-
Save Lokno/54bceab984e6f152c335b52307276e19 to your computer and use it in GitHub Desktop.
Simple slack bot that polls twitch and announces when users are active
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os,time | |
from slackclient import SlackClient | |
from twitch import TwitchClient | |
client = TwitchClient(client_id=os.environ.get('TWITCH_TOKEN')) | |
# Dictionary of twitch accounts | |
twitchAccounts = {'NAME' : {'username' : 'TWITCH_USERNAME'}} | |
wait_time = 60 | |
# get twitch user ids | |
for name,data in twitchAccounts.iteritems(): | |
users = client.users.translate_usernames_to_ids([data['username']]) | |
if len(users) > 0: | |
data['id'] = users[0].id | |
data['status'] : 'inactive' | |
BOT_NAME = 'BOT_NAME' | |
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN')) | |
if __name__ == "__main__": | |
while True: | |
for name,data in twitchAccounts.iteritems(): | |
if data['id'] == '': | |
continue | |
try: | |
stream = client.streams.get_stream_by_user(channel_id=data['id']) | |
if stream is not None: | |
if data['status'] == 'inactive': | |
data['status'] = 'active' | |
msg = "Attention: %s is now streaming %s twitch.tv/%s" % (name[0].upper()+name[1:],stream['game'],data['username']) | |
print(msg) | |
slack_client.api_call("chat.postMessage", channel='scrubbychat', text=msg, as_user=True) | |
else: | |
data['status'] = 'inactive' | |
except: | |
pass | |
print("sleeping...") | |
time.sleep(wait_time) | |
print("woke! Checking streams...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment