diff options
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r-- | telegram/handlers.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go index 45f10fd..9b86ac7 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -2,6 +2,8 @@ package telegram import ( "fmt" + "os" + "path/filepath" "strconv" "strings" "sync" @@ -43,6 +45,16 @@ func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex { return lock } +func (c *Client) cleanTempFile(path string) { + os.Remove(path) + + dir := filepath.Dir(path) + dirName := filepath.Base(dir) + if strings.HasPrefix(dirName, "telegabber-") { + os.Remove(dir) + } +} + func (c *Client) updateHandler() { listener := c.client.GetListener() defer listener.Close() @@ -111,6 +123,18 @@ func (c *Client) updateHandler() { uhOh() } c.updateAuthorizationState(typedUpdate) + case client.TypeUpdateMessageSendSucceeded: + typedUpdate, ok := update.(*client.UpdateMessageSendSucceeded) + if !ok { + uhOh() + } + c.updateMessageSendSucceeded(typedUpdate) + case client.TypeUpdateMessageSendFailed: + typedUpdate, ok := update.(*client.UpdateMessageSendFailed) + if !ok { + uhOh() + } + c.updateMessageSendFailed(typedUpdate) default: // log only handled types continue @@ -233,3 +257,17 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat c.forceClose() } } + +// clean uploaded files +func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucceeded) { + file, _ := c.contentToFile(update.Message.Content) + if file != nil && file.Local != nil { + c.cleanTempFile(file.Local.Path) + } +} +func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) { + file, _ := c.contentToFile(update.Message.Content) + if file != nil && file.Local != nil { + c.cleanTempFile(file.Local.Path) + } +} |