Skip to content

Commit

Permalink
docs: updated various code examples and updated docs detailing EventS…
Browse files Browse the repository at this point in the history
…ub in readme and index
  • Loading branch information
Teekeks committed Sep 4, 2023
1 parent 07a945b commit e8c9b9c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 142 deletions.
89 changes: 15 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ You can set that [here in your twitch dev dashboard](https://dev.twitch.tv/conso
```python
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope
from twitchAPI.type import AuthScope

twitch = await Twitch('my_app_id', 'my_app_secret')

Expand Down Expand Up @@ -109,6 +109,17 @@ twitch.app_auth_refresh_callback = app_refresh
twitch.user_auth_refresh_callback = user_refresh
```

## EventSub

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](https://pytwitchapi.readthedocs.io/en/stable/modules/twitchAPI.eventsub.html)



## PubSub

Expand All @@ -120,7 +131,7 @@ A more detailed documentation can be found [here on readthedocs](https://pytwitc
from twitchAPI.pubsub import PubSub
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
from twitchAPI.types import AuthScope
from twitchAPI.type import AuthScope
from twitchAPI.oauth import UserAuthenticator
import asyncio
from pprint import pprint
Expand Down Expand Up @@ -159,76 +170,6 @@ async def run_example():
asyncio.run(run_example())
```


## EventSub

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.

A more detailed documentation, including the setup requirements can be found [here on readthedocs](https://pytwitchapi.readthedocs.io/en/stable/modules/twitchAPI.eventsub.html)

### Short code example:

```python
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
from twitchAPI.eventsub import EventSub
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope
import asyncio

TARGET_USERNAME = 'target_username_here'
EVENTSUB_URL = 'https://url.to.your.webhook.com'
APP_ID = 'your_app_id'
APP_SECRET = 'your_app_secret'
TARGET_SCOPES = [AuthScope.MODERATOR_READ_FOLLOWERS]


async def on_follow(data: dict):
# our event happend, lets do things with the data we got!
print(data)


async def eventsub_example():
# create the api instance and get the ID of the target user
twitch = await Twitch(APP_ID, APP_SECRET)
user = await first(twitch.get_users(logins=TARGET_USERNAME))

# the user has to authenticate once using the bot with our intended scope.
# since we do not need the resulting token after this authentication, we just discard the result we get from authenticate()
# Please read up the UserAuthenticator documentation to get a full view of how this process works
auth = UserAuthenticator(twitch, TARGET_SCOPES)
await auth.authenticate()

# basic setup, will run on port 8080 and a reverse proxy takes care of the https and certificate
event_sub = EventSub(EVENTSUB_URL, APP_ID, 8080, twitch)

# unsubscribe from all old events that might still be there
# this will ensure we have a clean slate
await event_sub.unsubscribe_all()
# start the eventsub client
event_sub.start()
# subscribing to the desired eventsub hook for our user
# the given function (in this example on_follow) will be called every time this event is triggered
# the broadcaster is a moderator in their own channel by default so specifying both as the same works in this example
await event_sub.listen_channel_follow_v2(user.id, user.id, on_follow)

# eventsub will run in its own process
# so lets just wait for user input before shutting it all down again
try:
input('press Enter to shut down...')
finally:
# stopping both eventsub as well as gracefully closing the connection to the API
await event_sub.stop()
await twitch.close()
print('done')


# lets run our example
asyncio.run(eventsub_example())
```

## Chat

A simple twitch chat bot.
Expand All @@ -239,9 +180,9 @@ A more detailed documentation can be found [here on readthedocs](https://pytwitc
### Example code for a simple bot

```python
from twitchAPI import Twitch
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope, ChatEvent
from twitchAPI.type import AuthScope, ChatEvent
from twitchAPI.chat import Chat, EventData, ChatMessage, ChatSub, ChatCommand
import asyncio

Expand Down
71 changes: 7 additions & 64 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ You can set that `here in your twitch dev dashboard <https://dev.twitch.tv/conso
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope
from twitchAPI.type import AuthScope
twitch = await Twitch('my_app_id', 'my_app_secret')
Expand Down Expand Up @@ -143,66 +143,9 @@ EventSub

EventSub lets you listen for events that happen on Twitch.

See here for more info: `twitchAPI.eventsub <modules/twitchAPI.eventsub.html>`_
There are multiple EventSub transports available, used for different use cases.

.. code-block:: python
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
from twitchAPI.eventsub import EventSub
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope
import asyncio
TARGET_USERNAME = 'target_username_here'
EVENTSUB_URL = 'https://url.to.your.webhook.com'
APP_ID = 'your_app_id'
APP_SECRET = 'your_app_secret'
TARGET_SCOPES = [AuthScope.MODERATOR_READ_FOLLOWERS]
async def on_follow(data: dict):
# our event happend, lets do things with the data we got!
print(data)
async def eventsub_example():
# create the api instance and get the ID of the target user
twitch = await Twitch(APP_ID, APP_SECRET)
user = await first(twitch.get_users(logins=TARGET_USERNAME))
# the user has to authenticate once using the bot with our intended scope.
# since we do not need the resulting token after this authentication, we just discard the result we get from authenticate()
# Please read up the UserAuthenticator documentation to get a full view of how this process works
auth = UserAuthenticator(twitch, TARGET_SCOPES)
await auth.authenticate()
# basic setup, will run on port 8080 and a reverse proxy takes care of the https and certificate
event_sub = EventSub(EVENTSUB_URL, APP_ID, 8080, twitch)
# unsubscribe from all old events that might still be there
# this will ensure we have a clean slate
await event_sub.unsubscribe_all()
# start the eventsub client
event_sub.start()
# subscribing to the desired eventsub hook for our user
# the given function (in this example on_follow) will be called every time this event is triggered
# the broadcaster is a moderator in their own channel by default so specifying both as the same works in this example
await event_sub.listen_channel_follow_v2(user.id, user.id, on_follow)
# eventsub will run in its own process
# so lets just wait for user input before shutting it all down again
try:
input('press Enter to shut down...')
finally:
# stopping both eventsub as well as gracefully closing the connection to the API
await event_sub.stop()
await twitch.close()
print('done')
# lets run our example
asyncio.run(eventsub_example())
See here for more info about EventSub in general and the different Transports, including code examples: :doc:`/modules/twitchAPI.eventsub`

PubSub
------
Expand All @@ -216,7 +159,7 @@ See here for more info: `twitchAPI.pubsub <modules/twitchAPI.pubsub.html>`_
from twitchAPI.pubsub import PubSub
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
from twitchAPI.types import AuthScope
from twitchAPI.type import AuthScope
from twitchAPI.oauth import UserAuthenticator
import asyncio
from pprint import pprint
Expand Down Expand Up @@ -260,13 +203,13 @@ Chat
A simple twitch chat bot.
Chat bots can join channels, listen to chat and reply to messages, commands, subscriptions and many more.

See here for more info: `twitchAPI.chat <modules/twitchAPI.chat.html>`_
See here for more info: :doc:`/modules/twitchAPI.chat`

.. code-block:: python
from twitchAPI import Twitch
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope, ChatEvent
from twitchAPI.type import AuthScope, ChatEvent
from twitchAPI.chat import Chat, EventData, ChatMessage, ChatSub, ChatCommand
import asyncio
Expand Down
4 changes: 2 additions & 2 deletions twitchAPI/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ async def on_ready(cmd: EventData):
.. code-block:: python
from twitchAPI import Twitch
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope, ChatEvent
from twitchAPI.type import AuthScope, ChatEvent
from twitchAPI.chat import Chat, EventData, ChatMessage, ChatSub, ChatCommand
import asyncio
Expand Down
2 changes: 1 addition & 1 deletion twitchAPI/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope
from twitchAPI.type import AuthScope
twitch = await Twitch('my_app_id', 'my_app_secret')
Expand Down
2 changes: 1 addition & 1 deletion twitchAPI/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from twitchAPI.pubsub import PubSub
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
from twitchAPI.types import AuthScope
from twitchAPI.type import AuthScope
from twitchAPI.oauth import UserAuthenticator
import asyncio
from pprint import pprint
Expand Down

0 comments on commit e8c9b9c

Please sign in to comment.