Advertisement
vielfrass

Untitled

Jan 20th, 2021
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "net/http"
  7.  
  8.     "github.com/BurntSushi/toml"
  9.     "github.com/fatih/color"
  10.     "github.com/go-chi/chi"
  11.     "github.com/go-chi/chi/middleware"
  12.     tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
  13. )
  14.  
  15. var (
  16.     yellow = color.New(color.FgYellow).SprintFunc()
  17.     red    = color.New(color.FgRed).SprintFunc()
  18.     g      = color.New(color.FgGreen, color.Bold).SprintFunc()
  19.     b      = color.New(color.FgBlue, color.Bold).SprintFunc()
  20. )
  21.  
  22. func bot(token string) {
  23.     bot, err := tgbotapi.NewBotAPI(token)
  24.     if err != nil {
  25.         log.Panic(err)
  26.     }
  27.     bot.Debug = false
  28.     log.Printf("Authorized on account %s", bot.Self.UserName)
  29.     u := tgbotapi.NewUpdate(0)
  30.     u.Timeout = 60
  31.     updates, err := bot.GetUpdatesChan(u)
  32.     for update := range updates {
  33.         if update.Message == nil { // ignore any non-Message Updates
  34.             continue
  35.         }
  36.         fmt.Printf("%s %s %s %s\n", red("message>"), yellow(update.Message.Chat.ID, ">"), b(update.Message.From.UserName+">"), g(update.Message.Text))
  37.         msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
  38.         msg.ReplyToMessageID = update.Message.MessageID
  39.         bot.Send(msg)
  40.     }
  41.  
  42. }
  43.  
  44. var token string
  45.  
  46. func edit(w http.ResponseWriter, r *http.Request) {
  47.     token = r.FormValue("token")
  48.     fmt.Printf("%s %s %s\n", yellow("edit>"), b("token>"), g(token))
  49.     w.Write([]byte(token))
  50. }
  51.  
  52. type tomlConfig struct {
  53.     Token string
  54. }
  55.  
  56. func main() {
  57.     var config tomlConfig
  58.     if _, err := toml.DecodeFile("token.toml", &config); err != nil {
  59.         fmt.Println(err)
  60.     }
  61.     token = config.Token
  62.     fmt.Printf("%s %s\n", yellow("token:"), g(token))
  63.     go bot(token)
  64.     r := chi.NewRouter()
  65.     r.Use(middleware.Logger)
  66.     r.Get("/edit", edit) //http://localhost:3000/edit?token=1234567:fdsferfsqqqweeasfweriopj
  67.     http.ListenAndServe(":3000", r)
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement