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

上传头像触发RPCError 400: STICKER_MIME_INVALID问题求助

解决RPCError 400: STICKER_MIME_INVALID(UploadProfilePhotoRequest引发)问题

这个错误的核心原因是上传时文件的MIME类型被错误识别为贴纸类型——即便你使用了jpg/png格式,也可能因客户端自动推断MIME类型出错触发该问题。以下是具体修复方案:

修复步骤

  1. 手动指定文件MIME类型
    不要依赖客户端自动推断,明确告诉Telegram上传的是图片类型,通过InputFile类手动设置mime_type参数。
  2. 传递正确文件名
    显式传递带后缀的文件名,帮助客户端准确识别文件类型,避免推断偏差。

修改后的代码示例

import os
import random
from telethon import TelegramClient as TC
from telethon.tl.functions.photos import UploadProfilePhotoRequest
from telethon.tl.types import InputFile

async def to_add_profile_photo(photo_dir, session_file):
    async with TC(session_file) as client:
        photos = [os.path.join(photo_dir, f) for f in os.listdir(photo_dir) if os.path.isfile(os.path.join(photo_dir, f))]
        if not photos:
            print(f'Error: No files in {photo_dir}')
            return
        photo_path = random.choice(photos)
        
        # 根据文件后缀匹配对应MIME类型
        if photo_path.lower().endswith(('.jpg', '.jpeg')):
            mime_type = 'image/jpeg'
        elif photo_path.lower().endswith('.png'):
            mime_type = 'image/png'
        else:
            print(f'Error: Unsupported file type for {photo_path}')
            return
            
        try:
            with open(photo_path, 'rb') as f:
                # 手动构造InputFile并指定MIME类型与文件名
                input_file = InputFile(f, mime_type=mime_type, file_name=os.path.basename(photo_path))
                result = await client(UploadProfilePhotoRequest(await client.upload_file(input_file)))
            if result:
                print('OK')
        except Exception as e:
            print(f'Error during upload: {e}')

额外排查点

  • 验证图片文件有效性:部分文件后缀为jpg/png,但实际头部损坏或格式异常,可通过file命令(Linux/macOS)或图片查看工具确认文件完整性。
  • 更新Telethon版本:旧版本可能存在MIME类型推断bug,执行pip install --upgrade telethon升级至最新版。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 11:17:23