-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (44 loc) · 1.43 KB
/
main.go
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
// Binary bot-echo implements basic example for bot.
package main
import (
"context"
"go.uber.org/zap"
"github.com/gotd/td/examples"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message"
"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: log,
UpdateHandler: dispatcher,
}
return telegram.BotFromEnvironment(ctx, opts, func(ctx context.Context, client *telegram.Client) error {
// Raw MTProto API client, allows making raw RPC calls.
api := tg.NewClient(client)
// Helper for sending messages.
sender := message.NewSender(api)
// Setting up handler for incoming message.
dispatcher.OnNewMessage(func(ctx context.Context, entities tg.Entities, u *tg.UpdateNewMessage) error {
m, ok := u.Message.(*tg.Message)
if !ok || m.Out {
// Outgoing message, not interesting.
return nil
}
// Sending reply.
_, err := sender.Reply(entities, u).Text(ctx, m.Message)
return err
})
return nil
}, telegram.RunUntilCanceled)
})
}