Telethon发送带有剧透的图像

huangapple go评论64阅读模式
英文:

Telethon send image with spoiler

问题

我可以帮您翻译代码部分:

如何在telethon中发送带有隐藏内容的图片
我使用 **telethon-1.27.0**

这是我正在使用的代码
`client.send_message(user, file="/something.png")`

我尝试使用 `has_spoiler=True`,但收到了错误消息

> send_message() 收到了意外的关键字参数 'has_spoiler''spoiler'

> 在sendmessage中我发现该参数 - 消息已发送返回值为 Message(id=123, peer_id=PeerUser(user_id=123), date=datetime.datetime(2023, 2, 5, 23, 11, 49, tzinfo=datetime.timezone.utc), message='123', out=True, mentioned=False, media_unread=False, silent=False, post=False, from_scheduled=False, legacy=False, edit_hide=False, pinned=False, noforwards=False, from_id=None, fwd_from=None, via_bot_id=None, reply_to=None, media=MessageMediaPhoto(**spoiler=False**, photo=Photo(id=5982148115249082446
英文:

How can I send image with spoiler in telethon?
I use telethon-1.27.0.

This is the code I'm using:
client.send_message(user, file="/something.png")

I tried to use has_spoiler=True but got an error message

> send_message() got an unexpected keyword argument 'has_spoiler' or 'spoiler' and etc.

>in sendmessage i found that parameter - Message sent. Return Value Message(id=123, peer_id=PeerUser(user_id=123), date=datetime.datetime(2023, 2, 5, 23, 11, 49, tzinfo=datetime.timezone.utc), message='123', out=True, mentioned=False, media_unread=False, silent=False, post=False, from_scheduled=False, legacy=False, edit_hide=False, pinned=False, noforwards=False, from_id=None, fwd_from=None, via_bot_id=None, reply_to=None, media=MessageMediaPhoto(spoiler=False, photo=Photo(id=5982148115249082446

答案1

得分: 0

文档解释了如何发送隐藏的文本。

https://docs.telethon.dev/en/stable/examples/working-with-messages.html#sending-spoilers-hidden-text

当前的 Markdown 和 HTML 解析器尚未提供发送隐藏文本的方法。您需要使用 MessageEntitySpoiler 来实现消息文本的部分内容显示在剧透标记下。

这样做的最简单方法是修改内置解析器以支持使用它们已经提供的功能发送这些新的消息实体。

所引用的示例展示了如何在 class CustomMarkdown 中定义解析/取消解析方法。当它看到 [hidden text](spoiler) 并找到与 "spoiler" 完全匹配的文本时,它会编辑一个 MessageEntitySpoiler 对象,以产生所需的效果。

英文:

The documentation explains how to send spoilers.

https://docs.telethon.dev/en/stable/examples/working-with-messages.html#sending-spoilers-hidden-text

> The current markdown and HTML parsers do not offer a way to send spoilers yet. You need to use MessageEntitySpoiler so that parts of the message text are shown under a spoiler.
>
> The simplest way to do this is to modify the builtin parsers to support sending these new message entities with the features they already provide.

The cited
recipe
shows how to define parse / unparse methods in class CustomMarkdown.
When it sees [hidden text](spoiler) and finds a literal
match on "spoiler" it edits in a MessageEntitySpoiler
object that will produce the desired effect.

答案2

得分: 0

你需要使用Telethon版本1.27或更高版本,并手动构建正确的媒体(在这种情况下是InputMediaUploadedPhoto):

from telethon import TelegramClient, types

...

file = ...  # 你的文件路径 'photo.png' 或文件对象

uploaded = await client.upload_file(file)
await client.send_file(chat, types.InputMediaUploadedPhoto(
    uploaded,
    spoiler=True,
))
英文:

You will need to use Telethon v1.27 or greater, and then manually construct the correct media (in this case, InputMediaUploadedPhoto):

from telethon import TelegramClient, types

...

file = ...  # path to your file 'photo.png' or file object

uploaded = await client.upload_file(file)
await client.send_file(chat, types.InputMediaUploadedPhoto(
    uploaded,
    spoiler=True,
))

huangapple
  • 本文由 发表于 2023年2月6日 05:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定