forked from JosXa/BotListBot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.py
More file actions
135 lines (106 loc) · 3.76 KB
/
Copy pathhelpers.py
File metadata and controls
135 lines (106 loc) · 3.76 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
128
129
130
131
132
133
134
135
import logging
import re
import maya
from PIL import Image
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ParseMode
import captions
import settings
import util
from dialog import messages
from settings import SELF_CHANNEL_USERNAME
from logzero import logger as log
def slang_datetime(dt) -> str:
maya_date = maya.MayaDT(dt.timestamp())
return maya_date.slang_time()
def find_bots_in_text(text: str, first=False):
matches = re.findall(settings.REGEX_BOT_ONLY, text)
if not matches:
return None
try:
return matches[0] if first else matches
except:
return None
def format_name(entity):
res = entity.first_name or ""
if entity.first_name and entity.last_name:
res += " " + entity.last_name
elif entity.last_name:
res = entity.last_name
return res
def validate_username(username: str):
if len(username) < 3:
return False
if username[0] != '@':
username = '@' + username
match = re.match(settings.REGEX_BOT_ONLY, username)
return username if match else False
def get_commands():
commands = ""
try:
with open('files/commands.txt', 'rb') as file:
for command in file.readlines():
commands += '/' + command.decode("utf-8")
return commands
except FileNotFoundError:
log.error("File could not be opened.")
def get_channel():
from models import Channel
try:
return Channel.get(Channel.username == SELF_CHANNEL_USERNAME)
except Channel.DoesNotExist:
return False
def botlist_url_for_category(category):
return 'http://t.me/{}/{}'.format(get_channel().username, category.current_message_id)
def format_keyword(kw):
kw = kw[1:] if kw[0] == '#' else kw
kw = kw.replace(' ', '_')
kw = kw.replace('-', '_')
kw = kw.replace('\'', '_')
kw = kw.lower()
return kw
def reroute_private_chat(bot, update, quote, action, message, redirect_message=None,
reply_markup=None):
cid = update.effective_chat.id
mid = update.effective_message.message_id
if redirect_message is None:
redirect_message = messages.REROUTE_PRIVATE_CHAT
if util.is_group_message(update):
update.message.reply_text(
redirect_message,
quote=quote,
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton(
captions.SWITCH_PRIVATE,
url="https://t.me/{}?start={}".format(
settings.SELF_BOT_NAME,
action)),
InlineKeyboardButton('🔎 Switch to inline', switch_inline_query=action)
]]
))
else:
if mid:
bot.send_or_edit(cid, message, mid, reply_markup=reply_markup)
else:
update.message.reply_text(message, quote=quote, parse_mode=ParseMode.MARKDOWN,
reply_markup=reply_markup)
def make_sticker(filename, out_file, max_height=512, transparent=True):
image = Image.open(filename)
# resize sticker to match new max height
# optimize image dimensions for stickers
if max_height == 512:
resize_ratio = min(512 / image.width, 512 / image.height)
image = image.resize((int(image.width * resize_ratio), int(image.height * resize_ratio)))
else:
image.thumbnail((512, max_height), Image.ANTIALIAS)
if transparent:
canvas = Image.new('RGBA', (512, image.height))
else:
canvas = Image.new('RGB', (512, image.height), color='white')
pos = (0, 0)
try:
canvas.paste(image, pos, mask=image)
except ValueError:
canvas.paste(image, pos)
canvas.save(out_file)
return out_file