Skip to content

Instantly share code, notes, and snippets.

@littlecodersh
Last active March 26, 2024 12:33
Show Gist options
  • Save littlecodersh/ec8ddab12364323c97d4e36459174f0d to your computer and use it in GitHub Desktop.
Save littlecodersh/ec8ddab12364323c97d4e36459174f0d to your computer and use it in GitHub Desktop.
Main script behind itchat robot
#coding=utf8
import itchat
# tuling plugin can be get here:
# https://github.com/littlecodersh/EasierLife/tree/master/Plugins/Tuling
from tuling import get_response
@itchat.msg_register('Text')
def text_reply(msg):
if u'作者' in msg['Text'] or u'主人' in msg['Text']:
return u'你可以在这里了解他:https://github.com/littlecodersh'
elif u'源代码' in msg['Text'] or u'获取文件' in msg['Text']:
itchat.send('@fil@main.py', msg['FromUserName'])
return u'这就是现在机器人后台的代码,是不是很简单呢?'
elif u'获取图片' in msg['Text']:
itchat.send('@img@applaud.gif', msg['FromUserName']) # there should be a picture
else:
return get_response(msg['Text']) or u'收到:' + msg['Text']
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def atta_reply(msg):
return ({ 'Picture': u'图片', 'Recording': u'录音',
'Attachment': u'附件', 'Video': u'视频', }.get(msg['Type']) +
u'已下载到本地') # download function is: msg['Text'](msg['FileName'])
@itchat.msg_register(['Map', 'Card', 'Note', 'Sharing'])
def mm_reply(msg):
if msg['Type'] == 'Map':
return u'收到位置分享'
elif msg['Type'] == 'Sharing':
return u'收到分享' + msg['Text']
elif msg['Type'] == 'Note':
return u'收到:' + msg['Text']
elif msg['Type'] == 'Card':
return u'收到好友信息:' + msg['Text']['Alias']
@itchat.msg_register('Text', isGroupChat = True)
def group_reply(msg):
if msg['isAt']:
return u'@%s\u2005%s' % (msg['ActualNickName'],
get_response(msg['Text']) or u'收到:' + msg['Text'])
@itchat.msg_register('Friends')
def add_friend(msg):
itchat.add_friend(**msg['Text'])
itchat.send_msg(u'项目主页:github.com/littlecodersh/ItChat\n'
+ u'源代码 :回复源代码\n' + u'图片获取:回复获取图片\n'
+ u'欢迎Star我的项目关注更新!', msg['RecommendInfo']['UserName'])
itchat.auto_login(True, enableCmdQR=True)
itchat.run()
@samzong
Copy link

samzong commented Sep 15, 2021

1203为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:https://pc.weixin.qq.com Mac微信下载地址:https://mac.weixin.qq.com

大佬们这个怎么解决啊?

集成下 itchat_uos

pip install itchat_uos

@XYKaiDream
Copy link

新注册的号,装了itchat_uos后依然登陆不上去KeyError: 'wxsid'

@hyh19962008
Copy link

扫码之后显示

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    itchat.auto_login()
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/register.py", line 36, in auto_login
    loginCallback=loginCallback, exitCallback=exitCallback)
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/login.py", line 55, in login
    status = self.check_login()
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/login.py", line 141, in check_login
    if process_login_info(self, r.text):
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/login.py", line 183, in process_login_info
    core.loginInfo['wxsid'] = core.loginInfo['BaseRequest']['Sid'] = cookies["wxsid"]
KeyError: 'wxsid'

@muchun0
Copy link

muchun0 commented Oct 14, 2022

无法扫码登陆,在循环下载二维码。尝试了上边两位大佬提供的添加参数【itchat.auto_login(hotReload=True) 扫码不过的可以热启动】和【二维码扫不上的,改成itchat.auto_login(True, enableCmdQR=2)后可以了】均未解决

@sereinwong33
Copy link

扫码之后没动静,没有网页啊

@daicoolb
Copy link

daicoolb commented Dec 12, 2022 via email

@mimo-x
Copy link

mimo-x commented Jan 10, 2023

扫码之后显示

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    itchat.auto_login()
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/register.py", line 36, in auto_login
    loginCallback=loginCallback, exitCallback=exitCallback)
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/login.py", line 55, in login
    status = self.check_login()
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/login.py", line 141, in check_login
    if process_login_info(self, r.text):
  File "/home/demon/.local/lib/python3.6/site-packages/itchat/components/login.py", line 183, in process_login_info
    core.loginInfo['wxsid'] = core.loginInfo['BaseRequest']['Sid'] = cookies["wxsid"]
KeyError: 'wxsid'

了解下 https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs

4.登录报错 KeyError:'wxsid'
login.py", line 183, in process_login_info
    core.loginInfo['wxsid'] = core.loginInfo['BaseRequest']['Sid'] = cookies["wxsid"]
KeyError: 'wxsid'
一般原因为使用了itchat且无法登录网页版微信,解决方法是先卸载itchat,然后安装itchat-uos 1.5.0.dev0版本。

@daicoolb
Copy link

daicoolb commented Jan 10, 2023 via email

@pjxxcc
Copy link

pjxxcc commented Mar 29, 2023

tuling 库里面根本没有 get_response 这个模块呀

@daicoolb
Copy link

daicoolb commented Mar 29, 2023 via email

@Boy-Nine
Copy link

无法扫码登陆,在循环下载二维码。尝试了上边两位大佬提供的添加参数【itchat.auto_login(hotReload=True) 扫码不过的可以热启动】和【二维码扫不上的,改成itchat.auto_login(True, enableCmdQR=2)后可以了】均未解决

+1

@Boy-Nine
Copy link

Boy-Nine commented Mar 29, 2023

登录报错 KeyError:'wxsid'

login.py", line 183, in process_login_info
core.loginInfo['wxsid'] = core.loginInfo['BaseRequest']['Sid'] = cookies["wxsid"]
KeyError: 'wxsid'

  • 一般原因为使用了itchat且无法登录网页版微信,解决方法是先卸载itchat,然后安装itchat-uos 1.5.0.dev0版本。
  • 卸载 itchat-uos -> pip uninstall itchat-uos (ps:如果没有安装,可以不用卸载)
  • 安装:itchat-uos 1.5.0.dev0 -> pip install itchat-uos==1.5.0.dev0

安装完后 执行一下代码,重复扫码大概5-7次左右

itchat.auto_login(loginCallback=True, exitCallback=True,enableCmdQR=2,hotReload=True)

扫码完-出现下面这段话 则证明成功,等待一会就好了

Loading the contact, this may take a little while.

@Jiangyan-WANG
Copy link

网页版无法登陆的微信号是不是用不了这个,求解惑

@daicoolb
Copy link

daicoolb commented Jun 7, 2023 via email

@toaltf4
Copy link

toaltf4 commented Dec 23, 2023

发生异常: ExpatError
mismatched tag: line 64, column 4
File "C:\Users\die\Desktop\import itchat.py", line 27, in
itchat.auto_login(hotReload=False)
xml.parsers.expat.ExpatError: mismatched tag: line 64, column 4

@daicoolb
Copy link

daicoolb commented Dec 23, 2023 via email

@fumiadder
Copy link

Please scan the QR code to log in.
Please press confirm on your phone.
Traceback (most recent call last):
File "E:\myinstall\code\test\link_AI.py", line 35, in
itchat.auto_login(True, enableCmdQR=2)
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\site-packages\itchat\components\register.py", line 31, in auto_login
self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback,
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\site-packages\itchat\components\login.py", line 53, in login
status = self.check_login()
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\site-packages\itchat\components\login.py", line 137, in check_login
if process_login_info(self, r.text):
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\site-packages\itchat\components\login.py", line 172, in process_login_info
for node in xml.dom.minidom.parseString(r.text).documentElement.childNodes:
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\xml\dom\minidom.py", line 1998, in parseString
return expatbuilder.parseString(string)
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\xml\dom\expatbuilder.py", line 925, in parseString
return builder.parseString(string)
File "E:\myinstall\app\Anaconda3\envs\workpy39\lib\xml\dom\expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: mismatched tag: line 64, column 4

@Lueyu
Copy link

Lueyu commented Jan 19, 2024

import itchat
itchat.auto_login(True, enableCmdQR=2)
itchat.send('Hello, filehelper', toUserName='filehelper')
以上是测试代码,扫码以后出现如下报错
请问下这个报错如何解决,感谢!!
OS为windows

Please scan the QR code to log in.
Traceback (most recent call last):
File "C:\Users\srd0131\Desktop\cc\cc.py", line 3, in
itchat.auto_login(True, enableCmdQR=2)
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\itchat\components\register.py", line 31, in auto_login
self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback,
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\itchat\components\login.py", line 55, in login
status = self.check_login()
^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\itchat\components\login.py", line 137, in check_login
r = self.s.get(url, params=params, headers=headers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\sessions.py", line 602, in get
return self.request("GET", url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\adapters.py", line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request
response = conn.getresponse()
^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\connection.py", line 461, in getresponse
httplib_response = super().getresponse()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 1419, in getresponse
response.begin()
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 331, in begin version, status, reason = self._read_status()
^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 292, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\socket.py", line 707, in readinto
return self._sock.recv_into(b)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1253, in recv_into
return self.read(nbytes, buffer)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\srd0131\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1105, in read
return self._sslobj.read(len, buffer)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

@daicoolb
Copy link

daicoolb commented Jan 19, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment