-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
129 lines (106 loc) Β· 4.4 KB
/
main.py
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
128
129
import json
from pyrogram import Client, filters, types
import configparser
from strings import lang_msg, caps
config = configparser.ConfigParser()
config.read('config.ini')
bot_username = config.get("telegram", "bot_username")
app = Client("capslock")
# Convert function
def convert(caps_str: str) -> str:
new_str = str()
# replace capslock chars with your language:
for char in caps_str.lower():
if caps.get(char):
new_str += caps.get(char)
else:
new_str += char
return new_str
# Save users id's
def save_user(uid: int, save_use: bool = True):
""" save user/chat id to DB """
if save_use:
save_uses()
ids_file = "users.json"
try:
with open(ids_file, "r") as oFile:
users: list = json.load(oFile)
except FileNotFoundError:
users = []
if uid not in users:
users.append(uid)
with open(ids_file, "w") as nFile:
json.dump(users, nFile, indent=4)
# Save number of uses in the converter
def save_uses():
""" save count of uses to DB """
uses_file = "uses.txt"
try:
with open(uses_file) as usesO:
uses = int(usesO.read())
except FileNotFoundError:
with open(uses_file, "w") as usesO:
usesO.write("0")
with open(uses_file) as usesO:
uses = int(usesO.read())
uses += 1
with open(uses_file, "w") as usesO:
usesO.write(str(uses))
# Convert caps messages inside groups
@app.on_message(filters.group & filters.reply & filters.command(["caps", f"caps@{bot_username}"]))
def main(_, msg: types.Message):
msg.reply(convert(msg.reply_to_message.text))
save_user(msg.from_user.id)
# delete the /caps command
try:
msg.delete()
except:
pass
# The caps command in private chats
@app.on_message(filters.private & filters.command("caps"))
def private(_, msg: types.Message):
msg.reply(lang_msg(msg, "caps_private"))
save_user(msg.from_user.id)
# Convert caps messages in private chats
@app.on_message(filters.private & ~filters.command(["start", "caps", "help"]))
def private(_, msg: types.Message):
msg.reply(convert(msg.text))
save_user(msg.from_user.id)
# Convert caps messages with inline mode
@app.on_inline_query()
def inline(_, query: types.InlineQuery):
if not query.query:
return
query.answer([
types.InlineQueryResultArticle(
"π CapsLockBot π ",
types.InputTextMessageContent(convert(query.query)),
description=convert(query.query),
thumb_url="https://telegra.ph/file/a9fe802983616d6e82db3.png")
])
save_user(query.from_user.id)
# Start and help messages
@app.on_message(filters.command(["start", "help"]) & filters.private)
def messages(_, msg: types.Message):
if msg.command == ["start"]:
msg.reply(lang_msg(msg, "start_msg").format(msg.from_user.mention), disable_web_page_preview=True,
reply_markup=types.InlineKeyboardMarkup([
[types.InlineKeyboardButton(f"π {lang_msg(msg, 'inline_converter')} π ",
switch_inline_query_current_chat="RUCUYRHE")],
[types.InlineKeyboardButton(f"β {lang_msg(msg, 'add_to_group')} β",
url=f"https://t.me/{bot_username}?startgroup=true")],
[types.InlineKeyboardButton(f"π {lang_msg(msg, 'source_code')} π ",
url="https://github.com/david-lev/CapsLockBot")]
]))
elif msg.command == ["help"]:
msg.reply(lang_msg(msg, "help_msg").format(bot_username), disable_web_page_preview=True,
reply_markup=types.InlineKeyboardMarkup([
[types.InlineKeyboardButton(f"π {lang_msg(msg, 'inline_converter')} π ",
switch_inline_query_current_chat="RUCUYRHE")],
[types.InlineKeyboardButton(f"β {lang_msg(msg, 'add_to_group')} β",
url=f"https://t.me/{bot_username}?startgroup=true")],
[types.InlineKeyboardButton(f"π {lang_msg(msg, 'source_code')} π ",
url="https://github.com/david-lev/CapsLockBot")]
]))
save_user(msg.from_user.id, False)
app.run()