Skip to content

Linusp/bbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BearyBot

一个用在 BearyChat 中的辅助用机器人.

安装

下载本项目代码后,通过 setup.py 来安装:

python setup.py install

或者直接从 github 上安装:

sudo pip install git+https://github.com/Linusp/bbot.git

设置和使用

命令行工具 bbot 使用:

usage: bbot [-h] [-w WORD] [-p PORT]

Start a BearyBot

optional arguments:
  -h, --help            show this help message and exit
  -w WORD, --word WORD  Set trigger word of bot.
  -p PORT, --port PORT  Set port the bot listen, default value is 7428

该命令行工具即集成了两个命令,分别是:

  • /gif: 使用 Giphy API 搜索 gif 图片
  • /img: 使用 Google 搜索图片 (Google 图像搜索 API 已失效)
  • /talk: 聊天

聊天功能使用的是图灵机器人,需要获取相应的 API KEY,将你的 API KEY 写到 HOME 目录下的 .botrc 中去,以 json 格式保存,如:

{
    "turing_bot_key": "sldkfjklsdjf299rejkldjf9"
}

当然你也可以自定义自己的机器人,通过向中心控制模块注册更多的命令响应方法来集成更丰富的功能

from bearybot import BearyBot, Controller

def you_proc(paras, info):
    """description

    :type paras: str
    :param paras: parameter

    :type info: dict
    :param info: internal parameters
    """
    # do something
    # ......
    return {
        '<key>': <content>,
    }


controller = Controller()
controller.register('/new_command', you_proc) # 使用 /new_command 作为 命令

bot = BearyBot('@bot', controller) # 使用 '@bot' 作为触发词
bot.run()

注册新的命令响应方法时有以下几点要注意:

  1. paras 参数是一个 str,包含给自定义命令的参数

    中心控制模块会在得到输入后,去除触发词和命令本身后,将剩下的字符串用来做为命令响应方法的参数。

  2. info 参数是一些内部参数,包含 POST 请求中的请求参数,以及本地的一些配置
  3. 返回结果必须是一个 dict,如果返回结果是文本,那么就返回 {'text': <result>}
  4. 中心控制模块内置了三个命令,通过用相同命令名来注册不同的方法,可以覆盖内置的这三个命令的响应方法。
  5. 通过 Controller.set_default 方法可以设置无命令词时的默认处理方法

About

Simple robot for BearyChat

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages