support uploading stickers
This commit is contained in:
parent
ed13dd5216
commit
01351353e3
56
NTFServer.go
56
NTFServer.go
@ -27,6 +27,8 @@ type NTFServer struct {
|
|||||||
conns map[string]*libnmdc.HubConnection // hubnick -> hubconn
|
conns map[string]*libnmdc.HubConnection // hubnick -> hubconn
|
||||||
verbose bool
|
verbose bool
|
||||||
|
|
||||||
|
callOnMainThread chan func()
|
||||||
|
|
||||||
contentedMaxBytes int64
|
contentedMaxBytes int64
|
||||||
|
|
||||||
// Except the coalesce buffer, that requires a background worker.
|
// Except the coalesce buffer, that requires a background worker.
|
||||||
@ -44,6 +46,7 @@ func NewNTFServer(configFile string, verbose bool) (*NTFServer, error) {
|
|||||||
ret := NTFServer{
|
ret := NTFServer{
|
||||||
configFile: configFile,
|
configFile: configFile,
|
||||||
hubMessages: make(chan upstreamMessage, 0),
|
hubMessages: make(chan upstreamMessage, 0),
|
||||||
|
callOnMainThread: make(chan func(), 0),
|
||||||
conns: make(map[string]*libnmdc.HubConnection),
|
conns: make(map[string]*libnmdc.HubConnection),
|
||||||
coalesceBuffer: make(map[string]time.Time),
|
coalesceBuffer: make(map[string]time.Time),
|
||||||
verbose: verbose,
|
verbose: verbose,
|
||||||
@ -215,6 +218,12 @@ func (this *NTFServer) Run() error {
|
|||||||
log.Fatalf("Upstream update channel closed unexpectedly")
|
log.Fatalf("Upstream update channel closed unexpectedly")
|
||||||
}
|
}
|
||||||
this.HandleHubMessage(hubMsg)
|
this.HandleHubMessage(hubMsg)
|
||||||
|
|
||||||
|
case mtfn, ok := <-this.callOnMainThread:
|
||||||
|
if !ok {
|
||||||
|
log.Fatalf("Synchronisation channel closed unexpectedly")
|
||||||
|
}
|
||||||
|
mtfn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -427,10 +436,23 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
|
|||||||
return this.HandleTelegramUserParted(int64(update.Message.LeftChatMember.ID), update)
|
return this.HandleTelegramUserParted(int64(update.Message.LeftChatMember.ID), update)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
userID := int64(update.Message.From.ID)
|
||||||
|
|
||||||
|
if update.Message.Sticker != nil && this.contentedMaxBytes > 0 {
|
||||||
|
// Sticker
|
||||||
|
go this.uploadStickerWorker(userID, update.Message.Sticker)
|
||||||
|
}
|
||||||
|
|
||||||
if len(update.Message.Text) > 0 {
|
if len(update.Message.Text) > 0 {
|
||||||
|
|
||||||
|
// Intercept some bot commands
|
||||||
|
|
||||||
|
if update.Message.Text == "/userlist" || update.Message.Text == "/userlist@"+this.botName {
|
||||||
|
// Display native userlist inside the groupchat
|
||||||
|
|
||||||
// Find the responsible user's upstream connection
|
// Find the responsible user's upstream connection
|
||||||
userID := int64(update.Message.From.ID)
|
|
||||||
hubNick, ok := this.config.KnownUsers[userID]
|
hubNick, ok := this.config.KnownUsers[userID]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Couldn't send public message for user '%d' unexpectedly missing hub nick!", userID)
|
return fmt.Errorf("Couldn't send public message for user '%d' unexpectedly missing hub nick!", userID)
|
||||||
@ -441,11 +463,6 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
|
|||||||
return fmt.Errorf("Couldn't send public message for user '%d' (%s) unexpectedly missing upstream connection!", userID, hubNick)
|
return fmt.Errorf("Couldn't send public message for user '%d' (%s) unexpectedly missing upstream connection!", userID, hubNick)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intercept some bot commands
|
|
||||||
|
|
||||||
if update.Message.Text == "/userlist" || update.Message.Text == "/userlist@"+this.botName {
|
|
||||||
// Display native userlist inside the groupchat
|
|
||||||
|
|
||||||
usernames, err := this.getUserlistText(conn)
|
usernames, err := this.getUserlistText(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.GroupChatSayHTML("<i>Can't get userlist for you (internal error)</i>")
|
this.GroupChatSayHTML("<i>Can't get userlist for you (internal error)</i>")
|
||||||
@ -482,6 +499,28 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return this.HubSay(userID, sendMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO probably a file/image upload???
|
||||||
|
// TODO support "editing messages" by re-sending them with a ** suffix
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NTFServer) HubSay(userID int64, sendMsg string) error {
|
||||||
|
|
||||||
|
// Find the responsible user's upstream connection
|
||||||
|
hubNick, ok := this.config.KnownUsers[userID]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Couldn't send public message for user '%d' unexpectedly missing hub nick!", userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, ok := this.conns[hubNick]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Couldn't send public message for user '%d' (%s) unexpectedly missing upstream connection!", userID, hubNick)
|
||||||
|
}
|
||||||
|
|
||||||
// Also add it to the coalesce buffer so that we don't replay it from someone else's NMDC connection
|
// Also add it to the coalesce buffer so that we don't replay it from someone else's NMDC connection
|
||||||
this.Coalesce(hubNick, sendMsg)
|
this.Coalesce(hubNick, sendMsg)
|
||||||
|
|
||||||
@ -491,11 +530,6 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
|
|||||||
log.Printf("Failed to deliver message '%s': %s", sendMsg, err.Error())
|
log.Printf("Failed to deliver message '%s': %s", sendMsg, err.Error())
|
||||||
this.GroupChatSayHTML(fmt.Sprintf("<i>Couldn't sync message '%s' because: %s</i>", html.EscapeString(sendMsg), html.EscapeString(err.Error())))
|
this.GroupChatSayHTML(fmt.Sprintf("<i>Couldn't sync message '%s' because: %s</i>", html.EscapeString(sendMsg), html.EscapeString(err.Error())))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO probably a file/image upload???
|
|
||||||
// TODO support "editing messages" by re-sending them with a ** suffix
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,11 @@ func (this *NTFServer) ContentedUploadFromSync(main *telegram.File, thumbs *[]te
|
|||||||
|
|
||||||
func (this *NTFServer) ContentedUploadSync(fileId string, expectSizeBytes int64) (string, error) {
|
func (this *NTFServer) ContentedUploadSync(fileId string, expectSizeBytes int64) (string, error) {
|
||||||
|
|
||||||
|
// If file fits under size limit, take it
|
||||||
|
if expectSizeBytes > this.contentedMaxBytes {
|
||||||
|
return "", errors.New("The file was too large for the image host server (and no smaller thumbnail is available)")
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare to download the file from telegram
|
// Prepare to download the file from telegram
|
||||||
url, err := this.bot.GetFileDirectURL(fileId)
|
url, err := this.bot.GetFileDirectURL(fileId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
43
upload.go
Normal file
43
upload.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"html"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (this *NTFServer) uploadStickerWorker(userID int64, s *telegram.Sticker) {
|
||||||
|
var conUrl string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if int64(s.FileSize) < this.contentedMaxBytes {
|
||||||
|
conUrl, err = this.ContentedUploadSync(s.FileID, int64(s.FileSize))
|
||||||
|
|
||||||
|
} else if s.Thumbnail != nil && int64(s.Thumbnail.FileSize) < this.contentedMaxBytes {
|
||||||
|
conUrl, err = this.ContentedUploadSync(s.Thumbnail.FileID, int64(s.Thumbnail.FileSize))
|
||||||
|
|
||||||
|
} else {
|
||||||
|
err = errors.New("File too big and/or bad thumbnail")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Sticker upload failed: %s", err.Error())
|
||||||
|
this.callOnMainThread <- func() {
|
||||||
|
this.GroupChatSayHTML("<i>Can't upload sticker for native users: " + html.EscapeString(err.Error()) + "</i>")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sticker upload success
|
||||||
|
this.callOnMainThread <- func() {
|
||||||
|
// n.b. this will fail if the user has disconnected by the time the upload completed
|
||||||
|
|
||||||
|
this.HubSay(userID, s.Emoji+" "+conUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user