Skip to content

Instantly share code, notes, and snippets.

@Lokno
Created February 22, 2019 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lokno/54bceab984e6f152c335b52307276e19 to your computer and use it in GitHub Desktop.
Save Lokno/54bceab984e6f152c335b52307276e19 to your computer and use it in GitHub Desktop.
Simple slack bot that polls twitch and announces when users are active
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