読者です 読者をやめる 読者になる 読者になる

PythonでLingrのbot書いた

# -*- coding: utf-8 -*-
from lingr import *
import re, pickle

class MessageHandler:
    memo = None
    logpath = ''
    
    def handle(self, text):
        print "handling:" + text
        return (self.handlePut(text) or 
                self.handleAdd(text) or 
                self.handleGet(text) or 
                self.handleList(text) or
                self.handleSave(text))

    def __init__(self, logpath):
        self.logpath = logpath
        try:
            self.memo = pickle.load(open(logpath))
        except:
            self.memo = {}
    
    def __del__(self):
        self.save()
    
    def save(self):
        pickle.dump(self.memo, open(logpath, "w"))
    
    
    put_pattern = re.compile("^put\s+([^\s:]+)\s*:\s*(.+)$")
    def handlePut(self, text):
        result = self.put_pattern.match(text)
        if not result:return
        
        self.memo[result.group(1)] = result.group(2)
        return "putted"
    
    add_pattern = re.compile("^add\s+([^\s:]+)\s*:\s*(.+)$")
    def handleAdd(self, text):
        result = self.add_pattern.match(text)
        if not result or not self.memo.has_key(result.group(1)):return
        
        self.memo[result.group(1)] += "\n" + result.group(2)
        return "added"
    
    get_pattern = re.compile("^get\s+([^\s:]+)")
    def handleGet(self, text):
        result = self.get_pattern.match(text)
        if not result or not self.memo.has_key(result.group(1)):return
        
        return "=>" + self.memo[result.group(1)]
    
    def handleList(self, text):
        if text == "list":
            ret = "=>"
            for key in self.memo:
                ret += key + " "
            return ret
    
    def handleSave(self, text):
        if text == "save":
            self.save();
            return "saved"
        


class G91Bot:
    lingr = None
    counter = 0
    
    def __init__(self, nickname, id, apikey):
        
        self.lingr = lingr = Lingr(apikey)
        lingr.api.session.create()
        
        self.counter = lingr.api.room.enter(id=id, nickname=nickname).room.counter
    
    def run(self, handler):
        
        while True:
            try:
                result = self.lingr.api.room.observe(counter=self.counter)
                self.counter = result.counter

                msg = result.messages.message
                if msg.client_type == "human":
                    handler_result = handler.handle(msg.text)
                    if handler_result: 
                        print handler_result
                        self.lingr.api.room.say(message=handler_result.encode('utf-8'))
            except AttributeError:
                continue


    
    def __del__(self):
        api = self.lingr.api
        
        api.room.exit()
        api.session.destroy()


動かす時はこれlingr.pyを同じディレクトリにおいて

bot = G91Bot("bot_nickname", "chat_id", apikey)
bot.run(MessageHandler("memo.p"))

で動く。


使い方としては

put hoge:contents for hoge
get hoge
add hoge:contents for hoge
save
list

こんな感じ。
説明するのがだるい

g91hoge_botという名前で実際に動かしてるので試したい方はどうぞ。
追記:もう動かしてません