Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
YangRucheng committed Jan 13, 2025
1 parent c56f286 commit 661e0cb
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include nonebot/**

exclude test/*
exclude example/*
8 changes: 8 additions & 0 deletions example/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.venv
.git
temp
data
__pycache__


.env
10 changes: 10 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Python ###
__pycache__/

# Distribution / packaging
build/
dist/

# Environments
.env
.venv
35 changes: 35 additions & 0 deletions example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ---- 构建阶段 ----
FROM docker.proxy.yangrucheng.top/python:3.12-alpine AS builder

# 安装必要的构建工具和依赖
RUN sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories \
&& apk add --no-cache gcc musl-dev libffi-dev sqlite-dev tzdata

# 安装依赖
COPY ./requirements.txt /app/requirements.txt
RUN pip config set global.index-url https://pypi.proxy.yangrucheng.top/simple \
&& pip config set global.trusted-host pypi.proxy.yangrucheng.top \
&& pip install --upgrade pip --break-system-packages \
&& pip install -r /app/requirements.txt --break-system-packages

# ---- 生产阶段 ----
FROM docker.proxy.yangrucheng.top/python:3.12-alpine

# 安装基础依赖
RUN apk add --no-cache \
tzdata

# 设置时区
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

# 设置工作目录和数据卷
WORKDIR /app

# 从构建阶段复制已安装的 Python 包
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages

# 复制应用代码
COPY . /app

# 设置启动命令
CMD ["python3", "bot.py"]
18 changes: 18 additions & 0 deletions example/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from nonebot.adapters.wxmp import Adapter as WxmpAdapter

import nonebot

# 初始化 NoneBot
nonebot.init()

# 注册适配器
driver = nonebot.get_driver()
driver.register_adapter(WxmpAdapter)

# 在这里加载插件
nonebot.load_plugins("plugins") # 本地插件

if __name__ == "__main__":
nonebot.run(
forwarded_allow_ips="*",
)
124 changes: 124 additions & 0 deletions example/plugins/公众号测试.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from nonebot import on_message, on_command
from nonebot.plugin import PluginMetadata
from nonebot.params import CommandArg
from nonebot.adapters import Message
from nonebot.matcher import Matcher
from pathlib import Path
import asyncio
import os

from nonebot.adapters.wxmp import OfficalEvent, MessageSegment, Bot, File


async def rule_test(
event: OfficalEvent
):
""" 公众号事件 """
return True


@on_command("文本", rule=rule_test).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: OfficalEvent,
):
await bot.send(
event=event,
message="测试文本(被动回复消息)")

await asyncio.sleep(10)
await bot.send(
event=event,
message="测试文本(客服消息)")


@on_command("图片", rule=rule_test).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: OfficalEvent,
):
media_id = await bot.upload_temp_media(File(file_path=Path(os.getcwd()) / "res/demo.png", file_type="image"))
await bot.send(
event=event,
message=MessageSegment.image(
media_id=media_id,
)
)

await asyncio.sleep(10)
await bot.send(
event=event,
message=MessageSegment.image(
media_id=media_id,
)
)


@on_command("音频", rule=rule_test).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: OfficalEvent,
):
media_id = await bot.upload_temp_media(File(file_path=Path(os.getcwd()) / "res/demo.mp3", file_type="voice"))
await bot.send(
event=event,
message=MessageSegment.voice(
media_id=media_id,
)
)

await asyncio.sleep(10)
await bot.send(
event=event,
message=MessageSegment.voice(
media_id=media_id,
)
)


@on_command("视频", rule=rule_test).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: OfficalEvent,
):
media_id = await bot.upload_temp_media(File(file_path=Path(os.getcwd()) / "res/demo.mp4", file_type="video"))
await bot.send(
event=event,
message=MessageSegment.video(
media_id=media_id,
title="示例视频[被动消息]",
description="这是一个示例视频",
)
)

await asyncio.sleep(10)
await bot.send(
event=event,
message=MessageSegment.video(
media_id=media_id,
title="示例视频[客服消息]",
description="这是一个示例视频",
)
)


@on_command("小程序", rule=rule_test).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: OfficalEvent,
):
media_id = await bot.upload_temp_media(File(file_path=Path(os.getcwd()) / "res/demo.png", file_type="image"))
await bot.send(
event=event,
message=MessageSegment.miniprogrampage(
title="示例小程序",
appid="wx0ba7981861be3afc",
page_path="pages/home/home",
thumb_media_id=media_id,
)
)
78 changes: 78 additions & 0 deletions example/plugins/小程序测试.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from nonebot import on_message, on_command
from nonebot.plugin import PluginMetadata
from nonebot.params import CommandArg
from nonebot.adapters import Message
from nonebot.matcher import Matcher
from pathlib import Path
import asyncio
import os

from nonebot.adapters.wxmp import MiniprogramEvent, MessageSegment, Bot, File


async def rule_test(
event: MiniprogramEvent
):
""" 小程序测试 """
return True


@on_command("文本2", rule=rule_test, priority=0).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: MiniprogramEvent,
):
await bot.send(
event=event,
message=MessageSegment.text("Hello, world!"),
)


@on_command("链接2", rule=rule_test, priority=0).handle()
async def handle(
bot: Bot,
mather: Matcher,
event: MiniprogramEvent,
):
await bot.send(
event=event,
message=MessageSegment.link(
title="Hello, world!",
description="This is a test message.",
url="https://github.com/YangRucheng/nonebot-adapter-wxmp",
thumb_url="https://nonebot.dev/logo.png",
)
)


@on_command("小程序2", rule=rule_test, priority=0).handle()
async def handle_test(
bot: Bot,
mather: Matcher,
event: MiniprogramEvent,
):
""" 测试发送消息 """
await bot.send(
event=event,
message=MessageSegment.miniprogrampage(
title="Hello, world!",
page_path="pages/home/home",
thumb_media_path=Path("/app/res/demo.png"),
)
)


@on_command("图片2", rule=rule_test, priority=0).handle()
async def handle_test(
bot: Bot,
mather: Matcher,
event: MiniprogramEvent,
):
""" 测试发送消息 """
await bot.send(
event=event,
message=MessageSegment.image(
file_path=Path("/app/res/demo.png"),
)
)
8 changes: 8 additions & 0 deletions example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
httpx[http2]
websockets
xmltodict
fastapi

nonebot2[httpx,websockets,fastapi]

nonebot-adapter-wxmp
Binary file added example/res/demo.mp3
Binary file not shown.
Binary file added example/res/demo.mp4
Binary file not shown.
Binary file added example/res/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 661e0cb

Please sign in to comment.