]> git.giacomolli.uk - onechat/blobdiff - botapi.go
Send the replies to Telegram with their formats
[onechat] / botapi.go
index d02aff04b539507225a0ab858822abb98fd38adf..f48465c3174674173cb1b8d8337932cdae5c05ba 100644 (file)
--- a/botapi.go
+++ b/botapi.go
@@ -87,6 +87,17 @@ type botChat struct {
        Type string `json:"type"`
 }
 
        Type string `json:"type"`
 }
 
+// botEntity is a message entity of sendMessage: a part of the text with a
+// format, such as bold or a link. Offset and Length count UTF-16 code units.
+// URL is the target of a text_link, and Language the language of a pre.
+type botEntity struct {
+       Type     string `json:"type"`
+       Offset   int    `json:"offset"`
+       Length   int    `json:"length"`
+       URL      string `json:"url,omitempty"`
+       Language string `json:"language,omitempty"`
+}
+
 // botUser is the sender of a message.
 type botUser struct {
        ID int64 `json:"id"`
 // botUser is the sender of a message.
 type botUser struct {
        ID int64 `json:"id"`
@@ -119,16 +130,23 @@ func (b *botAPI) GetUpdates(ctx context.Context, offset int64) ([]botUpdate, err
        return updates, err
 }
 
        return updates, err
 }
 
-// SendMessage sends text to chat as plain text, with no parse mode. A call that
-// takes longer than sendTimeout fails with errBotNetwork.
-func (b *botAPI) SendMessage(ctx context.Context, chat int64, text string) error {
+// SendMessage sends text to chat with the format of entities, with no parse
+// mode, so the text needs no escaping. With no entities, the text is plain. A
+// call that takes longer than sendTimeout fails with errBotNetwork.
+func (b *botAPI) SendMessage(ctx context.Context, chat int64, text string, entities []botEntity) error {
        ctx, cancel := context.WithTimeout(ctx, b.sendTimeout)
        defer cancel()
 
        ctx, cancel := context.WithTimeout(ctx, b.sendTimeout)
        defer cancel()
 
-       return b.call(ctx, "sendMessage", map[string]any{
+       params := map[string]any{
                "chat_id": chat,
                "text":    text,
                "chat_id": chat,
                "text":    text,
-       }, nil)
+       }
+
+       if len(entities) > 0 {
+               params["entities"] = entities
+       }
+
+       return b.call(ctx, "sendMessage", params, nil)
 }
 
 // SendTyping shows the typing action in chat for about five seconds. A call
 }
 
 // SendTyping shows the typing action in chat for about five seconds. A call