python hipchat机器人

hippybot的Python项目详细描述


简介

hippybot是一个用python编写的hipchat.com聊天室机器人。从技术上讲,它只是一个jabber/xmpp机器人,但它具有连接hipchat和处理hipchat的定制功能。

hippybot包含一个简单的插件api,用于添加命令。

安装

有几种安装hippybot的方法:

使用setup.py:下载并安装

python setup.py install

从pypi安装:

pip install hippybot
# or using setuptools
easy_install hippybot

配置

hippybot发布了一个名为hippybot.conf.example的示例配置文件,复制并编辑它。您需要向hipchat组添加一个帐户,以便bot连接为。以帐户身份登录时,转到XMPP设置页以获取编辑配置文件所需的详细信息。

用法

像这样从终点站跑河马:

hippybot -c path/to/your/config/file.conf

要作为后台守护进程运行,请使用-d-daemon

hippybot -c path/to/your/config/file.conf -d

也可以提供pid文件的路径,以便与-p--pid一起使用。 note:目前,您必须提供自己的控制脚本,例如用于init.dupstart

您应该可以看到bot连接配置文件中列出的任何通道。然后,您可以使用at符号表示法(例如:

@botname rot13 hello world

如果您将rot13示例插件设置为加载(通过配置文件的plugins部分),那么bot将使用带有rot13'd(每个字符偏移13)文本“hello world”版本的at sign符号回复您。

机器人有两个内置命令:

  • ^{tt10}$: this will reload any updated plugins (note it will also reset the internal state of any loaded plugins, e.g. the counter in the mexican wave plugin). Note it does not reload the bot’s configuration file and so will not load new plugins.
  • ^{tt11}$: this will reload the bot itself, reloading the configuration file, reconnecting to HipChat and reloading any plugins, in the process. Note: it does not end the main process, you would have to do that yourself from the terminal (for example if HippyBot were updated).

插件

Hippybot目前有3个插件:

  • ^{tt9}$: this is mostly included as an example, it ROT13s, any text spoken directly to the bot, back at the speaker.

  • ^{tt13}$: this is a fun plugin that completes a mexican wave if 2 people in a row say ^{tt14}$ in a channel, e.g.:

    John Smith: \o/
    Joe Bloggs: \o/
    Hippy Bot:  \o/
    
  • ^{tt15}$: look up a term on Urban Dictionary, the first 3 definitions will be posted back to the channel. Warning: many terms are NSFW.

要指示bot加载插件,请在配置文件的plugin s部分的load字段中包含插件的模块路径,例如加载位于hippybot/plugins/中的mexican_wave.py文件中的mexican_wave插件,您可以将其写为:

[plugins]
load = hippybot.plugins.mexican_wave

要加载名为my_custom_plugin的插件,该插件已安装到python路径中myhippybotplugins下名为my_custom_plugin.py的文件名(或模块)中,您需要附加(在新行上,请注意缩进):

[plugins]
load = hippybot.plugins.mexican_wave
       myhippybotplugins.my_custom_plugin

要在Hippybot以外的路径中搜索插件,您可以使用加载路径选项:

[plugins]

load_path = /absolute/path/plugins
    hippybot/plugins
    plugins
    another/relative/path

插件API

Hippybot包含一个非常简单的插件API。要向hippybot添加命令,只需要一个可导入的python包(例如,PYTHON_PATH中包含名为__init__.py的文件的目录),然后将插件放在它自己的python文件中(rot13插件位于rot13.py)。

然后,您可以包含一个与文件同名且与要注册的命令相同的函数,并使用botcmddirectcmd装饰符:

# hello_world.py
from hippybot.decorators import directcmd

@directcmd
def hello_world(bot, mess, args):
    return u'Hello world!'

这将命令hello world注册为直接命令,这意味着文本“hello world!“将使用at符号直接对用户进行应答。另一方面,botcmddecorator将在频道中响应,而不针对原始扬声器。 默认情况下,这些基于函数的插件只支持直接命令(使用at-sign符号与bot对话),但是您可以使用基于类的插件创建更复杂的插件,并具有更大的控制能力。

若要创建基于类的插件,请使用任何所需的描述性名称正常创建python模块,并在模块中包含名为Plugin的类,例如hello world插件可以编写为:

# hello_world.py
from hippybot.decorators import botcmd, directcmd

class Plugin(object):
    global_commands = ['hello_world']
    command_aliases = {'hi!': 'hello'}

    @botcmd
    def hello_world(self, mess, args):
        return u'Hello world!'

    @directcmd
    def hello(self, mess, args):
        return u'Well hello there..'

它使用两个特殊属性:

  • ^{tt30}$: a list of command method names that can be triggered without targetting the bot using at-sign notation (just say the command in the channel without mentioning the bot).
  • ^{tt31}$: dict of command aliases and the methods they map to, this is a way of triggering a command from a string that can’t be used as a Python method name (e.g. using special symbols such as the “o/” trigger used in the mexican wave plugin).
  • ^{tt32}$: a list of handler method names that will be passed all incoming XMPP message objects regardless of type as. This can be used for low-level hanbdling of Jabber messages without using the higher level message handling of jabberbot or hippybot.

hipchat api

hippybot为HipChat API包含了一个非常简单的面向对象的包装器。要使用api,您需要create an API key并将其作为名为api_auth_token的选项输入到hipchat部分下的配置文件中,例如:

[hipchat]
api_auth_token = xxxxxxxxxxxxxxxxxxxxxxxx

如果您使用的是自托管hipchat服务,请设置如下设置:

[hipchat]
api_server = api.example.com
api_auth_token = xxxxxxxxxxxxxxxxxxxxxxxx

那么你就可以访问他通过bot实例上的api属性包装,例如从插件类的命令方法中包装:

# hello_world.py
from hippybot.decorators import botcmd

class Plugin(object):
    global_commands = ['hello']

    @botcmd
    def hello(self, mess, args):
        channel = unicode(mess.getFrom()).split('/')[0]
        # Say hello world as a room notification
        # Params to the API wrapper are sent as dicts
        self.bot.api.rooms.message({
            'room_id': channel,
            'from': self.bot._config['connection']['nickname'],
            'message': 'Hello world!'
        })

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Hibernate:合并并删除,而不是从数据库中删除实体对象   java如何在WebFilter中有条件地从Mono返回?   java调用mysql c3p0函数   可执行jar Java jar文件加载错误:无法找到或加载主类。类导致的错误:java。lang.ClassNotFoundException:某些东西。班   java如何减少/更改爬网后的延迟?   从其他语言(如Java、PHP、Perl、Python等)调用C/C++代码的最佳方式是什么?   java如何模拟影响对象的void返回方法   当我试图在ubuntu上启动JavaScala时,它抛出了一个异常   java如何正确输出游戏   理解java和C++背景下的JavaScript原型   oracle如何将Java函数转换为postgresql函数   多线程为什么我的java服务器程序在超时后不退出?   java如何使listView中的按钮在单击时工作?   试图将这个嵌套的java forloop转换为python,但我不知道如何转换。有没有一种方法可以像这样为循环执行if语句?   java幂函数在计算器中的应用   如何在java中滚动浏览mysql数据库   在Spring Boot应用程序的JUnit测试中,java没有符合自动连线JPA存储库要求的bean   java如何使用扩展类的JPanel对象?