You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Telethon实现Telegram指定用户的通知静音

使用Telethon对Telegram指定用户设置通知静音的实现方案

核心实现直接调用Telethon封装的Telegram底层UpdateNotifySettingsRequest接口即可,不需要依赖第三方额外封装的方法,完整实现步骤和代码如下:

  • 前置依赖:你已经完成Telethon客户端的初始化和账号授权流程,可正常发起API请求
  • 第一步:获取目标用户的实体对象,可通过用户名、用户ID、绑定手机号等参数查询
  • 第二步:构造通知设置更新请求,将静音到期时间设置为对应时间戳即可完成静音操作
from telethon import TelegramClient
from telethon.tl.functions.account import UpdateNotifySettingsRequest
from telethon.tl.types import InputPeerNotifySettings

# 替换为你自己的API参数
api_id = 123456 # 你的API_ID
api_hash = 'abcdef123456xxxxxx' # 你的API_HASH
client = TelegramClient('session_name', api_id, api_hash)

async def mute_user(target):
    # 获取目标用户实体
    user = await client.get_entity(target)
    # 构造永久静音请求,mute_until设为2147483647代表永久生效(Telegram支持的最大Unix时间戳)
    await client(UpdateNotifySettingsRequest(
        peer=user,
        settings=InputPeerNotifySettings(
            mute_until=2147483647,
            # 其余参数保持默认即可,不需要额外修改
            show_previews=True,
            silent=False
        )
    ))
    print("目标用户已设置永久静音")

# 如果需要取消静音,将mute_until设为0即可
async def unmute_user(target):
    user = await client.get_entity(target)
    await client(UpdateNotifySettingsRequest(
        peer=user,
        settings=InputPeerNotifySettings(
            mute_until=0,
            show_previews=True,
            silent=False
        )
    ))
    print("目标用户已解除静音")

# 调用示例
with client:
    client.loop.run_until_complete(mute_user('目标用户名/用户ID/绑定手机号'))

参数说明:

  1. target参数支持传入字符串格式的用户名、整数格式的用户ID、或者用户绑定的手机号
  2. mute_until参数为Unix时间戳,单位为秒:设为0代表取消静音,设为未来任意时间点的时间戳即可实现对应时长的临时静音,设为2147483647为Telegram支持的最大永久静音时长
  3. 该方法同样适用于群组、频道的通知静音,仅需将target替换为对应群组/频道的ID、用户名即可

内容的提问来源于stack exchange,提问作者Lalan Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 06:06:04