eljur/worker.go
2024-01-27 21:44:51 +03:00

205 lines
4.3 KiB
Go

package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
)
type board_item struct {
active bool
dates date_pair
title string
content string
attachments []outline
creator string
receivers string
edit_pattern []int
unique bool
}
type date_pair struct {
begin string
end string
}
type outline struct {
title string
link string
doctype string
}
func load_env_vars() (string, string, string, string, string) {
LOGIN := os.Getenv("ELJUR_LOGIN")
PASSWORD := os.Getenv("ELJUR_PASSWORD")
DOMAIN := os.Getenv("ELJUR_DOMAIN")
TG_TOKEN := os.Getenv("TG_TOKEN")
TG_ID := os.Getenv("TG_ID")
TG_API_URL := os.Getenv("TG_API_URL")
return LOGIN, PASSWORD, fmt.Sprintf("https://%s.eljur.ru/", DOMAIN), TG_ID, TG_API_URL + "bot" + TG_TOKEN + `/`
}
func update_file(val, path string) {
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
log.Fatal("cant update ", path, " : ", err)
os.Exit(1)
} else {
file.Truncate(0)
file.Write([]byte(val))
file.Close()
log.Println(path, "updated")
}
defer file.Close()
}
func get_file(path string) string {
var val, err = os.ReadFile(path)
if err != nil {
log.Fatal("cant get", path, ":", err)
return " "
} else {
log.Println("data/table.html got")
return string(val)
}
}
// Send photo & docs
func send_document(path, TELEGRAM_API_URL, TELEGRAM_CHAT_ID string) {
url := TELEGRAM_API_URL + "sendDocument"
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Open the file
f, err := os.Open(path)
if err != nil {
log.Fatalf("cannot open file: %v", err)
}
defer f.Close()
// Add file as stream of bytes
fw, err := w.CreateFormFile("document", f.Name())
if err != nil {
log.Fatalf("cannot create form file: %v", err)
}
_, err = io.Copy(fw, f)
if err != nil {
log.Fatalf("cannot write file to form file: %v", err)
}
// Add other fields
err = w.WriteField("chat_id", TELEGRAM_CHAT_ID)
if err != nil {
log.Fatalf("cannot write chat_id to form: %v", err)
}
// Close the writer
err = w.Close()
if err != nil {
log.Fatalf("cannot close writer: %v", err)
}
// Create a client
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("POST", url, &b)
if err != nil {
log.Fatalf("cannot create request: %v", err)
}
// Set the content type, this is important
req.Header.Set("Content-Type", w.FormDataContentType())
// Submit the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("HTTP error occurred: %v", err)
}
defer res.Body.Close()
// Check the response
if res.StatusCode == http.StatusOK {
log.Println("photo sent")
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("cannot read response body: %v", err)
}
log.Printf("can't send photo: %d - %s", res.StatusCode, string(body))
}
}
func send_photo(path, TELEGRAM_API_URL, TELEGRAM_CHAT_ID string) {
url := TELEGRAM_API_URL + "sendPhoto"
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Open the file
f, err := os.Open(path)
if err != nil {
log.Fatalf("cannot open file: %v", err)
}
defer f.Close()
// Add file as stream of bytes
fw, err := w.CreateFormFile("photo", f.Name())
if err != nil {
log.Fatalf("cannot create form file: %v", err)
}
_, err = io.Copy(fw, f)
if err != nil {
log.Fatalf("cannot write file to form file: %v", err)
}
// Add other fields
err = w.WriteField("chat_id", TELEGRAM_CHAT_ID)
if err != nil {
log.Fatalf("cannot write chat_id to form: %v", err)
}
// Close the writer
err = w.Close()
if err != nil {
log.Fatalf("cannot close writer: %v", err)
}
// Create a client
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("POST", url, &b)
if err != nil {
log.Fatalf("cannot create request: %v", err)
}
// Set the content type, this is important
req.Header.Set("Content-Type", w.FormDataContentType())
// Submit the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("HTTP error occurred: %v", err)
}
defer res.Body.Close()
// Check the response
if res.StatusCode == http.StatusOK {
log.Println("photo sent")
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("cannot read response body: %v", err)
}
log.Printf("can't send photo: %d - %s", res.StatusCode, string(body))
}
}