搭建GTalk聊天机器人系列(2)——查字典、查IP、短地址缩减

在上一篇《搭建GTalk聊天机器人系列(1)——远程控制》中,我详细介绍了在Linux下用freetalk搭建GTalk聊天机器人的详细步骤。

在这一部分中,我将介绍如何用freetalk实现GTalk聊天机器人的查字典,查IP,短地址缩减的功能。这里假设有两台机器,一台做Server,一台做Client。

1.Server端

需要安装一个lynx的文本浏览器,使用如下命令:

apt-get install lynx

其他前期准备工作跟上一篇讲的一样,只要把配置文件替换成如下的即可。

点击这里下载。

01 (ft-set-jid! "[email protected]")
02 (ft-set-server! "talk.google.com")
03 (ft-set-password! "passwd")
04 (ft-set-prompt! "#FreeTalk# ")
05 (ft-set-sslconn! #t)
06 (ft-set-port! 5223)
07
08 (define str (string #\null))
09 (define cmd (string #\null))
10 (define pronindex 0)
11 (define pronindex2 0)
12 (define flag #f)
13
14 (add-hook! ft-message-receive-hook
15 (lambda (time from nickname message)
16
17 (if (> (string-length message) 4 )
18    ;(ft-send-message from "Unknown command, type help for usage")
19   
20    (begin
21      (set! cmd (substring message 4))
22      (cond ((equal? (substring message 0 4) "dic ");Dictionary
23
24     (begin
25       (let*
26         ((port (open-input-pipe (string-append "lynx -source -assume_charset=UTF8 \"http://dict.cn/ws.php?utf8=true&q=” cmd "\"”)))
27          (strline (read-line port)))
28         (set! str (string #\null))
29         (while (not (eof-object? strline))
30            (if (string-contains strline "<audio>") (set! flag #t))
31            (if (equal? flag #t) (set! str (string-append str strline "<br>\n")))
32            (if (string-contains strline "</def>") (set! flag #f))
33            (set! strline (read-line port))
34            )
35         (close-pipe port))
36       (if (equal? str (string #\null))
37         (ft-send-message from "Sorry, the word is not found!")
38         (begin
39           (set! pronindex (string-contains str "<pron>"))
40           (set! str (string-replace str "[" (+ 6 pronindex) (+ 6 pronindex)))
41           (set! pronindex2 (string-contains str "</pron>"))
42           (set! str (string-replace str "]" pronindex2 pronindex2))
43           (let* ((srcfileport (open-output-file "/tmp/gtalkaudiotmp")))
44             (display str srcfileport)
45             (close-output-port srcfileport))
46           (send-message-pipe from "lynx -dump -assume_charset=UTF8 -force_html /tmp/gtalkaudiotmp")
47           ))
48       ))
49
50    ((equal? (substring message 0 4) "url ");Shorten URL
51     (send-message-pipe from (string-append "lynx -source \"http://is.gd/create.php?format=simple&url=” cmd "\"”)))
52
53    ((equal? (substring message 0 4) "add ");IP address lookup
54     (send-message-pipe from (string-append "lynx -source -assume_charset=UTF8 \"http://ip2loc.appspot.com/q/?ip=” cmd "&alt=plain\"”)))
55
56    ((equal? (substring message 0 4) "ctl ");Remote control
57     (send-message-pipe from cmd))
58
59    ;(else (ft-send-message from "Unknown command, type help for usage"))
60    )))))

只要将文件开头的用户名和密码替换成自己的即可。

2.输入语法举例

单词查询:dic word

短地址缩减:url http://example.com/example.html

IP地址定位:add 8.8.8.8

远程控制:ctl ls /

这里的查单词功能是可以播放单词发音的,但需要配置freetalk客户端才可以实现,其他功能用任意客户端均可实现。

3.Client端

这里需要安装一个软件:

apt-get install mpg321

然后下载配置文件放到主目录下的.freetalk文件夹里,并把文件名改成freetalk.scm

01 (ft-set-jid! "[email protected]")
02 (ft-set-server! "talk.google.com")
03 (ft-set-password! "passwd")
04 (ft-set-prompt! "#FreeTalk# ")
05 (ft-set-sslconn! #t)
06 (ft-set-port! 5223)
07
08 (define index #f)
09 (define endindex #f)
10 (add-hook! ft-message-receive-hook
11 (lambda (time from nickname message)
12 (begin
13    (set! index (string-contains message "http://mp3.dict.cn/mp3.php"))
14    (if (integer? index)
15     (begin
16       (set! endindex (string-index message #\newline index))
17       (if (integer? endindex)
18          (system (string-append "mpg123 " (substring message index endindex) " 2>/dev/null"))
19          (system (string-append "mpg123 " (substring message index) " 2>/dev/null" ))
20          )))
21        )))

同样替换你自己的GTalk帐号。用Client机器运行freetalk,向Server端机器人发送例如:dic word就可以查询“word”这个词的具体解释了,并且会自动播放该单词的发音。

到此为止,聊天机器人的功能基本就全部实现了,其实如果想添加新的功能,找到合适的API,自行添加进去也非常容易。但这里有个问题,要保证Server端一直在线并运行freetalk,而一般人家里没有服务器,这就很不方便了。所以我打算下一步把聊天机器人放到Google App Engine上,这样就可以24小时不间断运行了。

Linux, Programming , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *