欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python使用itcaht库实现微信自动收发消息功能

时间:2021-08-13 07:36:29|栏目:Python代码|点击:

itchat库

  • 模拟微信网页登录
  • 通过python code接受/发送微信消息
  • 实现微信聊天机器人:调用聊天机器人api,将接收到的微信消息传给api,再将api返回的消息传给微信

展示如何使用itchat发送微信消息

# !pip install itchat
import itchat

在当前文件夹下生成二维码图片,微信扫码即可登录网页版微信

itchat.auto_login()

Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 小白

通过微信名称查找接收消息的对象

users = itchat.search_friends(name="小白") # 这里是给自己发送消息
user_name = users[0]["UserName"]

对象表示成这样的一个字符串

user_name

'@40f90812b2233588e8ebda1e8d8f01d48d32a384e31f0a96yaa4859a41123456'

toUserName指定接受消息的对象;每隔10s发送一次"hello world",发送3次

import time

for i in range(3):
 itchat.send("hello, world", toUserName=user_name)
 time.sleep(10)

总结

上一篇:python打开文件并获取文件相关属性的方法

栏    目:Python代码

下一篇:Python切片操作去除字符串首尾的空格

本文标题:Python使用itcaht库实现微信自动收发消息功能

本文地址:http://www.codeinn.net/misctech/166555.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有