justintv / Twitch-API

A home for details about our API
www.twitch.tv
1.72k stars 380 forks source link

Possibility to trigger /host via API #454

Closed Kirbo closed 8 years ago

Kirbo commented 8 years ago

Related to: http://blog.twitch.tv/2014/07/host-mode-faq/

I would like to be able to switch the channel i want to host on my own channel via API. Can it be done?

scagood commented 8 years ago

It cannot be done directly in the API but you can put a small script onto a web server that connects to the IRC and performs the host command then disconnect. Unfortunately this is rather resource inefficient but it is the only way I could think of when I came across this problem, also this means you'll have to make calls to another server too.

I can post my version of this in PHP and Node JS tomorrow if you want it or sudo code if you want to come up with your own version.

If anyone else has a better suggestion I'm all ears.

Hope this helps Sca

Kirbo commented 8 years ago

I've been implementing a workaround for this by using Hubot on twitch channel, since i need it to other stuff too, but it feels like this should be possible to do via API so that one shouldn't have to create extra scripts and/or bots or stuff in order to swap host.

Edit: I created a Hubot -bot and downloaded hubot-twitch-adapter and created a script which executes in interval a http request to my API which runs a query to database, if the API returns a channel, the Hubot will say in Twitch channel "/host channel here".

No, this isn't the best way to do this, but I had to create a timetable for our organization where everyone can schedule when one wants to stream stuff (which our Twitch channel will then host) and to automate our channel to switch the host on specific time. Would rather have done this via Twitch API, if it were possible, but since it isn't, had to do a workaround for this..

Here's the script, if someone wants to know:

room = "#twitch_channel"
host = null
api_url = "http://example.com"

module.exports = (robot) ->
  setInterval () ->
    robot.http( api_url )
      .header('Accept', 'application/json')
      .get() (err, response, body) ->
        caster = JSON.parse body

        if caster.data and host isnt caster.data.channel
          host = "#{caster.data.channel}"
          robot.messageRoom room, "/host #{caster.data.channel}"
        else if !caster.data and host isnt null
          host = null
          robot.messageRoom room, "/unhost"
  , 10 * 1000
DallasNChains commented 8 years ago

There isn't a supported way to do this through the API. We'll document it if it becomes available in the future! :)