Spaces:
Sleeping
Sleeping
Download api/bot.go from Thaimozhi/Go-Filter-Bot: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/Thaimozhi/Go-Filter-Bot/resolve/refs%2Fpr%2F2/api/bot.go
- Command line
-
hf download hf://spaces/Thaimozhi/Go-Filter-Bot@refs/pr/2/api/bot.go
-
curl -L -o bot.go https://huggingface.co/spaces/Thaimozhi/Go-Filter-Bot/resolve/refs%2Fpr%2F2/api/bot.go
1.42 kB
| package api | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "io" | |
| "net/http" | |
| "os" | |
| "path" | |
| "strings" | |
| "github.com/Jisin0/Go-Filter-Bot/plugins" | |
| "github.com/Jisin0/Go-Filter-Bot/utils" | |
| "github.com/PaulSonOfLars/gotgbot/v2" | |
| ) | |
| var ( | |
| allowedTokens = strings.Split(os.Getenv("BOT_TOKEN"), " ") | |
| lenAllowedTokens = len(allowedTokens) | |
| ) | |
| const ( | |
| statusCodeSuccess = 200 | |
| ) | |
| // Handles all incoming traffic from webhooks. | |
| func Bot(w http.ResponseWriter, r *http.Request) { | |
| url := r.URL.Path | |
| _, botToken := path.Split(url) | |
| bot, _ := gotgbot.NewBot(botToken, &gotgbot.BotOpts{DisableTokenCheck: true}) | |
| // Delete the webhook incase token is unauthorized. | |
| if lenAllowedTokens > 0 && allowedTokens[0] != "" && !utils.Contains(allowedTokens, botToken) { | |
| bot.DeleteWebhook(&gotgbot.DeleteWebhookOpts{}) //nolint:errcheck // It doesn't matter if it errors | |
| w.WriteHeader(statusCodeSuccess) | |
| return | |
| } | |
| var update gotgbot.Update | |
| body, err := io.ReadAll(r.Body) | |
| if err != nil { | |
| fmt.Fprintf(w, "Error reading request body: %v", err) | |
| w.WriteHeader(statusCodeSuccess) | |
| return | |
| } | |
| err = json.Unmarshal(body, &update) | |
| if err != nil { | |
| fmt.Println("failed to unmarshal body ", err) | |
| w.WriteHeader(statusCodeSuccess) | |
| return | |
| } | |
| err = plugins.Dispatcher.ProcessUpdate(bot, &update, map[string]interface{}{}) | |
| if err != nil { | |
| fmt.Printf("error while processing update: %v", err) | |
| } | |
| w.WriteHeader(statusCodeSuccess) | |
| } | |