其实就是08年,设想的,针对上班族,开发一个MSN机器人,通过对MSN机器人发送指令,来玩策略类网页游戏。 当初已经完成了MSN机器人的所有程序部分。通过MSN发送指令,可以完成游戏中几乎所有的建设、军事、消息指令…..

当继续想完善时候,发现完全使用中文是实现,目前缺少完善的AIML中文语料库,这个很难有现有资源利用……

# -*- coding: utf-8 -*-

import aiml
import sys
import time
import os.path
import thread
import re
from datetime import datetime
import marshal


loginemail="xinze@live.cn" # that robot will login to
loginpassword="11111" # account password
botname="Xinze...." # Robot's nickname
admin_email="xinze@live.cn" # emailaddress for admin the robot

# ==== normally nothing needs to be changed below ====

rootdir=os.getcwd()
configfile="%s/chatbot.ini" % rootdir
sessiondir="%s/sessions/%s" % (rootdir,loginemail)
os.system("mkdir -p %s" % sessiondir)
logfilename="%s/log" % sessiondir 
sessionfilename="%s/session" % sessiondir

os.system("mkdir -p %s/tmp/" % rootdir)
msn_fortune="%s/tmp/msn-fortune" % rootdir
msn_fortune_zh="%s/tmp/msn-fortune-zh" % rootdir

blacklist=['sample@blacklist.org' ] # put here email addresses you want to block
blacklist=set(blacklist)
adminlist=[admin_email] # the 'admin' account's email address from which you send admin command
adminlist=set(adminlist)

dlist=[]
talkedto=[]

use_brain=True


def config(configfile, k):
    # setup bot properties such name, birthplace etc
    f=open(configfile)
    g=f.readlines()
    f.close()
    for i in g:
        h=i.split('=')
        if len(h)==2:
            k.setBotPredicate(h[0].strip(), h[1].strip()) 

    print sessionfilename
    # load session
    if os.path.exists(sessionfilename):
        f=open(sessionfilename)
        g=f.read()
        f.close()
        g=g.split('\n')
        for i in g:
            if i!='':
                talkedto.append(i)
                sessionFile = file("%s/%s.ses" %(sessiondir, i), "rb")
                session = marshal.load(sessionFile)
                sessionFile.close()
                for pred,value in session.items():
                    k.setPredicate(pred, value, i)

def dolog(message):
	global logfile
	logfile.write(message)
	logfile.flush()

def filter(message):
	message=message.replace("ALICE", botname)
	return message


def test_msg(senderemail, message):
    global dlist

    sender=senderemail.split("@")

    if senderemail in blacklist:
        return	

    dlist=[]

    if not senderemail in set(talkedto):
        talkedto.append(senderemail)

    logtime='%s/%s/%s %s:%s:%s' %( datetime.now().month, datetime.now().year, datetime.now().day, datetime.now().hour, datetime.now().minute, datetime.now().second)

    if not senderemail in adminlist:
        dolog('=== %s === Recv from %s < %s> ===\n%s\n' % (logtime, sender[0], senderemail, message))
    if message=='admquit' and senderemail in adminlist:
        message="%s is offline!" % botname
        #quit()

    if message=='admlist' and senderemail in adminlist:
        return

    if k.getPredicate("name", senderemail) =='':
        dispname=senderemail.split("@")
        if len(dispname) ==2:
            k.setPredicate("name", dispname[0], senderemail)
    remsg=k.respond(message, senderemail)
    remsg=filter(remsg)

    logtime='%s/%s/%s %s:%s:%s' %( datetime.now().month, datetime.now().year, datetime.now().day, datetime.now().hour, datetime.now().minute, datetime.now().second)
    if not senderemail in adminlist:
        dolog('=== %s === Send to %s < %s>===\n%s\n\n' % (logtime, sender[0], senderemail, remsg))
    
    print remsg


def savesession():
    # =========== save session
    f=open(sessionfilename, 'w')
    for i in talkedto:
        session = k.getSessionData(i)
        sessionFile = file("%s/%s.ses" % (sessiondir, i), "wb")
        marshal.dump(session, sessionFile)
        sessionFile.close()
        f.write("%s\n"% i)
    f.close()

def do_work():
    time.sleep(5)
    savesession() # save session data every 10 min
    thread.start_new_thread(do_work, ())

thread.start_new_thread(do_work, ())


# ================== Initialization
if __name__ == '__main__':
    k = aiml.Kernel()
    config(configfile, k)
    #k.verbose(False)

    # ================== load brain 
    if use_brain and os.path.isfile("standard.brn"):
        k.bootstrap(brainFile = "standard.brn")
    else:
        k.bootstrap(learnFiles = "startup.xml", commands = "load aiml b")
        k.saveBrain("standard.brn")

    if os.path.exists(logfilename):
        logfile=open(logfilename, "a")
    else:
        logfile=open(logfilename, "w")

    player_email="alex@live.cn"

    while True:
        message = raw_input('>')
        test_msg(player_email, message)

发表评论

电子邮件地址不会被公开。 必填项已用*标注