This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Not Working 100%

Help for those learning Tcl or writing their own scripts.
s
simo
Revered One
Posts: 1069
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

sorry for the unclearity it is still giving same error

this is what i have in use

Code: Select all


namespace eval news { 
   set news(key) "MY-KEY" 

   set news(url) "https://newsapi.org/v2/top-headlines?sources=%source&sortBy=latest&apiKey=%key" 
   set news(timeout) 5000 

   package require http 
   package require json 
   package require tls 

   proc tls:socket args { 
      set opts [lrange $args 0 end-2] 
      set host [lindex $args end-1] 
      set port [lindex $args end] 
      ::tls::socket -servername $host {*}$opts $host $port 
   } 

   bind pub * !news [namespace current]::fetch 

   proc fetch {nick uhost hand chan text} { 
      variable news 
      if {[scan $text {%s} source] != 1} { 
         puthelp "PRIVMSG $chan :Usage: !news <source> | Pick one from https://newsapi.org/sources" 
         return 
      } 
      ::http::register https 443 [namespace current]::tls:socket 
      set url [string map [list "%source" "$source" "%key" "$news(key)"] $news(url)] 
      set token [::http::geturl $url -timeout $news(timeout)] 
      set data [::http::data $token] 
      set json [::json::json2dict $data] 
      ::http::cleanup $token 
      ::http::unregister https 
      if {[lsearch [dict get $json] "articles"] > -1} { 
         foreach item [dict get $json articles] { 
            dict with item { 
               puthelp "PRIVMSG $chan: Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url" 
            } 
         } 
      } else { 
         puthelp "PRIVMSG $chan :The specified source is invalid, pick one from https://newsapi.org/sources" 
         # To see exact error message uncomment next line 
         #puthelp "PRIVMSG $chan: Error: [dict get $json message]" 
      } 
   } 
} 

User avatar
Dominatez
Halfop
Posts: 50
Joined: Mon Jan 14, 2019 5:08 pm
Location: United Kingdom

Post by Dominatez »

When i try !news it prompts for the

Usage: !news <source> | Pick one from https://newsapi.org/sources

When i try !news cnbc (Or any other) it does nothing. No errors. Just nothing.
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I haven't tested this on a bot cos at the time cos didn't have time for it, and since I had some spare time decided to have a look on this as well.

The problem is in this line:

Code: Select all

puthelp "PRIVMSG $chan: Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url" 
More specific with $chan: where there should have been a space between the two. As it is right now, if your channel name is #test for example it will try to send the messages to the #test: channel. Notice the : behind the name. :roll:

Transform the line into:

Code: Select all

puthelp "PRIVMSG $chan :Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url" 
and will work as expected.

If the bot doesn't have some special privileges on your network it will flood itself off very easy since the code I mentioned above will just dump all the 10 articles from the page it grabs.

To display let's say fist 3 articles make the loop from:

Code: Select all

foreach item [dict get $json articles] {
	dict with item {
		puthelp "PRIVMSG $chan :Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url"
	}
}
to:

Code: Select all

set i 0
foreach item [dict get $json articles] {
	if {[incr i] > 3} break
	dict with item {
		puthelp "PRIVMSG $chan :Title: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url"
	}
}
for example.

Edit: Here's the updated code.

Code: Select all

namespace eval news {
   set news(key) "your-key"

   set news(url) "https://newsapi.org/v2/top-headlines?sources=%source&sortBy=latest&apiKey=%key"
   set news(sources) "https://newsapi.org/v2/sources?apiKey=%key"
   set news(timeout) 5000

   package require http
   package require json
   package require tls

   proc tls:socket args {
      set opts [lrange $args 0 end-2]
      set host [lindex $args end-1]
      set port [lindex $args end]
      ::tls::socket -servername $host {*}$opts $host $port
   }

   bind pub * !news [namespace current]::fetch

