main.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "strings"
  7. "gopkg.in/telegram-bot-api.v4"
  8. )
  9. func main() {
  10. // Read the list of banned words from a text file
  11. bannedWordsFile := os.Getenv("BANNED_WORDS_FILE")
  12. bannedWords, err := readBannedWords(bannedWordsFile)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. // Create a Telegram bot client
  17. botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
  18. bot, err := tgbotapi.NewBotAPI(botToken)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. // Set up updates to listen for
  23. u := tgbotapi.NewUpdate(0)
  24. u.Timeout = 60
  25. // Start receiving updates from the bot
  26. updates, err := bot.GetUpdatesChan(u)
  27. // Process incoming updates
  28. for update := range updates {
  29. if update.Message == nil {
  30. continue
  31. }
  32. // Check for group or supergroup messages only
  33. if update.Message.Chat.Type != "group" && update.Message.Chat.Type != "supergroup" {
  34. continue
  35. }
  36. // Check if the message contains any banned words
  37. if containsBannedWords(update.Message.Text, bannedWords) {
  38. // Kick the user
  39. kickConfig := tgbotapi.KickChatMemberConfig{
  40. ChatMemberConfig: tgbotapi.ChatMemberConfig{
  41. ChatID: update.Message.Chat.ID,
  42. UserID: update.Message.From.ID,
  43. },
  44. }
  45. _, err := bot.KickChatMember(kickConfig)
  46. if err != nil {
  47. log.Println("Failed to kick user:", err)
  48. }
  49. // Delete the message
  50. deleteConfig := tgbotapi.DeleteMessageConfig{
  51. ChatID: update.Message.Chat.ID,
  52. MessageID: update.Message.MessageID,
  53. }
  54. _, err = bot.DeleteMessage(deleteConfig)
  55. if err != nil {
  56. log.Println("Failed to delete message:", err)
  57. }
  58. }
  59. }
  60. }
  61. // Read the list of banned words from a text file
  62. func readBannedWords(filename string) ([]string, error) {
  63. content, err := ioutil.ReadFile(filename)
  64. if err != nil {
  65. return nil, err
  66. }
  67. words := strings.Split(string(content), "\n")
  68. return words, nil
  69. }
  70. // Check if the message contains any banned words
  71. func containsBannedWords(text string, bannedWords []string) bool {
  72. for _, word := range bannedWords {
  73. if strings.Contains(text, word) {
  74. return true
  75. }
  76. }
  77. return false
  78. }