可插拔python电报机器人
telegrambot的Python项目详细描述
尽管这段代码在生产中被大量使用 好几个月的环境,我真的不会把它当作 伟大代码的例子。
公平地说,我偏离了方向,决定看看我 可能会滥用python的某些方面(例如元类/类 创造)。
你被警告了!
这样一来,telegrambot就很容易站起来 跑步。
% pip install telegrambot % cat mybot.cfg [core] bot_id = <telegram_bot_id> token = <telegram_bot_token> [bing] key = <bing_api_key> % telegrambot mybot.cfg
目前框架公开了三个命令;/crash和 /hang用于在 开发而/get是一个自带的api密钥bing映像 搜索者。
添加插件非常简单,只需创建一个名称空间包 在telegrambot.plugins和子类下 telegrambot.plugins.base.BasePlugin(现在我想起来了, 命名空间包可能不是必需的-我将不得不重新考虑 这一点上的动机)。
插件示例
fromtelegrambot.plugins.baseimportBasePlugin# due to annoying limitations class names must be uniqueclassHelloWorld(BasePlugin):# register /hello command to 'hello_handler' methodcommands{'hello':'hello_handler'}defhello_handler(self,args,msg):# `args` is a list of the arguments after the command:# '/hello world' -> args=['world']# `msg` is a dictionary representing the Telegram `Message` object# see TelegramAPIMixin for available Telegram API functionsself.send_message('Hello, {}'.(' '.join(args)),msg['chat']['id'],reply=msg['message_id'])