From 6bc4e08b15b797cb671e3bbce4183b4844394478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20R=C3=A9thelyi?= <rethelyibalint@gmail.com> Date: Thu, 25 Feb 2021 15:42:37 +0100 Subject: [PATCH] send files --- main.go | 5 ++++ messages/message.go | 2 +- server/server.go | 72 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index bce6a46..9680528 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,15 @@ package main import ( "fmt" "git.sch.bme.hu/insert-epic-projlab-team-name-here/telegram-bot/messages" + "git.sch.bme.hu/insert-epic-projlab-team-name-here/telegram-bot/server" "github.com/go-telegram-bot-api/telegram-bot-api" "gitlab.com/MikeTTh/env" "log" ) func main() { + go server.Serv() + bot, err := tgbotapi.NewBotAPI(env.GetOrDosomething("TOKEN", func() string { panic("GIVE ME TOKEEEEN") })) @@ -16,6 +19,8 @@ func main() { log.Panic(err) } + server.DefaultBot = bot + //bot.Debug = true log.Printf("Authorized on account %s", bot.Self.UserName) diff --git a/messages/message.go b/messages/message.go index 417c010..6689fe0 100644 --- a/messages/message.go +++ b/messages/message.go @@ -31,7 +31,7 @@ var Commands = []Command{ }, }, { - Name: "setdocchannel", + Name: "setdocgroup", Cmd: func(msg *tgbotapi.MessageConfig, upd *tgbotapi.Update) { server.EditChannel(msg.ChatID) msg.Text = "kész is haver 👌" diff --git a/server/server.go b/server/server.go index 2fe917e..b88184d 100644 --- a/server/server.go +++ b/server/server.go @@ -2,12 +2,20 @@ package server import ( "fmt" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "gitlab.com/MikeTTh/env" "io/ioutil" + "mime/multipart" + "net/http" "os" ) var chanId = int64(0) +var DefaultBot *tgbotapi.BotAPI + +const cookie = "magiccookie" + func init() { b, e := ioutil.ReadFile("conf/chan") if e != nil { @@ -28,3 +36,67 @@ func EditChannel(id int64) { fmt.Println(e) } } + +func inputHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + return + } + + cook, e := r.Cookie(cookie) + if e != nil { + http.Error(w, "elbasztad", http.StatusTeapot) + return + } + + if cook.Value != env.GetOrDosomething("MAGIC_COOKIE", func() string { + panic("i have no magic cookie") + }) { + http.Error(w, "elbasztad", http.StatusTeapot) + return + } + + e = r.ParseMultipartForm(1024 * 1024 * 1024) + if e != nil { + http.Error(w, "ezt is te basztad el", http.StatusTeapot) + return + } + + file, fileheader, e := r.FormFile("file") + if e != nil { + http.Error(w, "ezt elbasztad", http.StatusTeapot) + return + } + sendFile(file, fileheader) + file.Close() + +} + +func sendFile(file multipart.File, fileheader *multipart.FileHeader) { + b, e := ioutil.ReadAll(file) + if e != nil { + fmt.Println(e) + return + } + + fb := tgbotapi.FileBytes{ + Name: fileheader.Filename, + Bytes: b, + } + + doc := tgbotapi.NewDocumentUpload(chanId, fb) + _, e = DefaultBot.Send(doc) + if e != nil { + fmt.Println(e) + } + +} + +func Serv() { + mux := http.NewServeMux() + mux.HandleFunc("/in/", inputHandler) + + e := http.ListenAndServe(env.String("LISTEN", ":8080"), mux) + if e != nil { + fmt.Println(e) + } +} -- GitLab