Pull Request: WeChat Bot Integration for Second-Me (#81)
* Create Wechat-Robot * Delete Wechat-Robot * Create requirements.txt * Create Readme.md * Create wechat_bot.py * Create .env * Delete Wechat-robot directory * Add files via upload * Update Readme (1).md * Update Readme (1).md * Create Readme.md * Delete integrate/Readme (1).md * Update Readme.md
This commit is contained in:
parent
3d0a8a776a
commit
40b3b2a162
|
@ -0,0 +1,39 @@
|
|||
# Second-Me WeChat Bot 集成指南
|
||||
|
||||
这个项目将 Second-Me 与微信机器人进行集成,让您可以通过微信与您的 Second-Me 进行交互。
|
||||
|
||||
## 功能特点
|
||||
|
||||
- 🤖 将 Second-Me 无缝接入微信
|
||||
- 💬 支持文本消息的智能回复
|
||||
- 🔄 自动化的消息处理流程
|
||||
- 📝 完整的日志记录
|
||||
- ⚠️ 健壮的错误处理机制
|
||||
|
||||
## 安装要求
|
||||
|
||||
- Python 3.8+
|
||||
- WeChat 个人账号
|
||||
- Second-Me 项目环境
|
||||
|
||||
## 快速开始
|
||||
|
||||
1. 克隆项目并安装依赖:
|
||||
```bash
|
||||
git clone https://github.com/mindverse/second-me.git
|
||||
cd second-me/integrate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
2. 配置环境:
|
||||
- 确保 Second-Me 的配置文件正确设置
|
||||
- 检查 `.env` 文件中的必要配置
|
||||
|
||||
3. 运行机器人:
|
||||
```bash
|
||||
python wechat_bot.py
|
||||
```
|
||||
|
||||
4. 首次运行时,扫描终端显示的二维码登录微信
|
||||
|
||||
## 项目结构
|
|
@ -0,0 +1,6 @@
|
|||
# Second-Me 配置
|
||||
SECOND_ME_MODEL_PATH=path/to/model
|
||||
SECOND_ME_CONFIG_PATH=path/to/config
|
||||
|
||||
# 微信机器人配置
|
||||
WXPY_BOT_CACHE_PATH=wxpy.pkl
|
|
@ -0,0 +1,5 @@
|
|||
wxpy==0.3.9.8
|
||||
python-dotenv==0.19.0
|
||||
torch>=1.8.0
|
||||
transformers>=4.5.0
|
||||
numpy>=1.19.0
|
|
@ -0,0 +1,82 @@
|
|||
from wxpy import *
|
||||
import logging
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import sys
|
||||
import json
|
||||
|
||||
# 添加Second-Me的路径到系统路径
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), 'lpm_kernel'))
|
||||
|
||||
from lpm_kernel.kernel import SecondMeKernel
|
||||
from lpm_kernel.utils import load_config
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
|
||||
class WeChatBot:
|
||||
def __init__(self):
|
||||
# 初始化机器人,设置缓存路径
|
||||
self.bot = Bot(cache_path='wxpy.pkl')
|
||||
logger.info("微信机器人初始化成功")
|
||||
|
||||
# 初始化Second-Me
|
||||
try:
|
||||
config = load_config()
|
||||
self.second_me = SecondMeKernel(config)
|
||||
logger.info("Second-Me初始化成功")
|
||||
except Exception as e:
|
||||
logger.error(f"Second-Me初始化失败: {str(e)}")
|
||||
self.second_me = None
|
||||
|
||||
def handle_message(self, msg):
|
||||
"""处理接收到的消息"""
|
||||
try:
|
||||
# 获取消息内容
|
||||
content = msg.text
|
||||
sender = msg.sender
|
||||
|
||||
# 记录接收到的消息
|
||||
logger.info(f"收到来自 {sender.name} 的消息: {content}")
|
||||
|
||||
if self.second_me is None:
|
||||
msg.reply("抱歉,Second-Me服务未正确初始化,请稍后再试。")
|
||||
return
|
||||
|
||||
# 调用Second-Me处理消息
|
||||
response = self.second_me.process_message(content)
|
||||
|
||||
# 如果响应是字典,转换为字符串
|
||||
if isinstance(response, dict):
|
||||
response = json.dumps(response, ensure_ascii=False)
|
||||
|
||||
# 发送回复
|
||||
msg.reply(response)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理消息时出错: {str(e)}")
|
||||
msg.reply("抱歉,处理消息时出现错误。")
|
||||
|
||||
def run(self):
|
||||
"""运行机器人"""
|
||||
try:
|
||||
# 注册消息处理函数
|
||||
@self.bot.register()
|
||||
def print_messages(msg):
|
||||
self.handle_message(msg)
|
||||
|
||||
# 保持运行
|
||||
self.bot.join()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"运行机器人时出错: {str(e)}")
|
||||
self.bot.logout()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 创建并运行机器人
|
||||
bot = WeChatBot()
|
||||
bot.run()
|
Loading…
Reference in New Issue