PubSub accessible from Python/Discord.Py bot?

I’m fairly new to the whole web dev scene and even python itself. I cant find any examples so I’m assuming the answer is no, but I just wanted to bury this so I can move on and do what I need to do.

Is it possible? I see the example code is for JavaScript. But can the PubSub be used with python?

Sure PubSub is just a socket protocol to deliver data to you.

Since it’s Websockets led, it’s super easy to throw a javascript example up. Just like how Twitch Chat is available over A websocket, so if you can speak websocket you can use PubSub.

Some of the existing Twitch Python libraries probably have support, so use one of those or a Python WebSocket client

Thanks for the reply.

But yeahhhhh. If anyone out there reads this and they some example code for python on where to get started with this, do share :pray:

have been completely SOL finding examples and the docs are just not detailed enough for my noob self

Hey I’m looking the same as you to access pubsub using python. I just got this code running, maybe this can help you with the basics. Eventually we need to implement a heartbeat to maintain the connection.

channel-points-channel-v1.125627324 is my id, you need to change the value with your own id. You should check the OAuth guide (https://dev.twitch.tv/docs/authentication/getting-tokens-oauth).

Also, one thing to notice is that when you request your OAuth you need to request the specifics scopes for the topic you want to listen (https://dev.twitch.tv/docs/pubsub#topics)

import websocket
try:
import thread
except ImportError:
import _thread as thread
import time

oauth = XXXXXXXXXXX #Your Oauth Code

listenRedeems = {“type”: ‘LISTEN’,‘nonce’: ‘44h1k13746815ab1r2’,‘data’:{‘topics’: [‘channel-points-channel-v1.125627324’],‘auth_token’: ‘%s’ % (oauth))}}

def on_message(ws, message):
print(message)

def on_error(ws, error):
print(error)

def on_close(ws):
print("### closed ###")

def on_open(ws):
def run(*args):
ws.send(json.dumps(listenRedeems))

thread.start_new_thread(run, ())

if __ name __ == “__ main __”:
websocket.enableTrace(True)
ws = websocket.WebSocketApp(“wss://pubsub-edge.twitch.tv”,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.