mIRC Home    About    Download    Register    News    Help

Print Thread
Twitch bot which post commands to hastebin #250429 14/01/15 11:05 AM
Joined: Jan 2015
Posts: 8
A
aborge Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
A
Joined: Jan 2015
Posts: 8
Hello!
I have made a twitchbot that you can add/delete custom commands to stored in a file. Instead of posting all commands in the twitch chat I want it to post them to hastebin. I have managed to do so, and I save the link to a global variable. The problem is however that my bot post the previous link to twitchchat instead of the newest. after my bot posts in twitchchat, i check my global variables and %hastelink is the newest link. Somehow my bot posts the old link before it gets updated.
Here is some of my code:

Code:
;## the http alias is working as intended and post the variable,  then find the key using socketread and sets the link to a global variable.
/http post http://hastebin.com/documents/ %allcoms
msg $chan Volcania Custom commands: %hastelink


I can add the socketread too so you see how i set the global variable:
Code:
on *:sockread:http.*:{
  var %http.read
  sockread %http.read
  tokenize 32 %http.read
  if (!$1) return
  .signal -n http $1-
  if (HTTP/1.* 200 OK iswm $1-) echo $color(info) -s HTTP $hget(http,method) request to $hget(http,url) successful!

  while ($sockbr) {
    if (*{"key":* iswm %http.read) {
      unset %hastelink
      unset %hastekey
      set %hastekey $gettok($gettok(%http.read,4,34),1,44)
      set %hastelink http://hastebin.com/ $+ %hastekey
      echo -s Key : %hastelink
    }
    sockread %http.read
  }
}


Re: Twitch bot which post commands to hastebin [Re: aborge] #250435 14/01/15 08:53 PM
Joined: Jun 2014
Posts: 248
B
Belhifet Offline
Fjord artisan
Offline
Fjord artisan
B
Joined: Jun 2014
Posts: 248
You need to post the whole script if you want it troubleshooted.

Re: Twitch bot which post commands to hastebin [Re: Belhifet] #250438 14/01/15 09:16 PM
Joined: Jan 2004
Posts: 1,274
L
Loki12583 Offline
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,274
From his first code I'll assume he's calling the http alias and then immediately trying to message the channel. It doesn't work like that, none of the socket events have run at that point.

Re: Twitch bot which post commands to hastebin [Re: Loki12583] #250440 14/01/15 11:41 PM
Joined: Jan 2015
Posts: 8
A
aborge Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
A
Joined: Jan 2015
Posts: 8
Hey! And thank you for your reply.
Alright, i didnt know that the socket events have not run at that point. I can post the whole code to you.

Yes i am calling the http alias and message the channel after that. Can you tell me another way to first run the alias which starts all the socket events, and then message the channel after the global variable %hastelink is updated with the new link?


Here is all of the code for the http post script file. In another file I have the "on text" event that is calling the http alias as you see in my first post
Code:
Code:
on *:sockopen:http.*:{
  if (!$window(@http)) window -ek0 @http
  if ($sockerr) || (!$sock($sockname)) return
  if (/* !iswm $http.parse($hget(http,url)).path) { var %path / $+ $http.parse($hget(http,url)).path }
  else { var %path $http.parse($hget(http,url)).path }
  sockwrite -n $sockname $hget(http,method) %path HTTP/1.1
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname Content-Type: application/x-www-form-urlencoded
  sockwrite -n $sockname User-Agent: mIRC $version
  sockwrite -n $sockname Host: $http.parse($hget(http,url)).host
  sockwrite -n $sockname Content-Length: $len( $hget(http,data) )
  sockwrite -n $sockname Connection: Keep-Alive
  sockwrite -n $sockname Cache-Control: no-cache
  sockwrite -n $sockname
  if ($hget(http,method) == post) { sockwrite -n $sockname $hget(http,data) }
  sockwrite -n $sockname $crlf $+ $crlf
}
on *:sockread:http.*:{
  var %http.read
  sockread %http.read
  tokenize 32 %http.read
  if (!$1) return
  .signal -n http $1-
  if (HTTP/1.* 200 OK iswm $1-) echo $color(info) -s HTTP $hget(http,method) request to $hget(http,url) successful!

  while ($sockbr) {
    if (*{"key":* iswm %http.read) {
      unset %hastelink
      var %hastekey = $gettok($gettok(%http.read,4,34),1,44)
      set %hastelink http://hastebin.com/ $+ %hastekey
      echo -s Key : %hastelink
    }
    sockread %http.read
  }
}
on 1:sockclose:http.*:{
  .signal -n http Connection to $sock($sockname).ip has been closed remotely
}

alias http {
  if (!$regex($1-,(post|get) http://(.+)/(.*))) {
    echo $color(info) -at Usage: /http <post|get> <url> [data]
    echo $color(info) -at url has to match http://*/* and must not contain spaces!
    return
  }
  hadd -m http url $2
  hadd -m http method $upper($1)
  if ($3) hadd http data $3-
  if ($sock($+(http.,$1))) .sockclose $+(http.,$1)
  sockopen $+(http.,$1) $http.parse($2).host 80
}
alias http.parse {
  if ($regex($1-,^http://([^/]+)(/.*))) {
    if ($prop == host) return $regml(1)
    if ($prop == path) return $iif(($regml(2)),$regml(2),/)
  }
}
on *:SIGNAL:http:{

  /*
  if (!$window(@http)) window -ek0 @http
  echo @http < $1-
  */
  /*
  write http.log $1-
  */
}


Last edited by aborge; 14/01/15 11:44 PM.
Re: Twitch bot which post commands to hastebin [Re: Loki12583] #250486 17/01/15 01:59 PM
Joined: Jan 2015
Posts: 8
A
aborge Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
A
Joined: Jan 2015
Posts: 8
Originally Posted By: Loki12583
From his first code I'll assume he's calling the http alias and then immediately trying to message the channel. It doesn't work like that, none of the socket events have run at that point.


Hey, dont know if u saw my new message! Do you have any solution?

Re: Twitch bot which post commands to hastebin [Re: aborge] #250487 17/01/15 02:47 PM
Joined: Sep 2014
Posts: 259
S
Sakana Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Sep 2014
Posts: 259
Use something like .timer 1 1 msg #

Or if that doens't work, split the alias in two aliases and use a timer before running the 2nd part

Re: Twitch bot which post commands to hastebin [Re: aborge] #250492 17/01/15 08:40 PM
Joined: Jun 2014
Posts: 248
B
Belhifet Offline
Fjord artisan
Offline
Fjord artisan
B
Joined: Jun 2014
Posts: 248
Send the message after you set the global variable?

Re: Twitch bot which post commands to hastebin [Re: Belhifet] #250503 18/01/15 01:55 PM
Joined: Jan 2015
Posts: 8
A
aborge Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
A
Joined: Jan 2015
Posts: 8
Originally Posted By: Belhifet
Send the message after you set the global variable?


thats what i do but it doesnt work

Re: Twitch bot which post commands to hastebin [Re: Sakana] #250504 18/01/15 01:59 PM
Joined: Jan 2015
Posts: 8
A
aborge Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
A
Joined: Jan 2015
Posts: 8
Originally Posted By: Sakana
Use something like .timer 1 1 msg #

Or if that doens't work, split the alias in two aliases and use a timer before running the 2nd part


I tried the timer, but that didnt work.
what do you mean by the splitting?
what part of the alias should I put in each other alias? Do you mean the http alias, then a timer, then the message alias where it only sends the message to chat?

Re: Twitch bot which post commands to hastebin [Re: aborge] #250506 18/01/15 02:44 PM
Joined: Sep 2014
Posts: 259
S
Sakana Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Sep 2014
Posts: 259
Do the msg # under the sockread event? Otherwise I think you need to do something like

Code:
alias test { 
http
.timer 1 2 test2
}

alias test2 {

msg #

}

Last edited by Sakana; 18/01/15 02:46 PM.
Re: Twitch bot which post commands to hastebin [Re: Sakana] #250507 18/01/15 08:04 PM
Joined: Jan 2015
Posts: 8
A
aborge Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
A
Joined: Jan 2015
Posts: 8
Tried that thing, but didnt work

However I got it to work by putting the msg into the socketread and made a global variable for the current channel that I can put in there instead of msg $chan or msg #

Thank you