Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[提问] 会话轮询 session : on() 无法正常工作 #853

Open
xiaoxiaohaoa opened this issue Nov 20, 2024 · 10 comments
Open

[提问] 会话轮询 session : on() 无法正常工作 #853

xiaoxiaohaoa opened this issue Nov 20, 2024 · 10 comments
Labels
question 这啥呀这是,我不到啊

Comments

@xiaoxiaohaoa
Copy link

xiaoxiaohaoa commented Nov 20, 2024

Python 版本: 3.11.0

模块版本: 16.3.0

运行环境: Windows


正常运行了很久的私信会话轮询脚本昨天晚上突然发现监测不到新的私信消息了,查看运行日志发现直到昨天中午还是没问题的状态

fbf68b3861a1bd351d7a3ade76fef0ce
图中时间为GMT,“开始轮询”的时间点是发现脚本出问题尝试重启的时候

排查了下问题,日志显示正在轮询,但实际无法监测到任何发送或接收到的私信消息
把无关部分排除掉、只留最基本的轮询+自动回复功能之后再试,仍然如此
3884655317988b00f6b2105afec7e4ba
图中私信双方分别是当前凭据账号和另一个小号,但无论是哪一边发送的消息都没有被监测到

已尝试将模块更新到最新版本,并修改了相应的接口写法,但仍然无效(题外话,此前本人并没有及时跟进到新版本模组,一直用的接口是旧版的“@session.on(Event.TEXT)”,更新后才修改为新“的@session.on(EventType.TEXT)”)

个人猜测是昨天午间左右b站接口发生变动导致的,但具体什么情况完全没有头绪,看了看bili-API这边和隔壁BAC都没什么消息,也可能只是我个人的问题……
还是想尽快恢复私信监测功能,如果各位有任何想法都欢迎,感谢!

@xiaoxiaohaoa xiaoxiaohaoa added the question 这啥呀这是,我不到啊 label Nov 20, 2024
@Huzxiao
Copy link

Huzxiao commented Nov 20, 2024

同样遇到这个问题

@xiaoxiaohaoa
Copy link
Author

xiaoxiaohaoa commented Nov 20, 2024

更新,挂机运行几个小时后报了 no running event loopTimeoutError,不知道有没有关联?

运行期间没有产生除“开始轮询”以外的日志,仍然无法按预期监测私信。

以下是完整的报错信息:

Job "Session.run.<locals>.qurey (trigger: interval[0:00:06], next run at: 2024-11-20 18:42:48 CST)" raised an exception
Traceback (most recent call last):
  File "...\site-packages\bilibili_api\utils\sync.py", line 22, in sync
    asyncio.get_running_loop()
RuntimeError: no running event loop

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...\site-packages\aiohttp\client.py", line 663, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\aiohttp\connector.py", line 538, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\aiohttp\connector.py", line 1048, in _create_connection
    _, proto = await self._create_proxy_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\aiohttp\connector.py", line 1505, in _create_proxy_connection
    return await self._start_tls_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\aiohttp\connector.py", line 1248, in _start_tls_connection
    tls_transport = await self._loop.start_tls(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\asyncio\base_events.py", line 1253, in start_tls
    await waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...\site-packages\apscheduler\executors\base_py3.py", line 30, in run_coroutine_job
    retval = await job.func(*job.args, **job.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\bilibili_api\session.py", line 502, in qurey
    js: dict = await new_sessions(self.credential, self.maxTs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\bilibili_api\session.py", line 79, in new_sessions
    return await Api(**api, credential=credential).update_params(**params).result
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\bilibili_api\utils\network.py", line 265, in result
    self.__result = await self.request()
                    ^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\bilibili_api\utils\network.py", line 165, in inner
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\site-packages\bilibili_api\utils\network.py", line 576, in request
    async with session.request(**config) as resp:
  File "...\site-packages\aiohttp\client.py", line 1360, in __aenter__
    self._resp: _RetType = await self._coro
                           ^^^^^^^^^^^^^^^^
  File "...\site-packages\aiohttp\client.py", line 578, in _request
    with timer:
  File "...\site-packages\aiohttp\helpers.py", line 749, in __exit__
    raise asyncio.TimeoutError from exc_val
TimeoutError

@Nemo2011
Copy link
Owner

加上 settings.http_client = settings.HTTPClient.HTTPX 再尝试会不会有此问题

@Drelf2018
Copy link
Collaborator

破案了,现在 fetch_session_msgs 也要求 w_ridwts 参数了
QQ20241120-222444

async def fetch_session_msgs(
talker_id: int, credential: Credential, session_type: int = 1, begin_seqno: int = 0
) -> dict:
"""
获取指定用户的近三十条消息
Args:
talker_id (int) : 用户 UID
credential (Credential): Credential
session_type (int) : 会话类型 1 私聊 2 应援团
begin_seqno (int) : 起始 Seqno
Returns:
dict: 调用 API 返回结果
"""
credential.raise_for_no_sessdata()
params = {
"talker_id": talker_id,
"session_type": session_type,
"begin_seqno": begin_seqno,
}
api = API["session"]["fetch"]
return await Api(**api, credential=credential).update_params(**params).result

@xiaoxiaohaoa
Copy link
Author

加上 settings.http_client = settings.HTTPClient.HTTPX 再尝试会不会有此问题

不太清楚具体该加在哪里以及怎么写……
问了一下AI,把下面这一段搬进脚本里了

import httpx

class Settings:
    class HTTPClient:
        HTTPX = httpx

settings = Settings()
settings.http_client = settings.HTTPClient.HTTPX

async def fetch_data(url):
    async with settings.http_client.AsyncClient() as client:
        response = await client.get(url)
        return response.json()

现在脚本长这样
281fc79e505143c08b20c3aa4ba1af91

运行起来还是和之前一样,日志报告开始轮询,但仍然监测不到私信消息

@xiaoxiaohaoa
Copy link
Author

破案了,现在 fetch_session_msgs 也要求 w_ridwts 参数了 QQ20241120-222444

async def fetch_session_msgs(
talker_id: int, credential: Credential, session_type: int = 1, begin_seqno: int = 0
) -> dict:
"""
获取指定用户的近三十条消息
Args:
talker_id (int) : 用户 UID
credential (Credential): Credential
session_type (int) : 会话类型 1 私聊 2 应援团
begin_seqno (int) : 起始 Seqno
Returns:
dict: 调用 API 返回结果
"""
credential.raise_for_no_sessdata()
params = {
"talker_id": talker_id,
"session_type": session_type,
"begin_seqno": begin_seqno,
}
api = API["session"]["fetch"]
return await Api(**api, credential=credential).update_params(**params).result

请问这两个参数是什么、要怎么获取呢?有什么我这边能做的吗?

@Drelf2018
Copy link
Collaborator

请问这两个参数是什么、要怎么获取呢?有什么我这边能做的吗?

目前我还在研究新接口怎么调用,成功了之后会告诉你,不过今天应该搞不定。

@xiaoxiaohaoa
Copy link
Author

请问这两个参数是什么、要怎么获取呢?有什么我这边能做的吗?

目前我还在研究新接口怎么调用,成功了之后会告诉你,不过今天应该搞不定。

了解,非常感谢!

@Drelf2018
Copy link
Collaborator

如果想快速修复这个问题,可以按照我给出的代码,修改你本地的库里的代码

async def fetch_session_msgs(talker_id: int, credential: Credential, session_type: int = 1, begin_seqno: int = 0) -> dict:
    """
    获取指定用户的近三十条消息

    Args:
        talker_id    (int)       : 用户 UID

        credential   (Credential): Credential

        session_type (int)       : 会话类型 1 私聊 2 应援团

        begin_seqno  (int)       : 起始 Seqno

    Returns:
        dict: 调用 API 返回结果
    """

    credential.raise_for_no_sessdata()
    params = {
        "talker_id": talker_id,
        "session_type": session_type,
        "begin_seqno": begin_seqno,
+       "size": 50,
+       "sender_device_id": 1,
+       "build": 0,
+       "mobi_app": "web",
    }
    api = API["session"]["fetch"]

-   return await Api(**api, credential=credential).update_params(**params).result
+   return await Api(**api, credential=credential, wbi=True).update_params(**params).result

这段代码的位置大概在 你的python安装路径\Lib\site-packages\bilibili_api\session.py 第 30 行处

你的python安装路径即你之前发送的错误报告处用 ... 代替的部分


或者你可以等下次包更新时,重新安装本库以解决问题(可能很久之后)

@xiaoxiaohaoa
Copy link
Author

xiaoxiaohaoa commented Nov 20, 2024

如果想快速修复这个问题,可以按照我给出的代码,修改你本地的库里的代码

很顺利地成功了!!感激不尽!!
现在可以像之前一样正常监视私信并做出回应了。

感觉这个改动可以提一个pr?楼上有一位提到遇到同样问题的,我想应该也有其他人需要用到这个功能。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question 这啥呀这是,我不到啊
Projects
None yet
Development

No branches or pull requests

4 participants