Pywayne Lark Bot
飞书机器人模块,提供完整飞书 API 交互功能,包括消息发送、文件管理、用户/群组查询和消息监听。
Quick Start
python
from pywayne.lark_bot import LarkBot, TextContent
初始化
bot = LarkBot(app
id=yourapp
id, appsecret=your
appsecret)
发送文本消息到用户
bot.send
textto
user(useropen
id=userxxx, text=Hello!)
发送文本消息到群聊
bot.send
textto
chat(chatid=oc_xxx, text=Group message)
TextContent - 文本格式化工具
创建各种文本格式,用于发送带格式的文本消息。
Mentions
python
@所有人
at
all = TextContent.makeat
allpattern()
@指定用户
at
user = TextContent.makeat
someonepattern(user
openid=user
xxx, username=张三, idtype=open_id)
Text Styles
python
加粗、斜体、下划线、删除线
bold = TextContent.make
boldpattern(粗体)
italic = TextContent.make
italianpattern(斜体)
underline = TextContent.make
underlinepattern(下划线)
strikethrough = TextContent.make
deleteline_pattern(删除线)
超链接
link = TextContent.make
urlpattern(https://example.com, 点击访问)
LarkBot - 消息发送与查询
推荐路径:发送 Markdown(优先使用)
优先使用 sendmarkdownto_chat,它会自动处理大文本分包,并支持两种路由:
- - prefer=cardv2(默认):发送 schema 2.0 interactive 卡片,适合绝大多数 Markdown 场景
- prefer=post:发送 post 富文本;可设置 tablefallback=code_block 将 Markdown 表格稳定降级
python
md =
发布说明
测试中 |
默认走 card_v2(推荐)
bot.send
markdownto_chat(
chat
id=ocxxx,
md_text=md,
title=版本进度,
prefer=card_v2
)
需要 post 时切换路由
bot.send
markdownto_chat(
chat
id=ocxxx,
md_text=md,
title=版本进度,
prefer=post,
table
fallback=codeblock
)
发送消息
Text Message
python
bot.sendtexttouser(useropen_id, 私聊消息)
bot.sendtexttochat(chatid, 群聊消息)
Image Message
python
上传图片
image
key = bot.uploadimage(/path/to/image.jpg)
发送图片
bot.send
imageto
user(useropen
id, imagekey)
bot.send
imageto
chat(chatid, image_key)
Audio / Media / File Message
python
上传文件
file
key = bot.uploadfile(/path/to/file.pdf, file_type=pdf)
发送音频
bot.send
audioto
user(useropen
id, filekey)
发送多媒体
bot.send
mediato
chat(chatid, file_key)
发送文件
bot.send
fileto
user(useropen
id, filekey)
Post (Rich Text) Message
python
from pywayne.lark_bot import PostContent
post = PostContent(title=富文本标题)
添加内容
line = post.make
textcontent(这是粗体文本, styles=[bold])
post.add
contentin
newline(line)
发送
bot.send
postto
user(useropen
id, post.getcontent())
bot.send
postto
chat(chatid, post.get_content())
Interactive Card Message
python
直接传原始 interactive 卡片
card = {
header: {title: {content: 卡片标题, tag: plain_text}},
elements: [...]
}
bot.send
interactiveto
user(useropen_id, card)
bot.send
interactiveto
chat(chatid, card)
CardContentV2(schema 2.0 卡片构造器)
python
from pywayne.lark_bot import CardContentV2
card = CardContentV2(title=日报, template=blue)
card.add_markdown(# 今日进展\n\n- 完成接口联调\n- 修复2个问题)
card.add_hr()
card.addimage(imgkey=img_xxx)
bot.sendinteractivetochat(chatid, card.get_card())
Share Message
python
分享群聊
bot.send
sharedchat
touser(user
openid, shared
chatid)
bot.send
sharedchat
tochat(chat
id, sharedchat_id)
分享用户
bot.send
shareduser
touser(user
openid, shared
userid)
bot.send
shareduser
tochat(chat
id, shareduser_id)
PostContent - 富文本构建器
python
post = PostContent(title=标题)
可用内容类型
text = post.make
textcontent(文本, styles=[bold, underline])
link = post.make
linkcontent(链接文字, https://example.com)
at = post.make
atcontent(user
openid)
img = post.make
imagecontent(image
key=imgxxx)
media = post.make
mediacontent(file
key=filexxx, image
key=thumbxxx)
emoji = post.make
emojicontent(emoji_type=OK)
hr = post.make
hrcontent()
code
block = post.makecode
blockcontent(language=python, text=print(hello))
markdown = post.make
markdowncontent(
Markdown)
添加内容
post.add
contentin
newline(text)
post.add
contentsin_line([link, at]) # 同一行添加多个元素
python
推荐:直接添加 markdown,支持分块与表格降级
md =
迭代计划
doing |
post.addmarkdown(md, tableas=codeblock, maxchunk_bytes=8000)
文件操作
python
上传
image
key = bot.uploadimage(/path/to/image.jpg)
file
key = bot.uploadfile(/path/to/file.pdf, file_type=pdf)
下载
bot.download
image(imagekey, /save/path/image.jpg)
bot.download
file(filekey, /save/path/file.pdf)
下载消息中的所有资源
resources = bot.download
messageresources(
message
id=msgxxx,
message
content={imagekey:img_xxx},
save_dir=/save/dir
)
用户与群组查询
python
获取用户信息
users = bot.get
userinfo(emails=[test@example.com], mobiles=[13800138000])
获取群组列表
groups = bot.get
grouplist()
通过群名获取群ID
chat
ids = bot.getgroup
chatid
byname(项目讨论组)
获取群成员
members = bot.get
membersin
groupby
groupchat
id(chatid)
通过群成员名获取 open_id
member
ids = bot.getmember
openid
byname(chat_id, 张三)
获取群名和用户名
chat
name, username = bot.get
chatand
username(chat
id, userid)
LarkBotListener - 消息监听
飞书消息监听器,用于实时接收和处理消息。
python
from pywayne.larkbotlistener import LarkBotListener
from pathlib import Path
listener = LarkBotListener(
appid=yourapp_id,
appsecret=yourapp_secret
)
文本消息处理
python
@listener.texthandler(grouponly=False, user_only=False)
async def handletext(text: str, chatid: str, isgroup: bool, groupname: str, user_name: str):
print(f收到来自 {user_name} 的消息: {text})
# 回复
listener.sendmessage(chatid, f已收到:{text})
图片消息处理
python
@listener.imagehandler(grouponly=True)
async def handleimage(imagepath: Path, chatid: str, username: str):
print(f收到图片: {image_path})
# 处理图片...
文件消息处理
python
@listener.file_handler()
async def handlefile(filepath: Path, chatid: str, username: str):
print(f收到文件: {file_path})
# 处理文件...
通用消息监听
python
@listener.listen(message_type=post)
async def handle_post(ctx):
print(f收到富文本消息: {ctx.content})
启动监听
python
listener.run()
监听器参数说明
Handler