-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
127 lines (105 loc) · 4.56 KB
/
main.py
File metadata and controls
127 lines (105 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import json
import telebot
from telebot import types, util
from decouple import config
from translate import Translator as TranslateTranslator # Assuming you want to use this library
from googletrans import Translator as GoogleTranslator # Renaming to avoid conflicts
from weather import getCurrentWeather
BOT_TOKEN = config("BOT_TOKEN")
bot = telebot.TeleBot(BOT_TOKEN)
weather_keywords = ["weather", "temp", "temperature"]
greetings = ["hello", "hi", "hey"]
whoAreYou = ["who", "what"]
botName = "my bot name"
bot_data = {
"name": ["my bot name"]
}
text_messages = {
"welcome": "Welcome to xxxx Telegram group",
"welcomeNewMember": "Hey, {name}, welcome to our private group",
"sayingGoodbye": "The member {name} left the group",
"leave": "I have been added to a group other than the group I was designed for, bye🧐",
"call": "How can I help? 😀",
"warn": "❌ {name}, you have used one of the forbidden words ❌\n"
"🔴 You have {safeCounter} chances left. If exceeded, you will be expelled 🔴",
"kicked": "👮♂️⚠ The member {name}, ID owner {username}, has been kicked out for violating one of the group rules 👮♂️⚠"
}
commands = {
"translate": ["translate"]
}
def handleNewUserData(message):
id = str(message.new_chat_member.user.id)
name = message.new_chat_member.user.first_name
username = message.new_chat_member.user.username
with open("data.json", "r") as jsonFile:
data = json.load(jsonFile)
users = data.get("users", {})
if id not in users:
print("New user detected!")
users[id] = {"safeCounter": 5, "username": username, "name": name}
print("New user data saved!")
data["users"] = users
with open("data.json", "w") as editedFile:
json.dump(data, editedFile, indent=3)
def handleOffensiveMessage(message):
id = str(message.from_user.id)
name = message.from_user.first_name
username = message.from_user.username
with open("data.json", "r") as jsonFile:
data = json.load(jsonFile)
users = data.get("users", {})
if id not in users:
print("New user detected!")
users[id] = {"safeCounter": 5, "username": username, "name": name}
print("New user data saved!")
users[id]["safeCounter"] -= 1
safeCounterFromJson = users[id]["safeCounter"]
if safeCounterFromJson == 0:
bot.kick_chat_member(message.chat.id, id)
users.pop(id)
bot.send_message(message.chat.id, text_messages["kicked"].format(name=name, username=username))
else:
bot.send_message(message.chat.id, text_messages["warn"].format(name=name, safeCounter=safeCounterFromJson))
data["users"] = users
with open("data.json", "w") as editedFile:
json.dump(data, editedFile, indent=3)
return bot.delete_message(message.chat.id, message.message_id)
@bot.message_handler(commands=["start"])
def startBot(message):
bot.send_message(message.chat.id, text_messages["welcome"])
def is_msg(message):
return True
@bot.message_handler(func=is_msg)
def reply(message):
words = message.text.split()
first_word = words[0].lower()
if first_word in weather_keywords:
report = getCurrentWeather()
bot.send_message(message.chat.id, report or "Failed to get weather!!")
elif first_word in whoAreYou:
bot.reply_to(message, f"I am {botName}")
elif first_word in greetings:
bot.reply_to(message, "Hey, how's it going!")
elif first_word in bot_data["name"]:
bot.reply_to(message, text_messages["call"])
elif first_word in commands["translate"]:
translator = GoogleTranslator()
translation = translator.translate(" ".join(words[1:]), dest="ar")
bot.reply_to(message, translation.text)
else:
bot.reply_to(message, "That's not a command of mine!")
@bot.chat_member_handler()
def handleUserUpdates(message: types.ChatMemberUpdated):
newResponse = message.new_chat_member
if newResponse.status == "member":
handleNewUserData(message)
bot.send_message(message.chat.id, text_messages["welcomeNewMember"].format(name=newResponse.user.first_name))
elif newResponse.status == "left":
bot.send_message(message.chat.id, text_messages["sayingGoodbye"].format(name=newResponse.user.first_name))
@bot.my_chat_member_handler()
def leave(message: types.ChatMemberUpdated):
update = message.new_chat_member
if update.status == "member":
bot.send_message(message.chat.id, text_messages["leave"])
bot.leave_chat(message.chat.id)
bot.infinity_polling(allowed_updates=util.update_types)