基于各种触发器的机器人自动响应平台
gramhopper的Python项目详细描述
格拉姆霍珀
基于各种触发器的自动响应的机器人平台。
安装
使用pip
安装最新版本:
$ pip install gramhopper
使用Docker
拉取最新图像:
$ docker pull orbin/gramhopper:latest
配置
创建机器人
- Create a bot using BotFather。
- 将接收到的令牌保存在
~/.gramphopper/token.txt
。 - 通过向botfather发送
/setprivacy
,启用从bot的组接收消息, 然后选择新的bot并单击Disable
。
为基于用户的规则启用昵称
要使用用户昵称,必须先获取用户ID。
在电报中获取用户ID
使用@userinfobot:
- 转到@userinfobot并按“开始”。 然后机器人将打印您的用户ID。
- 要获取另一个用户的用户ID,只需转发来自该用户的消息,机器人就会打印其用户ID。
将昵称映射到用户ID
要使用用户昵称,~/.gramphopper/users.json
应该包含一个对象,其中键
是昵称,值是用户ID,例如:
{"nickname1":123456789,"nickname2":987654321}
规则配置
配置文件位于~/.gramphopper/rules.yml
。
基本上,配置文件有三个部分:
- global triggers:可以在规则中使用而无需重新定义的触发器。
- global responses:可以在规则中使用而无需重新定义的响应。
- 规则:触发器和响应对,它们共同定义了触发bot的内容和它的响应方式。
rules.yml
示例
以下配置文件:
triggers:-name:cattype:text.has_exact_wordword:-Meow-meow-name:ducktype:text.has_exact_wordword:Quackresponses:-name:found_dogtype:preset.replypreset_response:-Hey, here's a dog!rules:# Identifies a cat (a global "cat" trigger) and sends "Hello cat!" (an inline response).-trigger:catresponse:type:preset.messagepreset_response:Hello cat!# Identifies a dog (an inline trigger) and replies "Hey, here's a dog!"# (a global "found_dog" response).-trigger:type:text.has_substringsubstring:-Woof-woof-Ruff-ruffresponse:found_dog# Identifies an animal sound (an inline trigger) and replies # 'I hear "(the animal sound)"' (an inline response), with a 30% probability.-trigger:type:text.regexppattern:^(Quack|Meow|Woof|Moo)$response:type:match.messagetemplate:I hear "{0}"probability:0.3# Identifies 5 occurrences in 60 seconds of a duck sound (an inline event_streak# trigger that uses a global "duck" trigger") and replies "Shut up duck!".-trigger:type:event_streakcounting_event_trigger:duckstreak_timeout_sec:60event_count:5response:type:preset.messagepreset_response:Shut up duck!
将产生以下对话:
运行
要运行gramhopper,只需运行: