Skip to content
Snippets Groups Projects
Commit 6bc4e08b authored by Réthelyi Bálint's avatar Réthelyi Bálint :no_mouth:
Browse files

send files

parent 85d776c2
No related branches found
No related tags found
No related merge requests found
...@@ -3,12 +3,15 @@ package main ...@@ -3,12 +3,15 @@ package main
import ( import (
"fmt" "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/messages"
"git.sch.bme.hu/insert-epic-projlab-team-name-here/telegram-bot/server"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
"gitlab.com/MikeTTh/env" "gitlab.com/MikeTTh/env"
"log" "log"
) )
func main() { func main() {
go server.Serv()
bot, err := tgbotapi.NewBotAPI(env.GetOrDosomething("TOKEN", func() string { bot, err := tgbotapi.NewBotAPI(env.GetOrDosomething("TOKEN", func() string {
panic("GIVE ME TOKEEEEN") panic("GIVE ME TOKEEEEN")
})) }))
...@@ -16,6 +19,8 @@ func main() { ...@@ -16,6 +19,8 @@ func main() {
log.Panic(err) log.Panic(err)
} }
server.DefaultBot = bot
//bot.Debug = true //bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName) log.Printf("Authorized on account %s", bot.Self.UserName)
......
...@@ -31,7 +31,7 @@ var Commands = []Command{ ...@@ -31,7 +31,7 @@ var Commands = []Command{
}, },
}, },
{ {
Name: "setdocchannel", Name: "setdocgroup",
Cmd: func(msg *tgbotapi.MessageConfig, upd *tgbotapi.Update) { Cmd: func(msg *tgbotapi.MessageConfig, upd *tgbotapi.Update) {
server.EditChannel(msg.ChatID) server.EditChannel(msg.ChatID)
msg.Text = "kész is haver 👌" msg.Text = "kész is haver 👌"
......
...@@ -2,12 +2,20 @@ package server ...@@ -2,12 +2,20 @@ package server
import ( import (
"fmt" "fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"gitlab.com/MikeTTh/env"
"io/ioutil" "io/ioutil"
"mime/multipart"
"net/http"
"os" "os"
) )
var chanId = int64(0) var chanId = int64(0)
var DefaultBot *tgbotapi.BotAPI
const cookie = "magiccookie"
func init() { func init() {
b, e := ioutil.ReadFile("conf/chan") b, e := ioutil.ReadFile("conf/chan")
if e != nil { if e != nil {
...@@ -28,3 +36,67 @@ func EditChannel(id int64) { ...@@ -28,3 +36,67 @@ func EditChannel(id int64) {
fmt.Println(e) 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)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment