简体   繁体   中英

How do I receive Twitch.tv Webhooks in Python?

I'm trying to make a simple Discord Bot that posts when a streamer goes live but I'm not sure how to use the Twitch webhooks without having a website? Is it possible to handle it all in a Python script?

Documentation here

webhookurl = "https://api.twitch.tv/helix/webhooks/hub/"
payload = {"hub.mode":"subscribe",
    "hub.topic":"https://api.twitch.tv/helix/users/streams?user_id=27942990",
    "hub.callback":url,
    "hub.lease_seconds":"0" 
}
header = {"Content-Type":"application/json", "Client-ID": clientid}

req = requests.post(url, headers=header, data = payload)
resp = req.json()

print(resp)

Not sure what to put as the callback URL since I don't have a website to respond.

I'm using Python 3.6

Side note: I can find out if a channel is live but Twitch recommends the webhooks for checking when a stream goes live and I just want to try learn this.

Found out how to sort it(kind of) and thought I'd reply here.

I have to run a webserver to accept the data so this is what I have. In the POST function the web.data is the response which says if stream is live/not etc

import web

urls = ('/.*', 'hooks')

app = web.application(urls, globals())


class hooks:

    def POST(self):
        data = web.data()
        print("")
        print('DATA RECEIVED:')
        print(data)
        print("")

        return 'OK'

    def GET(self):
        try:
            data = web.input()
            data = data['hub.challenge']
            print("Hub challenge: ", data)
            return data
        except KeyError:
            return web.BadRequest


if __name__ == '__main__':
    app.run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM