wechaty / python-wechaty

Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
https://wechaty.readthedocs.io/zh_CN/latest/
Apache License 2.0
1.56k stars 229 forks source link

🐛🐛 Bug Report: Event loop is closed #296

Closed w311ang closed 2 years ago

w311ang commented 2 years ago

requirements

PS E:\wbwNodejs\autoWxSigning> pip list Package Version


aiofiles 0.8.0 altgraph 0.17.2 beautifulsoup4 4.10.0 betterproto 1.2.5 blinker 1.4 certifi 2021.10.8 charset-normalizer 2.0.9 click 8.0.3 colorama 0.4.4 dnspython 2.1.0 Flask 2.0.2 future 0.18.2 grpclib 0.4.2 h11 0.13.0 h2 4.1.0 hpack 4.0.0 hypercorn 0.13.2 hyperframe 6.0.1 idna 3.3 itchat-uos 1.4.1 itsdangerous 2.0.1 Jinja2 3.0.3 lxml 4.7.1 MarkupSafe 2.0.1 multidict 5.2.0 MyPytools 2.2.4 pefile 2021.9.3 ping3 3.0.2 pip 21.3.1 priority 2.0.0 psutil 5.8.0 pycryptodome 3.12.0 pyee 9.0.3 pyinstaller 4.7 pyinstaller-hooks-contrib 2021.4 pypng 0.0.21 PyQRCode 1.2.1 pywin32-ctypes 0.2.0 qrcode 7.3.1 quart 0.16.2 requests 2.26.0 schedule 1.1.0 setuptools 58.1.0 soupsieve 2.3.1 stringcase 1.2.0 toml 0.10.2 typing_extensions 4.0.1 urllib3 1.26.7 wechaty 0.8.33 wechaty-grpc 0.20.19 wechaty-puppet 0.4.0 wechaty-puppet-service 0.8.8 Werkzeug 2.0.2 wsproto 1.0.0

PS E:\wbwNodejs\autoWxSigning> python --version Python 3.10.1


## Describe your problem

> A clear and concise description of what the bug is.

## Reproduce your problem

```python
import asyncio
from typing import List, Optional, Union

from wechaty_puppet import FileBox  # type: ignore

from wechaty import Wechaty, Contact
from wechaty.user import Message, Room

class MyBot(Wechaty):

    async def on_message(self, msg: Message):
        """
        listen for message event
        """
        from_contact: Optional[Contact] = msg.talker()
        text = msg.text()
        room: Optional[Room] = msg.room()
        if text == 'ding':
            conversation: Union[
                Room, Contact] = from_contact if room is None else room
            await conversation.ready()
            await conversation.say('dong')
            file_box = FileBox.from_url(
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/'
                'u=1116676390,2305043183&fm=26&gp=0.jpg',
                name='ding-dong.jpg')
            await conversation.say(file_box)

asyncio.run(MyBot().start())

Error info

2022-01-20 22:04:06,043 - Wechaty - INFO - init_puppet_event_bridge() <<wechaty_puppet_service.puppet.PuppetService object at 0x0000027EC9BB71F0>>
2022-01-20 22:04:06,044 - Wechaty - INFO - initPuppetEventBridge() puppet.on(dong) (listenerCount:1) registering...
2022-01-20 22:04:06,044 - Wechaty - INFO - initPuppetEventBridge() puppet.on(error) (listenerCount:1) registering...
2022-01-20 22:04:06,044 - Wechaty - INFO - initPuppetEventBridge() puppet.on(heart-beat) (listenerCount:1) registering...
2022-01-20 22:04:06,045 - Wechaty - INFO - initPuppetEventBridge() puppet.on(ready) (listenerCount:1) registering...
2022-01-20 22:04:06,045 - Wechaty - INFO - receive <reset> event <%s>
2022-01-20 22:04:06,046 - Wechaty - INFO - initPuppetEventBridge() puppet.on(reset) (listenerCount:0) registering...
2022-01-20 22:04:06,046 - Wechaty - INFO - initPuppetEventBridge() puppet.on(friendship) (listenerCount:1) registering...
2022-01-20 22:04:06,046 - Wechaty - INFO - initPuppetEventBridge() puppet.on(login) (listenerCount:1) registering...
2022-01-20 22:04:06,046 - Wechaty - INFO - initPuppetEventBridge() puppet.on(logout) (listenerCount:1) registering...
2022-01-20 22:04:06,046 - Wechaty - INFO - initPuppetEventBridge() puppet.on(message) (listenerCount:1) registering...
2022-01-20 22:04:06,047 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-invite) (listenerCount:1) registering...
2022-01-20 22:04:06,047 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-join) (listenerCount:1) registering...
2022-01-20 22:04:06,047 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-leave) (listenerCount:1) registering...
2022-01-20 22:04:06,047 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-topic) (listenerCount:1) registering...
2022-01-20 22:04:06,048 - Wechaty - INFO - initPuppetEventBridge() puppet.on(scan) (listenerCount:1) registering...
2022-01-20 22:04:06,048 - Wechaty - INFO - starting puppet ...
2022-01-20 22:04:06,048 - PuppetService - INFO - init puppet ...
2022-01-20 22:04:06,075 - PuppetService - INFO - starting the puppet ...
Traceback (most recent call last):
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\wechaty_puppet_service\puppet.py", line 924, in start
    await self.puppet_stub.stop()
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\wechaty_grpc\wechaty\__init__.py", line 25, in stop
    return await self._unary_unary(
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\betterproto\__init__.py", line 1123, in _unary_unary
    await stream.send_message(request, end=True)
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 241, in send_message    await self.send_request()
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 205, in send_request    release_stream = await stream.send_request(
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\protocol.py", line 375, in send_request
    self._h2_connection.send_headers(stream_id, headers,
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\h2\connection.py", line 770, in send_headers
    frames = stream.send_headers(
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\h2\stream.py", line 881, in send_headers
    self._authority = authority_from_headers(headers)
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\h2\utilities.py", line 180, in authority_from_headers
    return v.encode('utf-8') if not isinstance(v, bytes) else vAttributeError: 'NoneType' object has no attribute 'encode'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\wbwNodejs\autoWxSigning\client.py", line 30, in <module>
    asyncio.run(MyBot().start())
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
    return future.result()
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\wechaty\wechaty.py", line 394, in start
    await self.puppet.start()
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\wechaty_puppet_service\puppet.py", line 926, in start
    await self.puppet_stub.start()
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\wechaty_grpc\wechaty\__init__.py", line 17, in start
    return await self._unary_unary(
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\betterproto\__init__.py", line 1123, in _unary_unary
    await stream.send_message(request, end=True)
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 241, in send_message    await self.send_request()
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 205, in send_request    release_stream = await stream.send_request(
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\protocol.py", line 375, in send_request
    self._h2_connection.send_headers(stream_id, headers,
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\h2\connection.py", line 770, in send_headers
    frames = stream.send_headers(
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\h2\stream.py", line 881, in send_headers
    self._authority = authority_from_headers(headers)
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\site-packages\h2\utilities.py", line 180, in authority_from_headers
    return v.encode('utf-8') if not isinstance(v, bytes) else vAttributeError: 'NoneType' object has no attribute 'encode'
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000027EC86B6F80>
Traceback (most recent call last):
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
  File "C:\Users\w311a\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed

Your experiments

The first time it was run it was successful, it was OK to ding-dong, the second time it didn't work, is there any cache that is causing the problem. I tried reinstalling wechaty-puppet-xp, but the problem is still there.

bigbrother666sh commented 2 years ago

ding-dong程序重启,gateway服务也要重启

w311ang commented 2 years ago

ding-dong程序重启,gateway服务也要重启

重启的意思不是关了再开吗,已经这样尝试过了

w311ang commented 2 years ago

295

w311ang commented 2 years ago

问题解决,发现服务端和客户端的配置并不是通用的,服务端的token变量名是WECHATY_TOKEN,而客户端是WECHATY_PUPPET_SERVICE_TOKEN,相当于我少了客户端的token配置,却报的这个错就离谱就不应该给个明显的无token错误吗

w311ang commented 2 years ago

给各位我的demo,应该可以少走点弯路 config.ps1

$env:WECHATY_PUPPET="wechaty-puppet-xp"
$env:WECHATY_PUPPET_SERVER_PORT="8788"
$env:WECHATY_PUPPET_SERVICE_TOKEN="__token__" #改成你的token
$env:WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_SERVER="true"
$env:WECHATY_PUPPET_SERVICE_ENDPOINT="127.0.0.1:8788"
$env:WECHATY_LOG="verbose" #别注释不然跑不起来~~此处靠bug运行~~

server.ps1

.\config.ps1
wechaty gateway --puppet $env:WECHATY_PUPPET --token $env:WECHATY_PUPPET_SERVICE_TOKEN --port $env:WECHATY_PUPPET_SERVER_PORT

client.ps1

.\config.ps1
python .\client.py

client.py from https://wechaty.readthedocs.io/zh_CN/latest/introduction/use-web-protocol/

import asyncio
from typing import List, Optional, Union

from wechaty_puppet import FileBox  # type: ignore

from wechaty import Wechaty, Contact
from wechaty.user import Message, Room

class MyBot(Wechaty):

    async def on_message(self, msg: Message):
        """
        listen for message event
        """
        from_contact: Optional[Contact] = msg.talker()
        text = msg.text()
        room: Optional[Room] = msg.room()
        if text == 'ding':
            conversation: Union[
                Room, Contact] = from_contact if room is None else room
            await conversation.ready()
            await conversation.say('dong')
            file_box = FileBox.from_url(
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/'
                'u=1116676390,2305043183&fm=26&gp=0.jpg',
                name='ding-dong.jpg')
            await conversation.say(file_box)

asyncio.run(MyBot().start())
huan commented 2 years ago

@w311ang Thank you very much for sharing your experiences to help others, it's very valuable!

w311ang commented 2 years ago

@w311ang Thank you very much for sharing your experiences to help others, it's very valuable!

Can I just join your team to join wechat group and get a token? Or should I make it into a repository?

huan commented 2 years ago

@w311ang To become a Wechaty Contributor, you can follow our Wechaty Contributor Program.

Learn it from our official website: https://wechaty.js.org/docs/contributor-program/

xinxinyihao commented 2 years ago

问题解决,发现服务端和客户端的配置并不是通用的,服务端的token变量名是WECHATY_TOKEN,而客户端是WECHATY_PUPPET_SERVICE_TOKEN,相当于我少了客户端的token配置,却报的这个错就离谱就不应该给个明显的无token错误吗

大佬,现在是只能发送文字,不能其他类型的消息么???

w311ang commented 2 years ago

@xinxinyihao 去看看文档可能有点帮助,gateway对接的可能不支持,但是puppet-xp本身是支持发送图片的