-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend.go
More file actions
106 lines (88 loc) · 2.82 KB
/
send.go
File metadata and controls
106 lines (88 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/AshokShau/gotdbot"
)
const (
adminID = int64(5938660179)
)
func sendJSON(c *gotdbot.Client, chatID, replyToID int64, output string) error {
language := "json"
if len([]rune(output)) <= maxMessageLen {
escaped := gotdbot.EscapeHTML(output)
text := "<pre language=\"" + language + "\">" + escaped + "</pre>"
_, err := c.SendTextMessage(chatID, text, &gotdbot.SendTextMessageOpts{
ParseMode: gotdbot.ParseModeHTML,
ReplyToMessageID: replyToID,
})
return err
}
tmpFile, err := os.CreateTemp("", "update-*.json")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
defer os.Remove(tmpFile.Name())
if _, err = tmpFile.WriteString(output); err != nil {
tmpFile.Close()
return fmt.Errorf("write temp file: %w", err)
}
tmpFile.Close()
_, err = c.SendDocument(chatID, gotdbot.InputFileLocal{Path: tmpFile.Name()}, &gotdbot.SendDocumentOpts{ReplyToMessageID: replyToID})
return err
}
func sendResult(c *gotdbot.Client, queryID int64, output string) error {
language := "json"
if len([]rune(output)) <= maxMessageLen {
escaped := gotdbot.EscapeHTML(output)
text := "<pre language=\"" + language + "\">" + escaped + "</pre>"
ftext, err := gotdbot.GetFormattedText(c, text, nil, gotdbot.ParseModeHTML)
if err != nil {
return fmt.Errorf("get formatted text: %w", err)
}
result := gotdbot.InputInlineQueryResultArticle{
Id: fmt.Sprintf("json-%d", time.Now().UnixNano()),
Title: "JSON Result",
InputMessageContent: &gotdbot.InputMessageText{
Text: ftext,
},
}
_, err = c.AnswerGuestQuery(queryID, result)
return err
}
tmpFile, err := os.CreateTemp("", "guest-query-*.json")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
defer os.Remove(tmpFile.Name())
if _, err = tmpFile.WriteString(output); err != nil {
tmpFile.Close()
return fmt.Errorf("write temp file: %w", err)
}
tmpFile.Close()
msg, err := c.SendDocument(adminID, gotdbot.InputFileLocal{Path: tmpFile.Name()}, &gotdbot.SendDocumentOpts{Caption: fmt.Sprintf("JSON result for guest query %d", queryID)})
if err != nil {
return fmt.Errorf("upload document: %w", err)
}
result := &gotdbot.InputInlineQueryResultDocument{
Id: strconv.FormatInt(time.Now().UnixNano(), 10),
Title: "JSON Result",
Description: "Large JSON output",
DocumentUrl: msg.RemoteFileID(), // TDLib MOOD
InputMessageContent: &gotdbot.InputMessageDocument{
Document: gotdbot.InputFileRemote{Id: msg.RemoteFileID()},
Caption: &gotdbot.FormattedText{
Text: fmt.Sprintf("JSON result for guest query %d", queryID),
},
},
}
_, err = c.AnswerGuestQuery(queryID, result)
if err != nil {
log.Printf("[DEBUG] failed to answer guest query: %v", err)
return fmt.Errorf("answer guest query: %w", err)
}
return nil
}