代码语言:javascript复制
# 导入itchat库和time库,以及itchat库中的一些常量
import itchat, time
from itchat.content import *
# 当收到文本、地图、名片、通知和分享消息时,执行以下函数
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
# 回复发送者消息类型和内容
msg.user.send('%s: %s' % (msg.type, msg.text))
# 当收到图片、语音、文件和视频消息时,执行以下函数
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
# 下载消息中的文件
msg.download(msg.fileName)
# 根据消息类型设置回复的符号
typeSymbol = {
PICTURE: 'img',
VIDEO: 'vid', }.get(msg.type, 'fil')
# 回复发送者包含文件的消息
return '@%s@%s' % (typeSymbol, msg.fileName)
# 当收到好友请求时,执行以下函数
@itchat.msg_register(FRIENDS)
def add_friend(msg):
# 接受好友请求
msg.user.verify()
# 向新好友发送问候消息
msg.user.send('Nice to meet you!')
# 当收到群聊中的文本消息时,执行以下函数
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
# 如果消息中@了机器人
if msg.isAt:
# 回复发送者被@时收到的消息内容
msg.user.send(u'@%su2005I received: %s' % (
msg.actualNickName, msg.text))
# 登录微信网页版,参数为True表示使用命令行显示登录二维码
# itchat.auto_login(True)
itchat.auto_login()
# 运行itchat,保持在线状态
itchat.run(True)
# itchat.run()
相关链接
代码语言:javascript复制itchat2023.5.4
文档
https://itchat.readthedocs.io/zh/latest/
标记: 红色
import itchat
itchat.auto_login()
标记: 任务完成
itchat.send('。。。。。', toUserName='filehelper')
github
https://github.com/why2lyj/ItChat-UOS
https://app.gitter.im/#/room/#littlecodersh_ItChat:gitter.im
https://gist.github.com/littlecodersh/ec8ddab12364323c97d4e36459174f0d
https://github.com/littlecodersh/itchatmp
itchatmp是一个开源的微信公众号、企业号接口,使用python调用微信公众号从未如此简单。
python-wechaty
https://github.com/wechaty/python-wechaty
DDR爱好者之家
Python微信库:itchat的用法详解
http://ddrfans.com/Html/1/191205.html
python使用itchat库实现微信机器人(好友聊天、群聊天)
http://ddrfans.com/Html/1/189973.html
星球
https://public.zsxq.com/groups/88885848842852.html
CSDN
30行python代码实现微信自动陪女盆友聊天(itchat-uos 无限制调用)
https://blog.csdn.net/python0737/article/details/128323347?ops_request_misc=%7B%22request%5Fid%22%3A%22168318506216800225538621%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fblog.%22%7D&request_id=168318506216800225538621&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-3-128323347-null-null.blog_rank_default&utm_term=itchat-uos&spm=1018.2226.3001.4450
17行代码用python对接openai的api实现chatgpt微信对话机器人
https://blog.csdn.net/qq_39653816/article/details/129784218
import itchat
import openai
from itchat.content import TEXT
# 将你的OpenAI API密钥填入这里
openai.api_key = "这里填入你自己的key"
# 定义一个函数,将问题传给OpenAI的GPT-3.5-turbo模型,并获得回答
def askbot(question):
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": question}
]
)
return completion.choices[0].message.content
# 注册文本消息处理函数,收到文本消息后将其发送给askbot函数,并将回答发送回给发送者
@itchat.msg_register(TEXT)
def wxreply(msg):
itchat.send(askbot(msg.text), toUserName=msg.FromUserName)
# 以命令行二维码的形式登录微信
itchat.login(enableCmdQR=2)
标记: 任务完成
# 开始运行微信客户端,接收并处理消息
itchat.run()
如何用Python保存语音、图片、视频等信息转发给好友
https://blog.csdn.net/shunshunss/article/details/122262317?ops_request_misc=%7B%22request%5Fid%22%3A%22168318506216800225538621%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fblog.%22%7D&request_id=168318506216800225538621&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-4-122262317-null-null.blog_rank_default&utm_term=itchat-uos&spm=1018.2226.3001.4450
python微信机器人制作教程 源码
https://blog.csdn.net/a2875254060/article/details/122186253?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-1-122186253-blog-128359055.235^v32^pc_relevant_default_base3&spm=1001.2101.3001.4242.2&utm_relevant_index=4
智能聊天机器人的实现
https://blog.csdn.net/zhayujie5200/article/details/128359055?ops_request_misc=%7B%22request%5Fid%22%3A%22168317267616800222872616%22%2C%22scm%22%3A%2220140713.130102334..%22%7D&request_id=168317267616800222872616&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-128359055-null-null.142^v86^control,239^v2^insert_chatgpt&utm_term=itchat-uos&spm=1018.2226.3001.4187
Py的itchat库:python开发神器之itchat的介绍、安装、使用方法详解
https://blog.csdn.net/qq_37934722/article/details/130037171
代码
import itchat
import json
from itchat.content import *
from channel.channel import Channel
from concurrent.futures import ThreadPoolExecutor
from common.log import logger
from common.tmp_dir import TmpDir
from config import conf
import requests
import io
# 创建一个线程池,最大工作线程数为8
thread_pool = ThreadPoolExecutor(max_workers=8)
# 注册文本消息处理函数
@itchat.msg_register(TEXT)
def handler_single_msg(msg):
WechatChannel().handle_text(msg)
return None
# 注册群聊文本消息处理函数
@itchat.msg_register(TEXT, isGroupChat=True)
def handler_group_msg(msg):
WechatChannel().handle_group(msg)
return None
# 注册语音消息处理函数
@itchat.msg_register(VOICE)
def handler_single_voice(msg):
WechatChannel().handle_voice(msg)
return None
# 微信消息通道类
class WechatChannel(Channel):
def __init__(self):
pass
# 启动微信客户端
def startup(self):
# 通过扫描二维码登录
itchat.auto_login(enableCmdQR=2)
# 开始监听消息
itchat.run()
# 处理语音消息
def handle_voice(self, msg):
if conf().get('speech_recognition') != True :
return
logger.debug("[WX]receive voice msg: " msg['FileName'])
thread_pool.submit(self._do_handle_voice, msg)
# 处理语音消息的具体实现
def _do_handle_voice(self, msg):
from_user_id = msg['FromUserName']
other_user_id = msg['User']['UserName']
if from_user_id == other_user_id:
file_name = TmpDir().path() msg['FileName']
msg.download(file_name)
query = super().build_voice_to_text(file_name)
if conf().get('voice_reply_voice'):
self._do_send_voice(query, from_user_id)
else:
self._do_send_text(query, from_user_id)
# 处理文本消息
def handle_text(self, msg):
logger.debug("[WX]receive text msg: " json.dumps(msg, ensure_ascii=False))
content = msg['Text']
self._handle_single_msg(msg, content)
# 处理单人文本消息的具体实现
def _handle_single_msg(self, msg, content):
from_user_id = msg['FromUserName']
to_user_id = msg['ToUserName'] # 接收人id
other_user_id = msg['User']['UserName'] # 对手方id
match_prefix = self.check_prefix(content, conf().get('single_chat_prefix'))
if "」n- - - - - - - - - - - - - - -" in content:
logger.debug("[WX]reference query skipped")
return
if from_user_id == other_user_id and match_prefix is not None:
# 好友向自己发送消息
if match_prefix != '':
str_list = content.split(match_prefix, 1)
if len(str_list) == 2:
content = str_list[1].strip()
img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
if img_match_prefix:
content = content.split(img_match_prefix, 1)[1].strip()
thread_pool.submit(self._do_send_img, content, from_user
将上面的代码修改:
1、将程序修改为可以运行的程序
修改下面的代码:1、取消语音和图片功能。2、接收到的消息print出来显示。3、添加if __name__ == '__main__':
WechatChannel().startup()等使他可以直接运行