Pyton Telegram机器人的点击式命令界面工具包
telegram-click的Python项目详细描述
telegram click
请自己尝试最新版本的example.py:
><<
功能
- [X]POSIX风格的参数解析
- [X]引用的参数(
/command"hello world"
) - [X]命名参数(
/command--text"hello world"
) - [X]可选参数
- [X]类型转换,包括对自定义类型的支持
- [X]参数输入验证
- [X]引用的参数(
- [X]自动帮助消息
- [X]当命令与无效参数一起使用时,显示帮助消息
- [X]用一个方法列出所有可用的命令
- [X]权限处理
- [X]分别为每个命令设置权限
- [X]将命令执行限制为私人聊天或组管理员
- [X]使用逻辑运算符组合权限
- [X]创建自定义权限处理程序
- [X]错误处理
- [X]如果出错,自动发送错误消息
- [X]可以选择发送异常消息
电报点击由
使用希望还有其他人:)
如何使用
将此库作为依赖项安装,以便在项目中使用它。
pip install telegram-click
然后用@command
decorator注释命令处理程序函数
这个库的:
fromtelegramimportUpdatefromtelegram.extimportCallbackContextfromtelegram_click.decoratorimportcommandfromtelegram_click.argumentimportArgumentclassMyBot:[...]@command(name='start',description='Start bot interaction')def_start_command_callback(self,update:Update,context:CallbackContext):# do somethingpass@command(name='age',description='Set age',arguments=[Argument(name='age',description='The new age',type=int,validator=lambdax:x>0,example='25')])def_age_command_callback(self,update:Update,context:CallbackContext,age:int):context.bot.send_message(update.effective_chat.id,"New age: {}".format(age))
参数
电报点击根据
<人力资源部ef="https://docs.python.org/3/library/shlex.html解析规则" rel="nofollow">shlex posix规则
因此,在一般情况下,空格充当参数分隔符和带引号的参数。
被解析为单个(支持双("
)和
单引号(’
)字符。
命名
参数可以有多个名称以允许使用缩写名称。这个 为参数指定的名字将用于 回调参数名(规范化为snake case)。因为这个 建议将完整的参数名指定为第一个参数名。
类型
因为所有用户输入最初都是str
类型,所以需要有一个
如果预期类型不是astr
,则转换。对于基本类型
bool
,int
,float
和str
转换器内置于此库中。
如果要使用其他类型,则必须指定如何转换
str
使用
参数
构造函数:
fromtelegram_click.argumentimportArgumentArgument(name='age',description='The new age',type=MyType,converter=lambdax:MyType(x),validator=lambdax:x>0,example='25')
权限处理
如果命令只应在满足特定条件时才可执行
您可以使用权限
参数指定这些条件:
fromtelegramimportUpdatefromtelegram.extimportCallbackContextfromtelegram_click.decoratorimportcommandfromtelegram_click.permissionimportGROUP_ADMIN@command(name='permission',description='Needs permission',permissions=GROUP_ADMIN)def_permission_command_callback(self,update:Update,context:CallbackContext):
可以使用&;和
(非)运算符组合多个权限。
如果用户没有使用命令的权限,则不会显示该命令 当此用户生成命令列表时。
集成权限处理程序
<表><广告>私人聊天
正常的群聊
超级群聊
群聊
用户id
用户名
组创建者
组管理员
无人
任何人
自定义权限处理程序
如果没有一个集成的处理程序适合您的需要,您可以简单地编写
通过扩展 默认情况下,来自未经许可的用户的命令调用将被忽略。
如果你想给他们发送一条类似"拒绝许可"的信息,你可以
将此消息传递给
telegram支持 当使用 要控制此行为,请指定 您可以使用
以上示例。 电报点击在大多数情况下自动处理错误。 当出现异常时,将通知用户
命令已使服务器崩溃。默认情况下他只能看到
一般性错误消息。如果要发送完整的堆栈跟踪
相反,将 还将通知用户输入错误,如 github是用于社会编码的:如果您想编写代码,我鼓励通过forks的pull请求进行贡献
关于这个仓库。为bug和新功能创建github票证,并对您感兴趣的功能进行评论。权限
基类,您自己的权限处理程序
并将mypermission
类的实例传递到权限列表中
fromtelegramimportUpdatefromtelegram.extimportCallbackContextfromtelegram_click.decoratorimportcommandfromtelegram_click.permission.baseimportPermissionfromtelegram_click.permissionimportGROUP_ADMINclassMyPermission(Permission):defevaluate(self,update:Update,context:CallbackContext)->bool:from_user=update.effective_message.from_userreturnfrom_user.idin[12345,32435]@command(name='permission',description='Needs permission',permissions=MyPermission()&GROUP_ADMIN)def_permission_command_callback(self,update:Update,context:CallbackContext):
显示"权限被拒绝"消息
@命令
装饰器。目标命令
@
符号,用于特定bot上的目标命令
用户名:/start # unspecified
/start@myAwesomeBot # targeted at self
/start@someOtherBot # targeted at other bot
messagehandler而不是
commandhandler时
甚至可以捕获针对其他机器人的命令。
默认情况下,只有没有目标的消息和目标的消息
直接在您的机器人上进行处理。
command\u target
参数:fromtelegramimportUpdatefromtelegram.extimportCallbackContextfromtelegram_click.decoratorimportcommandfromtelegram_clickimportCommandTargetfromtelegram_click.permissionimportNOBODY@command(name="commands",description="List commands supported by this bot.",permissions=NOBODY,command_target=CommandTarget.UNSPECIFIED|CommandTarget.SELF)def_unknown_command_callback(self,update:Update,context:CallbackContext):
错误处理
print_error
参数设置为true
贡献
许可证
telegram-click
Copyright (c) 2019 Markus Ressel
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 shall be included in all
copies or substantial portions of the Software.
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.
推荐PyPI第三方库