Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java程序分享区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 一起来写GTalk机器人吧 [精华]
Jove



CJSDN高级会员


发贴: 1228
于 2005-09-09 20:49 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://www.jivesoftware.org/smack/ 提供了一个很不错的开源XMPP java api (他们也做了Flash版的)

以下是一段Groovy code, 各位熟悉Java应该不难读懂它。
现在还不算一个真正的机器人,做的还很粗燥, 需要好好研究一下SMACK的API
考虑要用Eclipse写个自己的GTalk客户端,提供多种mode
正常情况下,可以和好友聊天
BT的时候切换到其他mode,比如人家说什么你也说什么,或者不停的播报当前时间或天气
或者用它组建一个聊天室,或者提供一个groovy console,可以执行groovy脚本
或者做成ant或maven的console,可以在里面发送build命令
或者做成Crack的通用界面,别人要SN,只要把软件名告诉我,他会提示输入用户名
生成Key输出。
或者做成拆数字等无聊游戏的界面
或者做成文字MUD的界面
或者做成RSS Reader的界面

娃哈哈,yy的好爽 :)

import org.jivesoftware.smack.*
import org.jivesoftware.smack.packet.*
import org.jivesoftware.smack.filter.*
import java.text.*

def gmailId,gmailPwd,targetId
if(args.length==2){
  gmailId=args[0]
  gmailPwd=args[1]
}else{
  print 'gmail id: '
  gmailId=System.in.readLine()
  print 'gmail password: '
  gmailPwd=System.in.readLine()
}

XMPPConnection g
succ=false
for(i in 0..5){
  try{
    println 'connecting to google talk server..'
    g = new XMPPConnection("talk.google.com", 5222,"gmail.com")
    g.login(gmailId,gmailPwd)
    succ=true
    break
  }catch(Exception e){
    println 'Exception: '+e.message
    //e.printStackTrace()
  }
}

if(!succ){
  println 'fail.'
  System.exit(0)
}

println 'isConnected:' +g.isConnected()
println 'isAuthenticated:'+g.isAuthenticated()
println 'isAnonymous:'+g.isAnonymous()
println 'isUsingTLS:'+g.isUsingTLS()

def iter=g.roster.entries
while(iter.hasNext()){
  println iter.next()
}

g.addPacketListener(new EchoDate(g),new MyFilter())

chat=g.createChat(gmailId+'@gmail.com')
println 'thread id:'+ chat.getThreadID()
chat.setFilteredOnThreadID(false)
chat.sendMessage('hi, there. My name is robot Jove. Now is '+new Date())
while(true)
  chat.nextMessage()

class EchoDate implements PacketListener{
  def connection
  def df=new SimpleDateFormat('MM/dd/yy HH:mm:ss.SSS')
  EchoDate(connection){this.connection=connection}
  void processPacket(Packet packet){
    if(packet instanceof Message){
      def from=packet.from
      
      if(from.indexOf('jovezhong@gmail.com')>=0){
        println 'got ack'
        return
      }  

      def txt=packet.body
      println "get message from ${from}: ${txt}"
      
      def msg=new Message(from)  
      switch(txt){
        case 'exit':
          connection.close()
          System.exit(0)
        case 'hi':
          msg.body='hi :) type help for usage'
          break
        case 'bye':
          msg.body='bye~ Have a good day! :)'
          break    
        case 'help':
          msg.body='I am the first robot made by Jove(jovezhong@gmail.com). However he is such an *idiot* that I have only little A.I :( .\nnow: for current timestamp'  
          break
        case 'now':
          msg.body="now is ${df.format(new Date())}"    
          break
        default:  
          msg.body=":-( what do you mean by ' *${txt}* '? type help for usage"
      }
      println "send reply:${msg.body}"
      connection.sendPacket(msg)
    }
  }
}

class MyFilter implements PacketFilter{
  public boolean accept(Packet packet) {return true}
}




话题树型展开
人气 标题 作者 字数 发贴时间
8071 [精华] 一起来写GTalk机器人吧 Jove 4811 2005-09-09 20:49
5351 Re:一起来写GTalk机器人吧 looluo 8 2005-09-09 21:19
5691 Re:一起来写GTalk机器人吧 looluo 63 2005-09-09 21:42

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923