| View previous topic :: View next topic |
| Author |
Message |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Sat Feb 23, 2019 11:47 am Post subject: |
|
|
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 |
|
 |
Dominatez Halfop

Joined: 14 Jan 2019 Posts: 46 Location: United Kingdom
|
Posted: Sat Feb 23, 2019 3:56 pm Post subject: |
|
|
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 |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Thu Feb 28, 2019 11:51 am Post subject: |
|
|
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.
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]"
}
}
}
|
_________________ Once the game is over, the king and the pawn go back in the same box.
Last edited by caesar on Fri Mar 01, 2019 3:08 pm; edited 1 time in total |
|
| Back to top |
|
 |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Fri Mar 01, 2019 10:04 am Post subject: |
|
|
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 |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Fri Mar 01, 2019 3:12 pm Post subject: |
|
|
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. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Fri Mar 01, 2019 3:17 pm Post subject: |
|
|
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 |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sat Mar 02, 2019 4:10 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Sat Mar 02, 2019 10:27 am Post subject: |
|
|
hm still gettin same error
Tcl error [::newsFetch::fetch]: wrong # args: should be "tls::socket ?options? host port" |
|
| Back to top |
|
 |
|