微信 AI 机器人是基于人工智能技术的自动化服务系统,具备自然语言处理能力,可理解用户输入并给出响应或执行任务。它能提供 24 小时实时服务,节省搜索时间,提高社群管理效率。
一、申请 API Key
- 访问千帆大模型官网,在模型广场搜索 ERNIE-Lite(免费),点击体验。

- 点击应用接入,填入必填项,点击创建(需实名认证)。


- 获取 API Key 和 Secret Key。

二、代码实现
注:运行代码前请登录 PC 端微信,否则可能运行失败。
完整代码
import requests
import json
import csv
import os
import schedule
import time
from wxauto import WeChat
class WeChatBot:
def __init__(self):
self.wx = WeChat()
self.list_name = [
'徐长卿', # 机器人账号添加管理员的备注,ros 为机器人,ros 给管理员的备注就应为徐长卿,可进行修改
'嘻嘻嘻',
,
]
group .list_name:
.wx.AddListenChat(who=group, savepic=)
():
url =
payload = json.dumps()
headers = {
: ,
:
}
response = requests.post(url, headers=headers, data=payload)
response.status_code == :
response.json().get()
:
(, response.text)
():
access_token = .get_access_token()
access_token:
url =
payload = json.dumps({
: [
{ : , : content }
]
})
headers = {
:
}
response = requests.post(url, headers=headers, data=payload)
response.status_code == :
response.json().get()
:
(, response.text)
:
()
():
good_morning_message =
ai_reply = .call_ai_model(good_morning_message)
ai_reply:
group .list_name:
.wx.SendMsg(msg=ai_reply, who=group)
():
evening_greetings =
group .list_name:
.wx.SendMsg(msg=evening_greetings, who=group)
():
wait =
:
msgs = .wx.GetListenMessage()
chat, messages msgs.items():
who = chat.who
who .list_name:
msg messages:
msg_type = msg.
content = msg.content
()
sender = msg.sender
who != .list_name[]:
content.startswith():
:
parts = content.split()[].strip()
commodity_name, quantity = parts.split(, )
.is_commodity_exists(commodity_name):
reply =
.save_to_csv_order(sender, commodity_name, quantity)
.wx.SendMsg(msg=reply, who=who)
:
reply =
.wx.SendMsg(msg=reply, who=who)
ValueError:
reply =
.wx.SendMsg(msg=reply, who=who)
content.startswith():
parts = content.split()[].strip()
parts:
commodity_name = parts
.is_commodity_exists(commodity_name):
price_info = .get_price_info(commodity_name)
reply =
:
reply =
.wx.SendMsg(msg=reply, who=who)
content.startswith():
ai_reply = .call_ai_model(content[:])
ai_reply:
.wx.SendMsg(msg=ai_reply, who=who)
who == .list_name[]:
content.startswith():
new_content = content[:].strip()
.forward_message(new_content)
content.startswith():
:
parts = content.split()[].strip()
parts:
commodity_name, price_info = parts.split(, )
.is_commodity_exists(commodity_name):
reply =
:
reply =
.save_to_csv(commodity_name, price_info)
.wx.SendMsg(msg=reply, who=who)
ValueError:
reply =
.wx.SendMsg(msg=reply, who=who)
content.startswith():
commodity_name = content.split()[].strip()
.delete_commodity_from_csv(commodity_name):
reply =
:
reply =
.wx.SendMsg(msg=reply, who=who)
schedule.run_pending()
time.sleep(wait)
():
group .list_name:
group != .list_name[]:
.wx.SendMsg(msg=content, who=group)
():
(, , encoding=) file:
writer = csv.writer(file)
writer.writerow([username, commodity_name, quantity])
()
():
(, , encoding=) file:
reader = csv.reader(file)
row reader:
row row[] == commodity_name:
row[]
():
(, , encoding=) file:
writer = csv.writer(file)
writer.writerow([commodity_name, price_info])
()
():
os.path.exists():
(, , encoding=) file:
reader = csv.reader(file)
row reader:
row row[] == commodity_name:
():
lines = []
found =
(, , encoding=) file:
reader = csv.reader(file)
row reader:
row row[] != commodity_name:
lines.append(row)
row:
found =
(, , encoding=) file:
writer = csv.writer(file)
writer.writerows(lines)
found
():
schedule.every().day.at().do(.send_morning_wishes)
schedule.every().day.at().do(.send_evening_greetings)
.listen_messages()
__name__ == :
bot = WeChatBot()
bot.start()


