-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgio.go
47 lines (44 loc) · 932 Bytes
/
tgio.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
package tgio
import (
"context"
"errors"
"fmt"
"io"
"log"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
)
// Forward forwards reader data to a telegram chat by sending
// messages as a given bot.
func Forward(ctx context.Context, reader io.Reader, token string, chat int) error {
bot, err := tgbot.NewBotAPI(token)
if err != nil {
return fmt.Errorf("couldn't create bot api: %w", err)
}
data := make([]byte, 1024)
var n int
errC := make(chan error)
for {
go func() {
n, err = reader.Read(data)
errC <- err
}()
select {
case <-ctx.Done():
return ctx.Err()
case err = <-errC:
}
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return fmt.Errorf("tgio: couldn't read: %w", err)
}
if n == 0 {
continue
}
msg := tgbot.NewMessage(int64(chat), string(data[:n]))
if _, err := bot.Send(msg); err != nil {
log.Println(fmt.Sprintf("tgio: %v", err))
}
}
}