mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Aug 2013
Posts: 5
U
uhDetox Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
U
Joined: Aug 2013
Posts: 5
Hello everyone, I am currently developing a Twitch.tv moderation bot and the last step I have is a !topic script. I have scoured across the internet and tried to create the script myself but it never works.

So basically what I want the bot to do is when i type !topic and then a message after that it sets the message as the topic, and then when i type !topic without any text after it, it says the topic message in my channel. I have seen this on almost every moderation bot so I know it can be done. Any help you guys can provide is greatly appreciated. Thanks.

Joined: Jan 2014
Posts: 55
J
Babel fish
Offline
Babel fish
J
Joined: Jan 2014
Posts: 55

I have never used Twitch but heres a code that works on a IRC network via mIRC

Place into remotes and do

!topic message.... E.g. !topic This is a test channel
!showtopic - Will show the topic for that channel

Must have op level to use the command.

Will work for as many channels you want but it you want to make it to work in on channel where it says on *:TEXT:*:#: { change it to on *:TEXT:*:#CHANNEL NAME: {


Code:

on *:TEXT:*:#: { 
  if ($strip($1) == !topic) {
    if ($nick !isop $chan) { .notice $nick Sorry but you must be opped to use this command! | halt }
    if ($2 == $null) { .notice $nick Incorrect Usage! To use this command do !topic topic message. Example !topic Welcome to #Test Channel |  halt }
    else {
      .topic $chan $2-
      .writeini -n topic.ini $chan Topic $2-
    }
  }
  if ($strip($1) == !showtopic) {
    if ($nick !isop $chan) { .notice $nick Sorry but you must be opped to use this command! | halt }
    if ($readini(topic.ini, $chan, Topic) !== $null)  .notice $nick The topic for $chan is $readini(topic.ini,$chan, Topic) 

  }


Regards

JayStew
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Code:
on @*:text:!topic*:#:{
  if ($2 != $null) {
    if ($nick isop #) topic # $2-
  }
  else {
    msg # $chan(#).topic
  }
}

Joined: Apr 2013
Posts: 27
R
Ameglian cow
Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
The twitch API shows this:
Code:
curl -H 'Accept: application/vnd.twitchtv.v2+json' -H 'Authorization: OAuth <access_token>' \
-d "channel[status]=Playing+cool+new+game!&channel[game]=Diablo" \
-X PUT https://api.twitch.tv/kraken/channels/test_user1

It is for java but you need to be the streamer name in mirc in order to do this so a differently named bot cannot make the name change unless the bot name is your twitch stream account

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
That's not related to java, it's just the curl syntax.

In order to do this you need to register you application here: http://www.twitch.tv/settings/connections, for your redirect url use http://localhost, you will need your client id to request an oath token (which is what your script uses).

To obtain the oath token use this link, make sure to replace the client id from the last step. You will be directed to a broken link, copy the access_token value from the address bar.
Quote:
https://api.twitch.tv/kraken/oauth2/authorize?response_type=token&client_id=your_client_ID&redirect_uri=http://localhost/&scope=channel_read+channel_editor


This $json script is required for use: http://www.mircscripts.org/comments.php?cid=4407

Replace the user_name and access_token in the beginning of the script. You may consider reading this token from a separate file so that it's not contained within the script.

Code:
alias -l twitch.token {
  if ($1 == user_name) return access_token
}

on @*:text:!topic*:#:{
  if ($2 != $null) {
    if ($nick isop #) twitch.topic # $2-
  }
  else {
    twitch.topic #
  }
}

alias twitch.topic {
  var %chan = $1, %topic = $2-
  var %sockname = twitch.topic. $+ $ticks
  hfree -w %sockname | hmake %sockname
  hadd %sockname headerfile %sockname $+ .header.txt
  hadd %sockname datafile %sockname $+ .data.txt
  hadd %sockname params %chan
  hadd %sockname oath.token $twitch.token($mid(%chan,2-))
  hadd %sockname request /kraken/channels/ $+ $mid(%chan,2-)
  if (%topic != $null) {
    hadd %sockname method PUT
    hadd %sockname data channel[status]= $+ $urlencode(%topic)
    hadd %sockname signal topic.set
  }
  else {
    hadd %sockname method GET
    hadd %sockname signal topic.get
  }
  sockopen -e %sockname api.twitch.tv 443
}

on *:signal:topic.get:{
  var %err = $1, %sockname = $2, %header = $3, %data = $4, %chan = $5

  if (* 200 OK iswm $read(%header,1)) msg %chan Status: $json(%data,status)
  else msg %chan Could not get status.

  hfree -w %sockname
  .remove %header | .remove %data
}

on *:signal:topic.set:{
  var %err = $1, %sockname = $2, %header = $3, %data = $4, %chan = $5

  if (* 200 OK iswm $read(%header,1)) msg %chan Status set: $json(%data,status)
  else msg %chan Could not set status.

  hfree -w %sockname
  .remove %header | .remove %data
}

on *:sockopen:twitch.*:{
  var %a = sockwrite -n $sockname
  %a $hget($sockname,method) $hget($sockname,request) HTTP/1.1
  %a Host: api.twitch.tv
  %a Connection: close
  %a Accept: application/vnd.twitchtv.v2+json
  if ($hget($sockname,oath.token) != $null) %a Authorization: OAuth $v1
  if ($hget($sockname,data) != $null) {
    %a Content-Type: application/x-www-form-urlencoded
    %a Content-Length: $len($v1)
  }
  %a $+($crlf,$hget($sockname,data))
}

on *:sockread:twitch.*:{
  var %header
  var %headerfile = $hget($sockname,headerfile)
  var %datafile = $hget($sockname,datafile)

  if (!$hget($sockname,header.complete)) {
    sockread %header
    while (%header != $null) {
      write %headerfile %header
      sockread %header
    }
    if ($sockbr) hadd $sockname header.complete $true
  }
  if ($hget($sockname,header.complete)) {
    sockread &read
    while ($sockbr) {
      bwrite %datafile -1 -1 &read
      sockread &read
    }
  }
}

on *:sockclose:twitch.*:{
  var %header = $hget($sockname,headerfile)
  var %data = $hget($sockname,datafile)
  var %signal = $hget($sockname,signal)
  var %params = $hget($sockname,params)

  if (%signal) .signal %signal 0 $sockname %header %data %params
  else {
    hfree -w $sockname
    .remove %header | .remove %data
  }
}

alias urlencode return $regsubex($1-,/([^A-Z0-9])/gi,$+(%,$base($asc(\1),10,16)))


And I don't even use this site

Joined: Feb 2014
Posts: 3
T
Self-satisified door
Offline
Self-satisified door
T
Joined: Feb 2014
Posts: 3
Hiya Loki and others,
I've been reading lots of your useful posts across this forum and am well on the way with my bot. My bot can now check for new followers, check if the channel is online or not and a few other bits and bobs.

I'm a massive mIRC fan and have been using it since 1999!

Unfortunately I'm struggling with this one. I'd like to use the bot to update the channels game and topic. This is the first time I'm using the bot to SEND information rather than just retrieve it.

I have registered my application - got my oath token - activated the application/bot to work on my channel - installed the script into my remote and run it a bunch of times.

Each time I'm getting the error - Could not set status.

I can see this is a generic error for 'this didn't work' but I cannot see how or why. It is possible I have made a mistake in the previous steps - but I'm not sure where to turn next. I can provide more information if needed- but the code is essentially what Loki has written above. The only possible difference is that my bot username is not the channelname - and I think I've tweaked the code to compensate for that - but that could be my problem?

I understand totally that this is a mirc board not a twitch board. I am a big mirc fan and I am so pleased that I can use my old mirc skills again in combination with my new twitch skills.

Thank you in advance for any help.

Adi

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
You don't need to be the owner of the channel as long as you requested the oath token with &scope=channel_read+channel_editor.

To troubleshoot, comment out the last line of the topic.get/set signals: ";.remove %header | .remove %data" so that you can open up the header and data files to see what the problem is. They're located in your $mircdir, type "/run ." to open it.

Looking back at the code, the "user_name" in the twitch.token alias may be a little confusing, this really refers to the channel name. So if you have an oath token for "#twitch", you would put "twitch" there, following this same form you can put the oath token for many different channels.

Joined: Feb 2014
Posts: 3
T
Self-satisified door
Offline
Self-satisified door
T
Joined: Feb 2014
Posts: 3
fantastic - I got it working. Somehow I had the wrong oath code in place - It may have been I was logged in (on the browser) as the wrong nickname. I also may have had the wrong parameters when I originally activated it. Works great now - I also used an extension on the code to update the currently playing game as well which means I can now almost completely control the stream from inside mirc.

One more question if I may. I'd also like to be able to start Ads (and as of different lengths) from within mirc. I have the knowledge to copy and adapt your existing code to put a seperate command in - but I'm not exactly sure what that command should be.

I found this in the twitch api document you linked in another thread:

Quote:
POST /channels/:channel/commercial

Start commercial on channel.

Authenticated, required scope: channel_commercial
Parameters
Name Required? Type Description
length optional integer Length of commercial break in seconds. Default value is 30. Valid values are 30, 60, or 90. You may only trigger a commercial longer than 30 seconds once every 8 minutes.
Example Request

curl -H 'Accept: application/vnd.twitchtv.v2+json' -H 'Authorization: OAuth <access_token>' \
-d "length=30" -X POST https://api.twitch.tv/kraken/channels/test_user1/commercial


..But the format of that is different to what is in your existing topic code. I assume I need to swap out your PUT code for a POST code, but I don't know what that should be.

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
It looks like you need to change the method, data, and request variables. If you don't care what the response is (to display an error code or something), then you don't even need to define a signal for this command. The length is optional, I'm not sure if a Content-Length=0 is required in this instance. My guess is probably not, so I don't think changes to the sockets are necessary.

Code:
on @*:text:!commercial*:#:{
  if ($nick isop #) twitch.commercial # $iif($2 isnum,$2)
}

alias twitch.commercial {
  var %chan = $1, %length = $2
  ...
  hadd %sockname request /kraken/channels/ $+ $mid(%chan,2-) $+ /commercial
  hadd %sockname method POST
  if (%length) hadd %sockname data length= $+ %length
  ...
}


You will need to request a new auth code with scope channel_commercial in addition to the other scopes.

Edit: Going to add the new scope myself for testing, I noticed that in my first post I said to use http://localhost as the redirect url, but then in the link to get the token the url contains http://localhost/ - this trailing slash matters and must match in both places.

Last edited by Loki12583; 17/02/14 03:05 PM.
Joined: Dec 2013
Posts: 779
N
Hoopy frood
Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Am I supposed to be online on the main twitch account while requesting an oauth or on the bot account? Tried both and failed so far. To clarify, my main is called "nillens" and the bot is "nillensbot".

I got my clientID, replaced that in the quoted url, got my auth and replaced the info in the code you presented.

So, user_name now says nillens and access_token now says what was given to me in the url.

Without changing any of the code, like Adipose seems to have done. Could you inform me of what I did wrong? Was I supposed to get ClientID from the "nillensbot" account instead?

I'm obviously getting the
Code:
    if (* 200 OK iswm $read(%header,1)) msg %chan Status set: $json(%data,status)
  else msg %chan Could not set status. 
error.


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
I believe the app has to be registered under the bot, and then you authorize it from your main account.

Last edited by Loki12583; 19/02/14 01:11 AM.
Joined: Feb 2014
Posts: 3
T
Self-satisified door
Offline
Self-satisified door
T
Joined: Feb 2014
Posts: 3
Thanks Loki

Joined: Dec 2013
Posts: 779
N
Hoopy frood
Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Gotcha! I'll give it a try and hopefully get it right this time smile
Thanks for the help good sir!


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Joined: Mar 2014
Posts: 65
E
Babel fish
Offline
Babel fish
E
Joined: Mar 2014
Posts: 65
Hey, I know this post is years old but that link for the $json script is no longer working, does anyone have this script still? (Just the $json one)


I do things with stuff that makes other things do stuff.
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
Check the link in my signature for JSONForMirc; though mine is a little different than the one used above

Last edited by FroggieDaFrog; 16/12/15 11:30 PM.

I am SReject
My Stuff
Joined: Mar 2014
Posts: 65
E
Babel fish
Offline
Babel fish
E
Joined: Mar 2014
Posts: 65
Sorry, haven't touched Mirc in a while. Not to sound like a noob (Even read everything you said) but how would I use this in a script, a really really simple one like this.

Code:
on *:TEXT:!title *:#: {
var %title = $json[http://api.twitch.tv/channels/twitch]
msg # The stream title is " $+ %title $+ "
}


I do things with stuff that makes other things do stuff.
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
Do you want to set the stream title, just retrieve the stream title, or both?

----

Making use of my JSON Parser, I've wrote my own version of the above:

http://pastebin.com/ixUxtBET

Before using, you'll need to create an OAUTH token that has channel_edit privileges then replace "OAUTH_TOKEN_HERE" with the valid OAUTH token

Last edited by FroggieDaFrog; 17/12/15 12:29 AM.

I am SReject
My Stuff
Joined: Mar 2014
Posts: 65
E
Babel fish
Offline
Babel fish
E
Joined: Mar 2014
Posts: 65
I just wanted to see the title, but I can extrapolate how to do that from what you have written. Thank you laugh


I do things with stuff that makes other things do stuff.
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
The !game and !title commands(without parameters) do not need the OAUTH token. Just when you want to set either of them


I am SReject
My Stuff
Joined: Apr 2014
Posts: 24
T
Ameglian cow
Offline
Ameglian cow
T
Joined: Apr 2014
Posts: 24
I keep getting this error url when I try to obtain a oath token. I registered the application under my bot account, I put in the client id. Then I try to obtain the oauth token with my main account. Does anyone know what I should do?


Link Copied to Clipboard