如果您想对cli、irc或xmpp bots进行编程,bl是一个python3框架。
功能
BL提供以下功能:object class save/load to/from a json file.
rest server serve saved object’s over http.
rss fetcher echo rss feeds to irc channels.
udp server udp to bot to irc channel.
watcher server run tail -f and have output send to IRC channel.
email scanning scan mbox format to searchable bl objects.
json backend objects are stored as json string in files on the fs.
db iteration over stored objects.
timestamp time based filenames gives logging capabilities
future future sensors should provide entry to the logger.
安装
克隆源:bart@okdan:~$ hg clone https://bitbucket.org/thatebart/bl
You might need to do one of the following if the bot doesn't work:
bart@okdan:~/bl$ export PYTHONPATH="."
bart@okdan:~/bl$ export PYTHONIOENCODING="utf-8"
另一个选择是使用pip3下载并全局安装:bart@okdan:~$ pip3 install bl --upgrade
IRC
使用-n<;nick>;、-s<;server>;、-c<;channel>;选项使机器人加入网络:bart@okdan:~$ bot -i irc -n bl -s irc.freenode.net -c \#bl
您可以使用-w选项将配置值写入~/.bot/config/irc
xmpp
对于xmpp bot,使用-i xmpp和-room参数:bart@okdan:~$ bot -i xmpp --room=test@conference.localhost
用户
需要添加一个用户源代码,以便能够发出bot命令。可以使用meet命令添加用户:bart@okdan:~$ bot meet user@server
user user@server created
要授予用户权限,可以使用perm命令:bart@okdan:~$ bot perm user@server ps
ok user@server
u命令显示用户对象的json转储:bart@okdan:~$ bot u user@server
{
"_container": {
"default": "",
"path": "/home/bart/.bot/user/2017-10-12/21:05:52.095737",
"prefix": "object",
"saved": "Thu Oct 12 21:07:03 2017",
"signature": "c113c4125f8c2a88d5b312e283792ae019c61a52"
},
"_type": "",
"origin": "user@server",
"perms": [
"USER",
"PS"
],
"user": "user@server"
}
默认的shell用户是root@shell,允许访问所有可用的命令。
命令
以下命令可用:alias key, value alias.
announce announce text on all channels in fleet.
begin begin stopwatch.
cfg edit config files.
cmnds show list of commands.
deleted show deleted records.
delperm delete permissions of an user.
dump dump objects matching the given criteria.
edit edit and save objects.
end stop stopwatch.
exit stop the bot.
fetcher fetch all rss feeds.
find present a list of objects based on prompt input.
first show the first record matching the given criteria.
fix fix a object by loading and saving it.
idle see how long a channel/nick has been idle.
last show last objectect matching the criteria.
license display BL license.
load force a plugin reload.
log log some text.
loglevel set loglevel.
loud disable silent mode of a bot.
ls show subdirs in working directory.
man show descriptions of the available commands.
mbox convert emails to bl objects.
meet create an user record.
nick change bot nick on IRC.
perm add/change permissions of an user.
permissions show permissions granted to a user.
perms show permission of user.
pid show pid of the BL bot.
ps show running threads.
reboot reboot the bot, allowing statefull reboot (keeping connections alive).
reload reload a plugin.
restore set deleted=False in selected records.
rm set deleted flag on objects.
rss add a rss url.
save make a kernel dump.
shop add a shopitem to the shopping list.
show show dumps of basic objects.
silent put a bot into silent mode.
start start a plugin.
stop stop a plugin.
synchronize synchronize rss feeds (fetch but don't show).
test echo origin.
timer timer command to schedule a text to be printed on a given time. stopwatch to measure elapsed time.
today show last week's logged objects.
todo log a todo item.
tomorrow show todo items for tomorrow.
u show user selected by userhost.
uptime show uptime.
version show version.
w show user data.
watch add a file to watch (monitor and relay to channel).
week show last week's logged objects.
whoami show origin.
yesterday show last week's logged objects.
模块
提供以下模块:bl bl package.
bl.bot bot base class.
bl.cli command line interfacce bot, gives a shell prompt to issue bot commands.
bl.clock timer, repeater and other clock based classes.
bl.cmnds bl basic commands.
bl.compose construct a object into it’s type.
bl.decorator method decorators
bl.db JSON file db.
bl.engine select.epoll event loop, easily interrup_table esp. versus a blocking event loop.
bl.error bl exceptions.
bl.event event handling classes.
bl.fleet fleet is a list of bots.
bl.handler schedule events.
bl.irc IRC bot class.
bl.launcher a launcher launches threads (or tasks in this case).
bl.log log module to set standard format of logging.
bl.object JSON file backed object with dotted access.
bl.raw raw output using print.
bl.rss rss module.
bl.selector functions used in code to select what objects to use.
bl.task adapted thread to add extra functionality to threads.
bl.trace functions concering stack trace.
bl.users class to access user records.
bl.xmpp XMPP bot class.
bl.register object with list for multiple values.
bl.rest rest interface.
bl.runner threaded loop to run tasks on.
bl.space central module to store objects in.
bl.static static definitions.
bl.template cfg objects containing default values for various services and plugins.
bl.test plugin containing test commands and classes.
bl.udp relay txt through a udp port listener.
bl.utils lib local helper functions.
bl.url functions that fetch data from url.
bl.watcher watch files.
bl.worker worker thread that handles submitted jobs through Worker.put(func, args, kwargs).
编程
bl使您可以编程自己的模块,启用自己的命令。
代码示例:def hi(event):
event.reply("hi %s" % event.origin)
用户编程模块从当前的botmods目录中读取
工作目录,你可以把你的.py文件放在那里。
许可证
BL有麻省理工学院的执照。# -*- coding: utf-8 -*-
#
# BL Framework to program bots
#
# LICENSE
#
# Copyright 2017,2018 B.H.J Thate
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice don't have to be included.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Bart Thate
# Heerhugowaard
# The Netherlands