【问题标题】:Sending message in telegram bot with images在带有图像的电报机器人中发送消息
【发布时间】:2016-10-18 01:42:22
【问题描述】:

我有 php 上的电报机器人代码,并回复通过replyWithMessage 方法发送的消息。

这里的所有命令:

 $this->replyWithMessage(['text' => $item['title']. "\n\n" . $url]);

如何在文本之前添加一些预览图像?

【问题讨论】:

    标签: php telegram telegram-bot


    【解决方案1】:

    您可以使用/sendphoto 并设置出现在图像下方的caption
    https://core.telegram.org/bots/api#sendphoto

    【讨论】:

    【解决方案2】:

    不,您可以在一条消息中发送包含照片的文本。 Telegram 允许你这样做,但方法有点棘手。

    1. 使用https://core.telegram.org/bots/api#sendmessage方法,设置选项disable_web_page_preview => false
    2. 在您的 text 数据中,在您的消息文本中放置一个带有不可见字符的图片链接。

    例子:

    $message = <<<TEXT
    *** your content ***
    *** somewhere below (or above) a link to your image with invisible character(s) ***
    <a href="https://www.carspecs.us/photos/c8447c97e355f462368178b3518367824a757327-2000.jpg"> ‏ </a>
    TEXT;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
    curl_setopt($ch, CURLOPT_URL, 'https://api.telegram.org/bot<token>/sendMessage');
    $postFields = array(
        'chat_id' => '@username',
        'text' => $message,
        'parse_mode' => 'HTML',
        'disable_web_page_preview' => false,
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    if(!curl_exec($ch))
        echo curl_error($ch);
    curl_close($ch);
    

    【讨论】:

    • 实际信息在这里:stackoverflow.com/a/43705283/4213969 tl dr:1) 将 &amp;#8205; 放入 a-tag 而不是空格 2) 设置 disable_web_page_preview=true
    • @Vladimir,感谢您的提醒,完美运行
    【解决方案3】:

    您不能发送同时包含图片和文字的短信。但是,如果您的文本包含 URL,Telegram 默认会显示网页的预览。 或者您可以一个接一个地发送两条消息或发送带有标题的照片。

    【讨论】:

      【解决方案4】:

      你也可以使用 markdown 来做到这一点:

      var axios = require('axios');
      
      axios.post('https://api.telegram.org/'+telegram_bot_id+'/sendMessage', { chat_id: _target_telegram_channel, parse_mode: 'markdown', text: '[ ‏ ](https://www.amazon.it/gp/product/B07NVCJ3V8?pf_rd_p=ba8c3f2e-eba5-4c79-9599-683af7a49dd1&pf_rd_r=XPRH5A07HN9W62DK1R84)' } )
          .then(response => {
              console.log(response);
          })
          .catch(error => {
              console.log(error);
          })
      

      【讨论】:

        猜你喜欢
        • 2017-03-19
        • 2016-03-11
        • 2020-12-15
        • 1970-01-01
        • 2017-09-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-31
        • 2018-02-25
        相关资源
        最近更新 更多