	proc fetch {nick uhost hand chan text} {
		variable news
		if {[scan $text {%s} source] != 1} {
			puthelp "PRIVMSG $chan :Usage: !news <source> | Pick one from https://newsapi.org/sources"
			return
		}

		::http::register https 443 [namespace current]::tls:socket
		set url [string map [list "%source" "$source" "%key" "$news(key)"] $news(url)]
		set token [::http::geturl $url -timeout $news(timeout)]
		set data [::http::data $token]
		set json [::json::json2dict $data]
		::http::cleanup $token
		::http::unregister https

		if {[dict get $json status] eq "ok"} {
			set i 0
			foreach item [dict get $json articles] {
				if {[incr i] > 3} break
				dict with item {
					puthelp "PRIVMSG $chan :Title: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url"
				}
			}
		} else {
			puthelp "PRIVMSG $chan :The specified source is invalid, pick one from https://newsapi.org/sources"
			# To see exact error message uncomment next line
			#puthelp "PRIVMSG $chan :Error: [dict get $json message]"
		}
	}
}
Last edited by caesar on Fri Mar 01, 2019 3:08 pm, edited 1 time in total.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1069
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

tested it and got this error :
14:58:12 <TCL-Tester> [14:56:42] Tcl error [::news::fetch]: invalid character "]"
14:58:12 <TCL-Tester> in expression "...text {%s} source] != 1]"
after i changed it to
if {[scan $text {%s} source] != 1} {
it still returned me the old error of
<TCL-Tester> [14:59:00] Tcl error [::news::fetch]: wrong # args: should be "tls::socket ?options? host port"
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Yeah, I messed with that. I added a command then changed my mind and forgot to fix it. Removing the extra ] behind -1 should do the trick. Edited and fixed the code above.

The tls:socket that I got in this code accepts any or no arguments. ANd since you get that error means you got the same code in some other place. To see if it's really the case then add a:

Code: Select all

putlog "correct tls::socket was called!"
at the end of the tls:socket function and see if you see that message in the party-line.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1069
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

this is what i got so far and i didnt get the log in PL

Code: Select all

namespace eval news { 
   set news(key) "my key" 

   set news(url) "https://newsapi.org/v2/top-headlines?sources=%source&sortBy=latest&apiKey=%key" 
   set news(sources) "https://newsapi.org/v2/sources?apiKey=%key" 
   set news(timeout) 5000 

   package require http 
   package require json 
   package require tls 

   proc tls:socket args { 
      set opts [lrange $args 0 end-2] 
      set host [lindex $args end-1] 
      set port [lindex $args end] 
      ::tls::socket -servername $host {*}$opts $host $port 
       putlog "tls:correct socket was called!" 
   } 

   bind pub * !news [namespace current]::fetch 

   proc fetch {nick uhost hand chan text} { 
      variable news 
      if {[scan $text {%s} source] != 1} { 
         puthelp "PRIVMSG $chan :Usage: !news <source> | Pick one from https://newsapi.org/sources" 
         return 
      } 

      ::http::register https 443 [namespace current]::tls:socket 
      set url [string map [list "%source" "$source" "%key" "$news(key)"] $news(url)] 
      set token [::http::geturl $url -timeout $news(timeout)] 
      set data [::http::data $token] 
      set json [::json::json2dict $data] 
      ::http::cleanup $token 
      ::http::unregister https 

      if {[dict get $json status] eq "ok"} { 
         set i 0 
         foreach item [dict get $json articles] { 
            if {[incr i] > 3} break 
            dict with item { 
               puthelp "PRIVMSG $chan :Title: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url" 
            } 
         } 
      } else { 
         puthelp "PRIVMSG $chan :The specified source is invalid, pick one from https://newsapi.org/sources" 
         # To see exact error message uncomment next line 
         #puthelp "PRIVMSG $chan :Error: [dict get $json message]" 
      } 
   } 
} 
still gettin : Tcl error [::news::fetch]: wrong # args: should be "tls::socket ?options? host port"

and i got like max 5 tcls loaded wich none use tls and in conf neither
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Right, replace the news in the namespace line with something else, let's say newsFetch and restart the bot. Issue the !new cnbc (or whatever) and see what happens.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1069
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

hm still gettin same error

Tcl error [::newsFetch::fetch]: wrong # args: should be "tls::socket ?options? host port"
Post Reply