在使用Telegram API时,是否有可能在文本消息中隐藏bot_command?

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

Is is possible to hide bot_command in text messages when using telegram api?

问题

我正在使用sendMessage方法来自[telegram API][1]。

我想发送类似这样的内容:

> 一些消息 用于命令的一些文本,另一段文本。


当用户从电报机器人读取此消息时,他可以点击 用于命令的一些文本 ,并且此操作将生成来自用户的消息,应该如下所示:

> none >/一些命令 带文本 >

是否可能实现这样的功能?

因为当我尝试使用 [messageEntity][2] 时它不起作用:

    "chat_id": 1,
    "text": "/一些命令 文本, 另一段文本",
    "entities": [
        {
            "type" : "bot_command",
            "offset": 0,
            "length": 18
        }
    ]
}'```

所以电报只解析消息并只能点击 `一些命令` ,而不是带文本的命令。另外,如果我尝试发送不带 `\` 的消息,它根本不会解析。


[1]: https://core.telegram.org/bots/api
[2]: https://core.telegram.org/bots/api#messageentity

<details>
<summary>英文:</summary>

I&#39;m using the `sendMessage` method from [telegram api][1]

I want to send something like this:

&gt; Some message ***some_text_for_command*** , another text.

---


When user will read this message from telegram bot, he can click on ***some_text_for_command*** and this action will generate message from user, which should looks like this:

&gt; ```none
&gt;/some_command with_text
&gt;```
---

Is is possible to realize something like this?

Because, when I try to use [messageEntity][2] is doesn&#39;t work:

curl --location --request POST 'https://api.telegram.org/bot{bot_code}/sendMessage'
--header 'Content-Type: application/json'
--data-raw '{
"chat_id": 1,
"text": "/some_command text, another text",
"entities": [
{
"type" : "bot_command",
"offset": 0,
"length": 18
}
]
}'


So telegram just parses message and gives an ability to click only on `some_command`, not for the command with the text. Also if I try to send message without `\`, it should parse it at all.


  [1]: https://core.telegram.org/bots/api
  [2]: https://core.telegram.org/bots/api#messageentity

</details>


# 答案1
**得分**: 1

不,不幸的是这是不可能的。

----------

有两种方法可以接近您期望的输出。

#### 选项1,命令+键盘

您可以发送一个没有参数的命令,例如`/some_command`,在响应中单击该命令后,显示一个[键盘](https://core.telegram.org/bots/features#keyboards),显示可用于`some_text`的选项。

文档中的示例图像:
[![enter image description here][1]][1]

#### 选项2,内联代码+复制/粘贴

您可以发送一个内联代码块,用户可以按下以复制整个命令,这样用户只需单击并粘贴命令到机器人:

/some_command some_text

&lt;https://stackoverflow.com/questions/59713920/how-to-make-that-when-you-click-on-the-text-it-was-copied-pytelegrambotapi&gt;

[1]: https://i.stack.imgur.com/2BcqZ.png

<details>
<summary>英文:</summary>

No, unfortunately this is not possible.


----------

There are 2 ways to come somehow close to your desired output.

#### Option 1, command + keyboard

You could send a command without a parameter, like `/some_command` where in response on clicking that command you show a [keyboard](https://core.telegram.org/bots/features#keyboards) with the options available for `some_text`

Example image from documenatation on how the keyboard could look like:
[![enter image description here][1]][1]


#### Option 2, inline-code + copy/paste

You could send an inline code block, on which the user can press to copy the whole command, this way the user just have to click and paste the command to the bot:

/some_command some_text

&lt;https://stackoverflow.com/questions/59713920/how-to-make-that-when-you-click-on-the-text-it-was-copied-pytelegrambotapi&gt;

  [1]: https://i.stack.imgur.com/2BcqZ.png

</details>



huangapple
  • 本文由 发表于 2023年4月17日 16:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033167.html
匿名

发表评论

匿名网友

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

确定