itchat 微信接口不能直接发消息给指定用户

很久没有用itchat,最近有需求需要用到,所以把以前用的代码进行复用。

    itchat.auto_login(hotReload=True)
    
    itchat.send(content,toUserName='filehelper')
    account=itchat.get_friends('user1')
    print account
    itchat.send(content, toUserName='user1')


结果是用户user1没有收到消息。而filehelper却可以收到用户消息


折腾了一番,去最新的官方更新了看了下,现在只能支持用user1的数字序列进行发送消息。


比如 上面的account中,里面有一个Username @ 一串数字的


<User: {u'UserName': u'@c49f3dd811957bd5ed998410d74560716cc0511e6a861c048adf387aa5c8577c', u'City': u'\u6df1\u5733', u'DisplayName': u'', u'UniFriend': 0, 'MemberList': <ContactList: []>, u'PYQuanPin': u'junge', u'RemarkPYInitial': u'', u'Uin': 0, u'AppAccountFlag': 0, u'VerifyFlag': 0, u'Province': u'\u5e7f\u4e1c', u'KeyWord': u'', u'RemarkName': u'', u'PYInitial': u'JG', u'ChatRoomId': 0, u'IsOwner': 0, u'HideInputBarFlag': 0, u'EncryChatRoomId': u'', u'AttrStatus': 236325, u'SnsFlag': 1, u'MemberCount': 0, u'OwnerUin': 0, u'Alias': u'', u'Signature': u'\u5988\u5988\uff0c\u6211\u6c38\u8fdc\u6000\u5ff5\u60a8\uff0c\u6c38\u8fdc\u6d3b\u5728\u6211\u5fc3\u4e2d\uff0c\u795d\u60a8\u5728\u5929\u5802\u5b89\u597d', u'ContactFlag': 3, u'NickName': u'\u541b\u54e5', u'RemarkPYQuanPin': u'', u'HeadImgUrl': u'/cgi-bin/mmwebwx-bin/webwxgeticon?seq=641471480&username=@c49f3dd811957bd5ed998410d74560716cc0511e6a861c048adf387aa5c8577c&skey=@crypt_7801355c_17bd503bf8c1b2f92998687691a9c156', u'Sex': 1, u'StarFriend': 0, u'Statues': 0}>,


那么上面的数字就是用户的数字序列,你只要把

itchat.send(content, toUserName='user1')

中的user1改为@c49f3dd811957bd5ed998410d74560716cc0511e6a861c048adf387aa5c8577c 就可以了。

记得@不能漏。



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以使用微信官方提供的接口实现给指定微信好友发送消息,具体步骤如下: 1. 首先,需要在微信公众平台上申请开发者账号,并创建一个公众号。 2. 在公众号后台,开启开发者模式,并获取相应的 AppID 和 AppSecret。 3. 通过微信官方提供的 API,获取 access_token,用于发送消息和其他操作。 4. 调用微信官方提供的接口,向指定好友发送消息。 以下是一个简单的 Python 示例代码: ```python import requests # 获取 access_token def get_access_token(app_id, app_secret): url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={app_id}&secret={app_secret}" response = requests.get(url) access_token = response.json().get("access_token") return access_token # 发送消息 def send_message(access_token, to_user, content): url = f"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={access_token}" data = { "touser": to_user, "msgtype": "text", "text": { "content": content } } response = requests.post(url, json=data) result = response.json() if result.get("errcode") == 0: print("消息发送成功!") else: print("消息发送失败!") if __name__ == "__main__": # 替换成自己的 AppID 和 AppSecret app_id = "your_app_id" app_secret = "your_app_secret" # 替换成自己的 access_token access_token = get_access_token(app_id, app_secret) # 替换成自己的好友 openid 和消息内容 to_user = "your_friend_openid" content = "Hello, World!" # 发送消息 send_message(access_token, to_user, content) ``` 需要注意的是,由于微信官方对接口的访问频率有限制,建议在发送消息之前,先检查当前 access_token 是否有效,如果无效,则重新获取。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值