Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import websocket
- import requests
- def on_error(ws, error):
- print(f"Error: {error}")
- def on_close(ws, a, b):
- print("Closed the WebSocket connection")
- def on_message(ws, message):
- from main import get_oauth_stuff, s_command_call
- from config import broadcaster_id, target_channel
- import config
- import tts
- import secret
- import utils
- message = json.loads(message)
- if message["metadata"]["message_type"] == "session_welcome":
- session_id = message["payload"]["session"]["id"]
- oauth_stuff = get_oauth_stuff()
- headers = {
- "Authorization": "Bearer %s" % secret.refresh_token,
- "Client-Id": oauth_stuff["client_id"],
- "Content-Type": "application/json",
- }
- data = {
- "type": "channel.follow",
- "version": "beta",
- "condition": {
- "broadcaster_user_id": f"{broadcaster_id}",
- "moderator_user_id": f"{broadcaster_id}"
- },
- "transport": {
- "method": "websocket",
- "session_id": session_id,
- },
- }
- r = requests.post("https://api.twitch.tv/helix/eventsub/subscriptions", headers=headers, json=data)
- assert r.status_code == 202
- if message["metadata"]["message_type"] == "notification":
- if message["metadata"]["subscription_type"] == "channel.follow":
- user = message["payload"]["event"]["user_name"]
- if hasattr(config, "follow_callback"):
- config.follow_callback(user)
- else:
- default_follow_callback()
- def handle():
- ws = websocket.WebSocketApp(
- "wss://eventsub-beta.wss.twitch.tv/ws",
- on_message=on_message,
- on_error=on_error,
- on_close=on_close,
- )
- ws.run_forever()
- def default_follow_callback(user):
- import config
- import tts
- from main import s_command_call
- utils.send_chat_message(f"Thank you for following {user}")
- tts.tts_command(s_command_call(target_channel, f"Thank you for following {user}", False))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement