egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Not Working 100%
Goto page Previous  1, 2
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
simo
Master


Joined: 22 Mar 2015
Posts: 330

PostPosted: Sat Feb 23, 2019 11:47 am    Post subject: Reply with quote

sorry for the unclearity it is still giving same error

this is what i have in use

Code:


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]"
      }
   }
}

Back to top
View user's profile Send private message
Dominatez
Voice


Joined: 14 Jan 2019
Posts: 28
Location: United Kingdom

PostPosted: Sat Feb 23, 2019 3:56 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3584
Location: Mint Factory

PostPosted: Thu Feb 28, 2019 11:51 am    Post subject: Reply with quote

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:

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. Rolling Eyes

Transform the line into:
Code:

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:

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

to:
Code:

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:

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]"
      }
   }
}

_________________
I tawt I taw a puddy tat!


Last edited by caesar on Fri Mar 01, 2019 3:08 pm; edited 1 time in total
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 330

PostPosted: Fri Mar 01, 2019 10:04 am    Post subject: Reply with quote

tested it and got this error :
Quote:
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
Quote:
if {[scan $text {%s} source] != 1} {


it still returned me the old error of

Quote:

<TCL-Tester> [14:59:00] Tcl error [::news::fetch]: wrong # args: should be "tls::socket ?options? host port"
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3584
Location: Mint Factory

PostPosted: Fri Mar 01, 2019 3:12 pm    Post subject: Reply with quote

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:

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.
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 330

PostPosted: Fri Mar 01, 2019 3:17 pm    Post subject: Reply with quote

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

Code:

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
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3584
Location: Mint Factory

PostPosted: Sat Mar 02, 2019 4:10 am    Post subject: Reply with quote

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.
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 330

PostPosted: Sat Mar 02, 2019 10:27 am    Post subject: Reply with quote

hm still gettin same error

Tcl error [::newsFetch::fetch]: wrong # args: should be "tls::socket ?options? host port"
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber