Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (65 loc) · 2.32 KB
/
Copy pathmain.go
File metadata and controls
72 lines (65 loc) · 2.32 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
// Binary bot-inline implements an inline bot that answers inline queries
// (@your_bot some text) with article results.
//
// It demonstrates the telegram/message/inline helper for building inline
// query results and the telegram/message/styling helper for styled message
// content. Enable inline mode for your bot via @BotFather (/setinline) first.
package main
import (
"context"
"crypto/rand"
"go.uber.org/zap"
"github.com/gotd/log/logzap"
"github.com/gotd/td/examples"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message/inline"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
)
func main() {
// Environment variables:
// BOT_TOKEN: token from BotFather
// APP_ID: app_id of Telegram app.
// APP_HASH: app_hash of Telegram app.
// SESSION_FILE: path to session file
// SESSION_DIR: path to session directory, if SESSION_FILE is not set
examples.Run(func(ctx context.Context, log *zap.Logger) error {
// Dispatcher handles incoming updates.
dispatcher := tg.NewUpdateDispatcher()
opts := telegram.Options{
Logger: logzap.New(log),
UpdateHandler: dispatcher,
}
return telegram.BotFromEnvironment(ctx, opts, func(ctx context.Context, client *telegram.Client) error {
api := tg.NewClient(client)
// Handle inline queries (@your_bot <query>).
dispatcher.OnBotInlineQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotInlineQuery) error {
log.Info("Inline query", zap.String("query", u.Query))
// inline.New builds an answer for the given query ID.
// rand.Reader provides randomness for result IDs.
_, err := inline.New(api, rand.Reader, u.QueryID).
CacheTimeSeconds(1). // small cache, results depend on query
Set(ctx,
// Article result with styled message content.
inline.Article("Styled greeting",
inline.MessageStyledText(
styling.Bold("Hello"),
styling.Plain(", you searched for "),
styling.Italic(u.Query),
),
).
ID("styled").
Description("Sends a styled greeting"),
// Article result that echoes the query as plain text.
inline.Article("Echo",
inline.MessageText(u.Query),
).
ID("echo").
Description("Sends your query back"),
)
return err
})
return nil
}, telegram.RunUntilCanceled)
})
}