Commit 936026e3 authored by Markus Seidl's avatar Markus Seidl

Added photo handling (hopefully).

parent eeef0103
......@@ -7,6 +7,7 @@ import (
"log"
"os"
"rgifbot/config"
"rgifbot/encoder/ffmpeg"
"strings"
"time"
......@@ -20,7 +21,9 @@ type Message struct {
var Config config.BotConfig
func CCTelegramLoop(handler func(message Message) error) {
var emptyTimestamp time.Time
func CCTelegramLoop(handler func(message Message) (timestamp time.Time, err error)) {
Config = config.LoadConfig()
if Config.KnownIds == nil {
......@@ -37,71 +40,120 @@ func CCTelegramLoop(handler func(message Message) error) {
return
}
b.Handle(tb.OnText, func(m *tb.Message) {
registerTextHandler(b, handler)
registerVideoHandler(b, handler)
registerPhotoHandler(b, handler)
registerExitCommand(b)
registerPasswordCommand(b)
b.Start()
}
func registerPhotoHandler(b *tb.Bot, handler func(message Message) (timestamp time.Time, err error)) {
b.Handle(tb.OnPhoto, func(m *tb.Message) {
if !isAllowed(b, m) {
return
}
log.Printf("Received message <%s>, <%d>", m.Text, len(m.Entities))
text := m.Text
if strings.HasPrefix(text, "http") {
err := handler(Message{Url: text})
SendMediaResponse(err, b, m)
_, _ = b.Send(m.Sender, "Downloading...")
f, err := ioutil.TempFile("", "telegram_file")
if m.Photo.InCloud() {
err = b.Download(&m.Photo.File, f.Name())
if err != nil {
sendMediaResponse(err, b, m, emptyTimestamp)
return
}
} else {
sendMediaResponse(errors.New("UnknownMediaLocation"), b, m, emptyTimestamp)
return
}
f = ffmpeg.Convert(f)
timestamp, err := handler(Message{Url: "", File: f})
sendMediaResponse(err, b, m, timestamp)
})
}
func registerPasswordCommand(b *tb.Bot) {
b.Handle("/password", func(m *tb.Message) {
if isAllowed(b, m) {
_, _ = b.Send(m.Sender, "You are already authenticated.")
return
}
log.Printf("Login attempt: %s, payload: %s", m.Text, m.Payload)
pass := m.Payload
if pass != Config.Password {
_, _ = b.Send(m.Sender, "Wrong!")
return
}
Config.KnownIds[m.Sender.ID] = true
_, _ = b.Send(m.Sender, "Welcome back! 😍")
config.StoreConfig(Config)
})
}
func registerExitCommand(b *tb.Bot) {
b.Handle("/exit", func(m *tb.Message) {
if !isAllowed(b, m) {
return
}
_, _ = b.Send(m.Sender, "Please send me gifs, videos or 9Gag links 🤡")
os.Exit(1)
})
}
func registerVideoHandler(b *tb.Bot, handler func(message Message) (timestamp time.Time, err error)) {
b.Handle(tb.OnVideo, func(m *tb.Message) {
if !isAllowed(b, m) {
return
}
_, _ = b.Send(m.Sender, "Downloading...")
f, err := ioutil.TempFile("", "telegram_file")
if m.Video.InCloud() {
err = b.Download(&m.Video.File, f.Name())
if err != nil {
SendMediaResponse(err, b, m)
sendMediaResponse(err, b, m, emptyTimestamp)
return
}
} else {
SendMediaResponse(errors.New("UnknownMediaLocation"), b, m)
sendMediaResponse(errors.New("UnknownMediaLocation"), b, m, emptyTimestamp)
return
}
err = handler(Message{Url: "", File: f})
SendMediaResponse(err, b, m)
timestamp, err := handler(Message{Url: "", File: f})
sendMediaResponse(err, b, m, timestamp)
})
}
b.Handle("/exit", func(m *tb.Message) {
func registerTextHandler(b *tb.Bot, handler func(message Message) (timestamp time.Time, err error)) {
b.Handle(tb.OnText, func(m *tb.Message) {
if !isAllowed(b, m) {
return
}
log.Printf("Received message <%s>, <%d>", m.Text, len(m.Entities))
os.Exit(1)
})
text := m.Text
if strings.HasPrefix(text, "http") {
timestamp, err := handler(Message{Url: text})
sendMediaResponse(err, b, m, timestamp)
b.Handle("/password", func(m *tb.Message) {
log.Printf("Login attempt: %s, payload: %s", m.Text, m.Payload)
pass := m.Payload
if pass != Config.Password {
_, _ = b.Send(m.Sender, "Wrong!")
return
}
Config.KnownIds[m.Sender.ID] = true
_, _ = b.Send(m.Sender, "Welcome back! 😍")
config.StoreConfig(Config)
_, _ = b.Send(m.Sender, "Please send me photos, videos or 9Gag links 🤡")
})
b.Start()
}
func SendMediaResponse(err error, b *tb.Bot, m *tb.Message) {
func sendMediaResponse(err error, b *tb.Bot, m *tb.Message, timestamp time.Time) {
if err == nil {
_, _ = b.Send(m.Sender, "Understood 🤩.")
_, _ = b.Send(m.Sender, fmt.Sprintf("Understood 🤩 (%d).", timestamp.UnixNano()))
} else {
_, _ = b.Send(m.Sender, fmt.Sprintf("No clue... 😒 <%s>", err.Error()))
}
......
package ffmpeg
import (
"io/ioutil"
"log"
"os"
"os/exec"
)
const ffmpegLocation = "/usr/bin/ffmpeg"
func Convert(infile *os.File) (outfile *os.File) {
_ = infile.Sync() // make sure everything is on disk
// we are making a switch, so the infile is supposed to be closed outside, but we close it in this function
// on the other side we are not closing outfile, which is then closed outside.
outfile, _ = ioutil.TempFile("", "ffmpeg")
var args []string
// ffmpeg -loop 1 -i koala.png -c:v libx264 -t 5 -pix_fmt yuv420p -vf scale=1920:1080 out.mp4
args = append(args, "-loop", "1", "-i",
infile.Name(),
"-c:v", "libx264", "-t", "5", "-pix_fmt", "yuv420p", "-vf", "scale=1920:1080",
outfile.Name(),
)
app := ffmpegLocation
cmd := &exec.Cmd{
Path: app,
Args: args,
}
log.Printf("Executing: %s %v", app, args)
temp, err := cmd.CombinedOutput()
log.Print(err)
log.Print(string(temp))
_, _ = outfile.Seek(0, 0)
fileInfo, _ := os.Stat(outfile.Name())
log.Printf("Conversion from <%s> to <%s> produced file with length <%d>\n", infile.Name(), outfile.Name(),
fileInfo.Size())
_ = infile.Close()
return
}
......@@ -18,9 +18,11 @@ func main() {
db := simpledb.Database{Directory: dir}
go func() {
bot.CCTelegramLoop(func(message bot.Message) (err error) {
bot.CCTelegramLoop(func(message bot.Message) (timestamp time.Time, err error) {
var file *os.File
var emptyTimestamp time.Time
if message.Url != "" {
file, err = scrapper.Scrap(message.Url)
if err != nil {
......@@ -29,16 +31,17 @@ func main() {
} else if message.File != nil {
file = message.File
} else {
return errors.New("UnknownMessageType")
return emptyTimestamp, errors.New("UnknownMessageType")
}
defer file.Close()
err = db.Store("", time.Now(), file)
timestamp = time.Now()
err = db.Store("", timestamp, file)
if err != nil {
return err
return emptyTimestamp, err
}
return nil
return
})
}()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment