Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 5a52e635ae21e71ebec99eafcf1844396227f8c1 Author: Wim <wim@42.be> Date: Sun Dec 12 00:35:32 2021 +0000 Update telegram-bot-api to v5 (#1660) diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index 3292e05..80a7185 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -97 +97 @@ import (     "github.com/42wim/matterbridge/bridge/config"   "github.com/42wim/matterbridge/bridge/helper" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"  )    func (b *Btelegram) handleUpdate(rmsg *config.Message, message, posted, edited *tgbotapi.Message) *tgbotapi.Message { @@ -947 +947 @@ func (b *Btelegram) handleQuoting(rmsg *config.Message, message *tgbotapi.Messag  // handleUsername handles the correct setting of the username  func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Message) {   if message.From != nil { - rmsg.UserID = strconv.Itoa(message.From.ID) + rmsg.UserID = strconv.FormatInt(message.From.ID, 10)   if b.GetBool("UseFirstName") {   rmsg.Username = message.From.FirstName   } @@ -1677 +1677 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {   rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text)   // channels don't have (always?) user information. see #410   if message.From != nil { - rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.Itoa(message.From.ID), b.General) + rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.FormatInt(message.From.ID, 10), b.General)   }     b.Log.Debugf("<= Sending message from %s on %s to gateway", rmsg.Username, b.Account) @@ -18042 +18044 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {  // handleDownloadAvatar downloads the avatar of userid from channel  // sends a EVENT_AVATAR_DOWNLOAD message to the gateway if successful.  // logs an error message if it fails -func (b *Btelegram) handleDownloadAvatar(userid int, channel string) { +func (b *Btelegram) handleDownloadAvatar(userid int64, channel string) {   rmsg := config.Message{   Username: "system",   Text: "avatar",   Channel: channel,   Account: b.Account, - UserID: strconv.Itoa(userid), + UserID: strconv.FormatInt(userid, 10),   Event: config.EventAvatarDownload,   Extra: make(map[string][]interface{}),   }   - if _, ok := b.avatarMap[strconv.Itoa(userid)]; !ok { - photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1}) - if err != nil { - b.Log.Errorf("Userprofile download failed for %#v %s", userid, err) - } + if _, ok := b.avatarMap[strconv.FormatInt(userid, 10)]; ok { + return + }   - if len(photos.Photos) > 0 { - photo := photos.Photos[0][0] - url := b.getFileDirectURL(photo.FileID) - name := strconv.Itoa(userid) + ".png" - b.Log.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize) + photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1}) + if err != nil { + b.Log.Errorf("Userprofile download failed for %#v %s", userid, err) + }   - err := helper.HandleDownloadSize(b.Log, &rmsg, name, int64(photo.FileSize), b.General) - if err != nil { - b.Log.Error(err) - return - } - data, err := helper.DownloadFile(url) - if err != nil { - b.Log.Errorf("download %s failed %#v", url, err) - return - } - helper.HandleDownloadData(b.Log, &rmsg, name, rmsg.Text, "", data, b.General) - b.Remote <- rmsg + if len(photos.Photos) > 0 { + photo := photos.Photos[0][0] + url := b.getFileDirectURL(photo.FileID) + name := strconv.FormatInt(userid, 10) + ".png" + b.Log.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize) + + err := helper.HandleDownloadSize(b.Log, &rmsg, name, int64(photo.FileSize), b.General) + if err != nil { + b.Log.Error(err) + return   } + data, err := helper.DownloadFile(url) + if err != nil { + b.Log.Errorf("download %s failed %#v", url, err) + return + } + helper.HandleDownloadData(b.Log, &rmsg, name, rmsg.Text, "", data, b.General) + b.Remote <- rmsg   }  }   @@ -2727 +2747 @@ func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Messa   name = message.Document.FileName   text = " " + message.Document.FileName + " : " + url   case message.Photo != nil: - photos := *message.Photo + photos := message.Photo   size = photos[len(photos)-1].FileSize   text, name, url = b.getDownloadInfo(photos[len(photos)-1].FileID, "", true)   } @@ -33111 +33315 @@ func (b *Btelegram) handleDelete(msg *config.Message, chatid int64) (string, err   if msg.ID == "" {   return "", nil   } +   msgid, err := strconv.Atoi(msg.ID)   if err != nil {   return "", err   } - _, err = b.c.DeleteMessage(tgbotapi.DeleteMessageConfig{ChatID: chatid, MessageID: msgid}) + + cfg := tgbotapi.NewDeleteMessage(chatid, msgid) + _, err = b.c.Send(cfg) +   return "", err  }   @@ -38323 +38923 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) string {   }   switch filepath.Ext(fi.Name) {   case ".jpg", ".jpe", ".png": - pc := tgbotapi.NewPhotoUpload(chatid, file) + pc := tgbotapi.NewPhoto(chatid, file)   pc.Caption, pc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)   c = pc   case ".mp4", ".m4v": - vc := tgbotapi.NewVideoUpload(chatid, file) + vc := tgbotapi.NewVideo(chatid, file)   vc.Caption, vc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)   c = vc   case ".mp3", ".oga": - ac := tgbotapi.NewAudioUpload(chatid, file) + ac := tgbotapi.NewAudio(chatid, file)   ac.Caption, ac.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)   c = ac   case ".ogg": - voc := tgbotapi.NewVoiceUpload(chatid, file) + voc := tgbotapi.NewVoice(chatid, file)   voc.Caption, voc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)   c = voc   default: - dc := tgbotapi.NewDocumentUpload(chatid, file) + dc := tgbotapi.NewDocument(chatid, file)   dc.Caption, dc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)   c = dc   } @@ -4397 +4457 @@ func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Messa   indexMovedBy := 0     // for now only do URL replacements - for _, e := range *message.Entities { + for _, e := range message.Entities {   if e.Type == "text_link" {   url, err := e.ParseURL()   if err != nil { diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 199a76a..d696e9b 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -97 +97 @@ import (   "github.com/42wim/matterbridge/bridge"   "github.com/42wim/matterbridge/bridge/config"   "github.com/42wim/matterbridge/bridge/helper" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"  )    const ( @@ -4911 +497 @@ func (b *Btelegram) Connect() error {   }   u := tgbotapi.NewUpdate(0)   u.Timeout = 60 - updates, err := b.c.GetUpdatesChan(u) - if err != nil { - b.Log.Debugf("%#v", err) - return err - } + updates := b.c.GetUpdatesChan(u)   b.Log.Info("Connection succeeded")   go b.handleRecv(updates)   return nil diff --git a/go.mod b/go.mod index a497bff..444eaf2 100644 --- a/go.mod +++ b/go.mod @@ -107 +107 @@ require (   github.com/d5/tengo/v2 v2.10.0   github.com/davecgh/go-spew v1.1.1   github.com/fsnotify/fsnotify v1.5.1 - github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20200524105306-7434b0456e81 + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.0   github.com/gomarkdown/markdown v0.0.0-20211207152620-5d6539fd8bfc   github.com/google/gops v0.3.22   github.com/gorilla/schema v1.2.0 @@ -1097 +1096 @@ require (   github.com/spf13/jwalterweatherman v1.1.0 // indirect   github.com/spf13/pflag v1.0.5 // indirect   github.com/subosito/gotenv v1.2.0 // indirect - github.com/technoweenie/multipartstreamer v1.0.1 // indirect   github.com/tinylib/msgp v1.1.6 // indirect   github.com/valyala/bytebufferpool v1.0.0 // indirect   github.com/valyala/fasttemplate v1.2.1 // indirect diff --git a/go.sum b/go.sum index ea0194e..acbd40d 100644 --- a/go.sum +++ b/go.sum @@ -3938 +3938 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB  github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=  github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=  github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= -github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20200524105306-7434b0456e81 h1:FdZThbRF0R+2qgyBl3KCVNWWBmKm68E+stT3rnQ02Ww= -github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20200524105306-7434b0456e81/go.mod h1:lDm2E64X4OjFdBUA4hlN4mEvbSitvhJdKw7rsA8KHgI= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.0 h1:BtndtqqCQfPsL2uMkYmduOip1+dPcSmh40l82mBUPKk= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.0/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=  github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=  github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=  github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= @@ -12238 +12236 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP  github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=  github.com/tebeka/snowball v0.4.2/go.mod h1:4IfL14h1lvwZcp1sfXuuc7/7yCsvVffTWxWxCLfFpYg=  github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8= -github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= -github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=  github.com/throttled/throttled v2.2.5+incompatible/go.mod h1:0BjlrEGQmvxps+HuXLsyRdqpSRvJpq0PNIsOtqP9Nos=  github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=  github.com/tidwall/gjson v1.9.2/go.mod h1:2tcKM/KQ/GjiTN7mfTL/HdNmef9Q6AZLaSK2RdfvSjw=