如何从Telegram的telego Go机器人中显示HTML标记?

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

How to display HTML markup from telegram telego Go bot?

问题

我想要像 Durger King 应用程序一样,通过我的机器人向客户推送格式化的消息。对于机器人的任何输入,都会显示一个显示为格式化的“让我们开始吧”的消息,并附带一张图片。在下面有一个“订购食物”WebApp按钮,点击后会打开 PWA。

这是一个简单的图片,还是格式化的 HTML?使用 telego Go 机器人如何发送图片或格式化的 HTML?

如何从Telegram的telego Go机器人中显示HTML标记?

英文:

I'd like to push a formatted message to to clients from my bot in much the same way as the Durger King app does. In response to any input to the bot a message showing for formatted 'Let's get started' is displayed along with a picture. Underneath there is order food WebApp button which opens the PWA.

Is this simply and image or is it formatted HTML, which would possibly be easier to manage.

How can one send either a an image or formatted HTML using the telego Go bot.

如何从Telegram的telego Go机器人中显示HTML标记?

答案1

得分: 1

这是可能错误的实现方式:

var (
    helloMsg = &tele.Message{Text: "<b>让我们开始吧!🎉</b>"}
)

func main() {
    pref := tele.Settings{
        Token:  os.Getenv("TOKEN"),
        Poller: &tele.LongPoller{Timeout: 5 * time.Second},
    }

    b, err := tele.NewBot(pref)
    if err != nil {
        log.Fatal(err)
        return
    }

    b.Handle("/start", func(c tele.Context) error {
        log.Println("检测到启动")
        b.Send(c.Message().Sender, helloMsg.Text, &tele.SendOptions{
            ParseMode: "HTML",
        }, webapp)
        return nil
    })
}

我还没有尝试过是否可以使用上下文而不是机器人本身,并且返回nil可能不是一个好主意。但基本上,这将把ParseMode设置为HTML,并且可以粘贴表情符号...

效果如下图所示:

如何从Telegram的telego Go机器人中显示HTML标记?

英文:

Here's what is probably the wrong way of achieving this:

var (helloMsg    = &amp;tele.Message{Text: &quot;&lt;b&gt;Let&#39;s get started!&lt;/b&gt;&#127839;&quot;})

func main() {	
pref := tele.Settings{
	Token:  os.Getenv(&quot;TOKEN&quot;),
	Poller: &amp;tele.LongPoller{Timeout: 5 * time.Second},
}

b, err := tele.NewBot(pref)
if err != nil {
	log.Fatal(err)
	return
}

    b.Handle(&quot;/start&quot;, func(c tele.Context) error {
	log.Println(&quot;Detected Start&quot;)
	b.Send(c.Message().Sender, helloMsg.Text, &amp;tele.SendOptions{
		ParseMode: &quot;HTML&quot;,
	}, webapp)
	return nil
})}
}

I haven't tried to see if it is possible to use the context instead of the bot itself, and returning nil probably isn't a great idea. But essentially you this is setting the ParseMode to HTML and somehow the emoji can be pasted in...

This is what it looks like:

如何从Telegram的telego Go机器人中显示HTML标记?

答案2

得分: 1

这是使用上下文的版本:

b.Handle("/start", func(c tele.Context) error {
	log.Println("检测到开始")
	b.Send(c.Message().Sender, helloMsg.Text, &tele.SendOptions{
		ParseMode: "HTML",
	}, webapp)
	return nil
})
英文:

Here's the version using the context:

b.Handle(&quot;/start&quot;, func(c tele.Context) error {
	log.Println(&quot;Detected Start&quot;)
	b.Send(c.Message().Sender, helloMsg.Text, &amp;tele.SendOptions{
		ParseMode: &quot;HTML&quot;,
	}, webapp)
	return nil
})

huangapple
  • 本文由 发表于 2022年12月13日 04:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/74776746.html
匿名

发表评论

匿名网友

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

确定