all repos — escarbot @ 661438f9080f348c334f56cc65014c5288474afb

Earthbound Café's custom Telegram bot, with lots of cool utilities built-in.

webui/webui.go (view raw)

  1package webui
  2
  3import (
  4	"bytes"
  5	"log"
  6	"net/http"
  7	"strconv"
  8	"text/template"
  9
 10	"github.com/BiRabittoh/escarbot/telegram"
 11)
 12
 13type WebUI struct {
 14	Server   *http.ServeMux
 15	EscarBot *telegram.EscarBot
 16
 17	port string
 18}
 19
 20var indexTemplate = template.Must(template.ParseFiles("index.html"))
 21
 22func indexHandler(bot *telegram.EscarBot) http.HandlerFunc {
 23	return func(w http.ResponseWriter, r *http.Request) {
 24		buf := &bytes.Buffer{}
 25		err := indexTemplate.Execute(buf, bot)
 26		if err != nil {
 27			http.Error(w, err.Error(), http.StatusInternalServerError)
 28			return
 29		}
 30		buf.WriteTo(w)
 31	}
 32}
 33
 34func toggleBotProperty(w http.ResponseWriter, r *http.Request) bool {
 35	r.ParseForm()
 36	res := r.Form.Get("toggle")
 37	http.Redirect(w, r, "/", http.StatusFound)
 38	return res == "on"
 39}
 40
 41func getChatID(w http.ResponseWriter, r *http.Request) (int64, error) {
 42	r.ParseForm()
 43	res := r.Form.Get("id")
 44	http.Redirect(w, r, "/", http.StatusFound)
 45	return strconv.ParseInt(res, 10, 64)
 46}
 47
 48func linksHandler(bot *telegram.EscarBot) http.HandlerFunc {
 49	return func(w http.ResponseWriter, r *http.Request) {
 50		bot.LinkDetection = toggleBotProperty(w, r)
 51	}
 52}
 53
 54func channelForwardHandler(bot *telegram.EscarBot) http.HandlerFunc {
 55	return func(w http.ResponseWriter, r *http.Request) {
 56		bot.ChannelForward = toggleBotProperty(w, r)
 57	}
 58}
 59
 60func adminForwardHandler(bot *telegram.EscarBot) http.HandlerFunc {
 61	return func(w http.ResponseWriter, r *http.Request) {
 62		bot.AdminForward = toggleBotProperty(w, r)
 63	}
 64}
 65
 66func channelHandler(bot *telegram.EscarBot) http.HandlerFunc {
 67	return func(w http.ResponseWriter, r *http.Request) {
 68		res, err := getChatID(w, r)
 69		if err != nil {
 70			log.Println(err)
 71			return
 72		}
 73		bot.ChannelID = res
 74	}
 75}
 76
 77func groupHandler(bot *telegram.EscarBot) http.HandlerFunc {
 78	return func(w http.ResponseWriter, r *http.Request) {
 79		res, err := getChatID(w, r)
 80		if err != nil {
 81			log.Println(err)
 82			return
 83		}
 84		bot.GroupID = res
 85	}
 86}
 87
 88func adminHandler(bot *telegram.EscarBot) http.HandlerFunc {
 89	return func(w http.ResponseWriter, r *http.Request) {
 90		res, err := getChatID(w, r)
 91		if err != nil {
 92			log.Println(err)
 93			return
 94		}
 95		bot.AdminID = res
 96	}
 97}
 98
 99func NewWebUI(port string, bot *telegram.EscarBot) WebUI {
100
101	go telegram.BotPoll(bot)
102
103	r := http.NewServeMux()
104	r.HandleFunc("/", indexHandler(bot))
105	r.HandleFunc("/setLinks", linksHandler(bot))
106	r.HandleFunc("/setChannelForward", channelForwardHandler(bot))
107	r.HandleFunc("/setAdminForward", adminForwardHandler(bot))
108	r.HandleFunc("/setChannel", channelHandler(bot))
109	r.HandleFunc("/setGroup", groupHandler(bot))
110	r.HandleFunc("/setAdmin", adminHandler(bot))
111
112	return WebUI{
113		Server:   r,
114		EscarBot: bot,
115		port:     port,
116	}
117}
118
119func (webui *WebUI) Poll() {
120	log.Println("Serving on port", webui.port)
121	err := http.ListenAndServe(":"+webui.port, webui.Server)
122	if err != nil {
123		log.Fatal(err)
124	}
125}