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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package plugins
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/neonxp/tamtam"
"transport/lib"
)
type Telegram struct {
Routing []lib.Routing
API *tgbotapi.BotAPI
Updates chan lib.Message
Bus chan lib.Message
}
func NewTelegram(token string, updates chan lib.Message, bus chan lib.Message, routing []lib.Routing) *Telegram {
tgApi, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
return &Telegram{API: tgApi, Updates: updates, Bus: bus, Routing: routing}
}
func (t *Telegram) Run(ctx context.Context) error {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := t.API.GetUpdatesChan(u)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
case upd := <-updates:
if upd.Message == nil { // ignore any non-Message Updates
continue
}
for _, r := range t.Routing {
if r.TgID == upd.Message.Chat.ID {
from := fmt.Sprintf("%s %s", upd.Message.From.FirstName, upd.Message.From.LastName)
if upd.Message.From.UserName != "" {
from = fmt.Sprintf("%s %s (%s)", upd.Message.From.FirstName, upd.Message.From.LastName, upd.Message.From.UserName)
}
isSticker := ""
images := make([]string, 0)
if upd.Message.Sticker != nil {
isSticker = upd.Message.Sticker.Emoji
s, _ := t.API.GetFileDirectURL(upd.Message.Sticker.Thumbnail.FileID)
images = append(images, s)
}
if upd.Message.Photo != nil && len(*upd.Message.Photo) > 0 {
p := (*upd.Message.Photo)[0]
s, _ := t.API.GetFileDirectURL(p.FileID)
images = append(images, s)
}
t.Bus <- lib.Message{
To: r.TTID,
From: from,
Text: upd.Message.Text,
Images: images,
Sticker: isSticker,
}
}
}
case msg := <-t.Updates:
att := make([]interface{}, 0)
for _, a := range msg.Images {
att = append(att, tamtam.Image{Url: a})
}
text := fmt.Sprintf("*%s*:\n%s", msg.From, msg.Text)
if len(msg.Text) > 0 {
res, err := t.API.Send(tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChatID: msg.To,
ReplyToMessageID: 0,
},
Text: text,
DisableWebPagePreview: false,
ParseMode: "markdown",
})
log.Printf("[TG] Send text: %#v %#v", res, err)
}
if len(msg.Images) > 0 {
for _, i := range msg.Images {
u, err := t.DownloadFile(i)
if err != nil {
log.Printf("[TG] Download image: %#v", err)
continue
}
m := tgbotapi.NewPhotoUpload(msg.To, u)
m.Caption = msg.Sticker
res, err := t.API.Send(m)
log.Printf("[TG] Send image: %#v %#v", res, err)
}
}
}
}
}
func (t *Telegram) DownloadFile(u string) (string, error) {
resp, err := http.Get(u)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Write the body to file
f, err := ioutil.TempFile(os.TempDir(), "tg*")
if err != nil {
return "", err
}
_, err = io.Copy(f, resp.Body)
defer f.Close()
return f.Name(), nil
}
|