diff options
-rw-r--r-- | bot.go | 74 |
1 files changed, 62 insertions, 12 deletions
@@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "strings" "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/neonxp/rutina" @@ -12,9 +13,11 @@ import ( ) type message struct { - From string - To int64 - Text string + From string + To int64 + Text string + Images []string + Sticker string } type routing struct { @@ -52,10 +55,23 @@ func main() { if upd.Message.Sender.Username != "" { from = fmt.Sprintf("%s (%s)", upd.Message.Sender.Name, upd.Message.Sender.Username) } + isSticker := "" + images := make([]string, 0) + for _, a := range upd.Message.Body.Attachments { + switch a := a.(type) { + case *tamtam.PhotoAttachment: + images = append(images, a.Payload.Url) + case *tamtam.StickerAttachment: + images = append(images, a.Payload.Url) + isSticker = "STICKER" + } + } tt2tg <- message{ - To: r.TgID, - From: from, - Text: upd.Message.Body.Text, + To: r.TgID, + From: from, + Text: upd.Message.Body.Text, + Images: images, + Sticker: isSticker, } } } @@ -64,8 +80,16 @@ func main() { log.Printf("Unknown type: %#v", upd) } case msg := <-tg2tt: + + text := fmt.Sprintf("[TG] %s: %s", msg.From, msg.Text) + if msg.Sticker != "" { + text = fmt.Sprintf("[TG] %s: [%s]", msg.From, msg.Sticker) + } + if len(msg.Images) > 0 { + text += "[IMAGES] " + strings.Join(msg.Images, " ") + } res, err := tamtamApi.SendMessage(msg.To, msg.To, &tamtam.NewMessageBody{ - Text: fmt.Sprintf("[TG] %s: %s", msg.From, msg.Text), + Text: text, }) log.Printf("[TT] Answer: %#v %#v", res, err) } @@ -82,7 +106,6 @@ func main() { log.Panic(err) } - tgApi.Debug = true u := tgbotapi.NewUpdate(0) u.Timeout = 60 updates, err := tgApi.GetUpdatesChan(u) @@ -101,15 +124,42 @@ func main() { 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, _ := tgApi.GetFileDirectURL(upd.Message.Sticker.Thumbnail.FileID) + images = append(images, s) + } + if upd.Message.Photo != nil { + for _, p := range *upd.Message.Photo { + s, _ := tgApi.GetFileDirectURL(p.FileID) + images = append(images, s) + } + } tg2tt <- message{ - To: r.TTID, - From: from, - Text: upd.Message.Text, + To: r.TTID, + From: from, + Text: upd.Message.Text, + Images: images, + Sticker: isSticker, } } } case msg := <-tt2tg: - res, err := tgApi.Send(tgbotapi.NewMessage(msg.To, fmt.Sprintf("[TT] %s: %s", msg.From, msg.Text))) + att := make([]interface{}, 0) + for _, a := range msg.Images { + att = append(att, tamtam.Image{Url: a}) + } + text := fmt.Sprintf("[TT] %s: %s", msg.From, msg.Text) + if msg.Sticker != "" { + text = fmt.Sprintf("[TT] %s: [%s]", msg.From, msg.Sticker) + } + if len(msg.Images) > 0 { + text += "[IMAGES] " + strings.Join(msg.Images, " ") + } + m := tgbotapi.NewMessage(msg.To, text) + res, err := tgApi.Send(m) log.Printf("[TG] Answer: %#v %#v", res, err) } } |