- Notifications
You must be signed in to change notification settings - Fork40
A Python 3.7 compatible implementation of the Twitch API, EventSub and Chat
License
Teekeks/pyTwitchAPI
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a full implementation of the Twitch Helix API, EventSub and Chat in python 3.7+.
Install using pip:
pip install twitchAPI
A full API documentation can be foundon readthedocs.org.
For support please join theTwitch API discord server
Setting up an Instance of the Twitch API and get your User ID:
fromtwitchAPI.twitchimportTwitchfromtwitchAPI.helperimportfirstimportasyncioasyncdeftwitch_example():# initialize the twitch instance, this will by default also create a app authentication for youtwitch=awaitTwitch('app_id','app_secret')# call the API for the data of your twitch user# this returns a async generator that can be used to iterate over all results# but we are just interested in the first result# using the first helper makes this easy.user=awaitfirst(twitch.get_users(logins='your_twitch_user'))# print the ID of your user or do whatever else you want with itprint(user.id)# run this exampleasyncio.run(twitch_example())
The Twitch API knows 2 different authentications. App and User Authentication.Which one you need (or if one at all) depends on what calls you want to use.
It's always good to get at least App authentication even for calls where you don't need it since the rate limits are way better for authenticated calls.
Please read the docs for more details and examples on how to set and use Authentication!
App authentication is super simple, just do the following:
fromtwitchAPI.twitchimportTwitchtwitch=awaitTwitch('my_app_id','my_app_secret')
To get a user auth token, the user has to explicitly click "Authorize" on the twitch website. You can use various online services to generate a token or use my build in Authenticator.For my Authenticator you have to add the following URL as a "OAuth Redirect URL":http://localhost:17563You can set thathere in your twitch dev dashboard.
fromtwitchAPI.twitchimportTwitchfromtwitchAPI.oauthimportUserAuthenticatorfromtwitchAPI.typeimportAuthScopetwitch=awaitTwitch('my_app_id','my_app_secret')target_scope= [AuthScope.BITS_READ]auth=UserAuthenticator(twitch,target_scope,force_verify=False)# this will open your default browser and prompt you with the twitch verification websitetoken,refresh_token=awaitauth.authenticate()# add User authenticationawaittwitch.set_user_authentication(token,target_scope,refresh_token)
You can reuse this token and use the refresh_token to renew it:
fromtwitchAPI.oauthimportrefresh_access_tokennew_token,new_refresh_token=awaitrefresh_access_token('refresh_token','client_id','client_secret')
Optionally you can set a callback for both user access token refresh and app access token refresh.
fromtwitchAPI.twitchimportTwitchasyncdefuser_refresh(token:str,refresh_token:str):print(f'my new user token is:{token}')asyncdefapp_refresh(token:str):print(f'my new app token is:{token}')twitch=awaitTwitch('my_app_id','my_app_secret')twitch.app_auth_refresh_callback=app_refreshtwitch.user_auth_refresh_callback=user_refresh
EventSub lets you listen for events that happen on Twitch.
The EventSub client runs in its own thread, calling the given callback function whenever an event happens.
There are multiple EventSub transports available, used for different use cases.
See here for more info about EventSub in general and the different Transports, including code examples:on readthedocs
A simple twitch chat bot.Chat bots can join channels, listen to chat and reply to messages, commands, subscriptions and many more.
A more detailed documentation can be foundhere on readthedocs
fromtwitchAPI.twitchimportTwitchfromtwitchAPI.oauthimportUserAuthenticatorfromtwitchAPI.typeimportAuthScope,ChatEventfromtwitchAPI.chatimportChat,EventData,ChatMessage,ChatSub,ChatCommandimportasyncioAPP_ID='my_app_id'APP_SECRET='my_app_secret'USER_SCOPE= [AuthScope.CHAT_READ,AuthScope.CHAT_EDIT]TARGET_CHANNEL='teekeks42'# this will be called when the event READY is triggered, which will be on bot startasyncdefon_ready(ready_event:EventData):print('Bot is ready for work, joining channels')# join our target channel, if you want to join multiple, either call join for each individually# or even better pass a list of channels as the argumentawaitready_event.chat.join_room(TARGET_CHANNEL)# you can do other bot initialization things in here# this will be called whenever a message in a channel was send by either the bot OR another userasyncdefon_message(msg:ChatMessage):print(f'in{msg.room.name},{msg.user.name} said:{msg.text}')# this will be called whenever someone subscribes to a channelasyncdefon_sub(sub:ChatSub):print(f'New subscription in{sub.room.name}:\\n'f' Type:{sub.sub_plan}\\n'f' Message:{sub.sub_message}')# this will be called whenever the !reply command is issuedasyncdeftest_command(cmd:ChatCommand):iflen(cmd.parameter)==0:awaitcmd.reply('you did not tell me what to reply with')else:awaitcmd.reply(f'{cmd.user.name}:{cmd.parameter}')# this is where we set up the botasyncdefrun():# set up twitch api instance and add user authentication with some scopestwitch=awaitTwitch(APP_ID,APP_SECRET)auth=UserAuthenticator(twitch,USER_SCOPE)token,refresh_token=awaitauth.authenticate()awaittwitch.set_user_authentication(token,USER_SCOPE,refresh_token)# create chat instancechat=awaitChat(twitch)# register the handlers for the events you want# listen to when the bot is done starting up and ready to join channelschat.register_event(ChatEvent.READY,on_ready)# listen to chat messageschat.register_event(ChatEvent.MESSAGE,on_message)# listen to channel subscriptionschat.register_event(ChatEvent.SUB,on_sub)# there are more events, you can view them all in this documentation# you can directly register commands and their handlers, this will register the !reply commandchat.register_command('reply',test_command)# we are done with our setup, lets start this bot up!chat.start()# lets run till we press enter in the consoletry:input('press ENTER to stop\n')finally:# now we can close the chat bot and the twitch api clientchat.stop()awaittwitch.close()# lets run our setupasyncio.run(run())
About
A Python 3.7 compatible implementation of the Twitch API, EventSub and Chat
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.