矩阵的基本bot
python-matrixbot的Python项目详细描述
python矩阵boot
一个python模块,用作Matrixbot的基类。
兼容性
Python3.6+
安装
pip install python-matrixbot
用法
matrixbot类将连接到matrix服务器,在每个连接上启动一个侦听器
房间,并收听来自其他用户的房间邀请。它还包括
可用于扩展功能。它建立在
Matrix Python SDK可以是
通过MatrixBot.client
from matrixbot import MatrixBot
# create a new bot
bot = MatrixBot(
host="example.com",
display_name="Example Bot",
token=access_token,
user_id="@bot:example.com"
)
# run forever
bot.start()
获取加入的房间列表:
room_ids = bot.rooms.keys()
room_objects = bot.rooms.values()
离开废弃的一对一房间
for room in bot.rooms.values():
bot.leave_if_empty(room)
外部api调用的缓存结果
为了防止滥用依赖外部api调用的命令,matrixbot包含
非常基本的缓存机制。在MatrixBot.fetch()
中包装所有api调用以返回
缓存结果并自动刷新过时的数据。
def get_url(url):
return response.get(url)
result = bot.fetch(
"cache_key", lambda: get_url("https://example.com"), timeout=900
)
默认情况下,模块使用在重新启动之间丢失的内存缓存。创造 持久缓存为构造函数提供指向sqlite数据库的路径:
bot = MatrixBot(
...
cache_db="/path/to/sqlite_database"
)
次级分类
创建子类以扩展和覆盖默认功能
class MyBot(MatrixBot):
# override `process_message()` to filter/parse/react to room events:
def process_message(self, room, event):
# say hello when a user joins the room
if event["type"] == "m.room.member":
if event["content"]["membership"] == "join":
self.say(room, "Hello!", mention=event["sender"])
# override `accept_invite()` to disable auto-join:
def accept_invite(self, room_id):
pass
bot = MyBot(...)
许可证
麻省理工学院
维护人员
欢迎投稿。
关于胡言乱语
giberfish,inc是一个志愿者非营利组织,致力于开发免费的在线推广工具。 活动人士和普通人的隐私。如果你喜欢这个模块并决定 在你的项目中使用它,请考虑捐赠1美元来帮助我们继续前进。谢谢!
https://gibberfish.org/donate/