twitch.set_user_authentication( token, [AuthScope.USER_READ_EMAIL], refresh_token ) # setting the user authentication so any api call will also use it # setting up the Webhook itself # Please note that the first parameter is the domain your webhook is reachable from the outside, the last parameter # is the port that the Webhook should use hook = TwitchWebHook("https://my.cool.ip:443", 'your app id', 8080) hook.authenticate( twitch ) # this will use the highest authentication set, which is the user authentication. # some hooks don't require any authentication, which would remove the requirement to set up a https reverse proxy # if you don't require authentication just dont call authenticate() hook.start() # the hook has to run before you subscribe to any events since the twitch api will do a handshake this this webhook as soon as you subscribe success, uuid_stream = hook.subscribe_stream_changed('your user id', callback_stream_changed) print(f'was subscription successfull?: {success}') success, uuid_user = hook.subscribe_user_changed('your user id', callback_user_changed) print(f'was subscription successfull?: {success}') # now we are fully set up and listening to our webhooks, lets wait for a user imput to stop again: input('Press enter to stop...') # lets unsubscribe again success = hook.unsubscribe(uuid_user) print(f'was unsubscription successfull?: {success}') # since hook.unsubscribe_on_stop is true, we dont need to unsubscribe manually, so lets just stop hook.stop()
class DiscordTwitchWebhook(): def __init__(self, twitch_appid, twitch_secret, callback_url): self.discord_username = "******" self.twitch = Twitch(twitch_appid, twitch_secret) self.callback_url = callback_url self.hook = TwitchWebHook(callback_url, twitch_appid, 8080) self.authenticated = False self.subscriptions = [] def authenticate(self): self.authenticated = False try: self.twitch.authenticate_app([]) self.hook.authenticate(self.twitch) except TwitchAuthorizationException: print("Twitch authentication failed") except RuntimeError: print("Webhook must be https") else: self.authenticated = True return self.authenticated def subscribe_user(self, user: DiscordTwitchCallback): if not self.authenticated: raise Exception #TODO handle more exceptions user_data = self.twitch.get_users(logins=[user.username]) user.data = user_data['data'][0] user.id = user.data['id'] if user not in self.subscriptions: ret, user.uuid = self.hook.subscribe_stream_changed( user.id, self.callback_stream_changed) if ret: self.subscriptions.append(user) else: print(f"Failed to subscribe to {user.username}") def unsubscribe_user(self, user: DiscordTwitchCallback): #TODO return def start(self): self.hook.start() def stop(self): self.hook.unsubscribe_all(self.twitch) self.hook.stop() def callback_stream_changed(self, uuid, twdata): print('Callback for UUID ' + str(uuid), twdata) user = next((user for user in self.subscriptions if user.uuid == uuid), None) if user == None: print("Callback failed") return if twdata['type'] == 'live': emb = self.create_embed(twdata) user.run_callback(emb) def create_embed(self, twdata): return Embed(title=f"{twdata['user_name']}", description=f"{twdata['user_name']} is streaming {twdata['game_name']}! Get in here!", color=6570404, url=f"https://twitch.tv/{twdata['user_name']}") \ .set_image(url=twdata['thumbnail_url'].format(width="1280", height="720"))