-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
179 lines (158 loc) · 5.75 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from os import getenv
from uuid import uuid4
import telebot
from telebot import types
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from templates import templates
from helper import make_meme, generate_example_text
from db import base, User, TemplateTotalUse, Memes
from messages import help_msg, welcome
TOKEN = getenv("TOKEN")
bot = telebot.TeleBot(TOKEN)
engine = create_engine("sqlite:///data.db/?check_same_thread=False")
base.metadata.create_all(engine)
Session = sessionmaker(engine)
session = Session()
# start
@bot.message_handler(commands=["start", "help"])
def receive_start(message):
chat_id = message.chat.id
user = User(chat_id)
if session.query(User).filter_by(chat_id=chat_id).first() is None:
session.add(user)
session.commit()
bot.send_message(chat_id, welcome)
bot.send_message(chat_id, help_msg, parse_mode="Markdown")
# make a meme
@bot.message_handler(commands=["make"])
def receive_make_meme(message):
chat_id = message.chat.id
try:
args = message.text[message.text.index(" ")+1:]
except IndexError:
bot.send_message(chat_id, "Please enter the template ID and the text.")
return
template_id, *text = args.split(",")
try:
template_id = int(template_id)
except ValueError:
bot.send_message(chat_id, "Template ID should be an integer.")
else:
if template_id in templates:
if (
session.query(TemplateTotalUse)
.filter_by(template_id=template_id)
.first()
is None
):
use = TemplateTotalUse(template_id)
session.add(use)
session.commit()
else:
use = (
session.query(TemplateTotalUse)
.filter_by(template_id=template_id)
.first()
)
use.use = TemplateTotalUse.use + 1
session.commit()
if chat_id > 0:
callback_data = ",".join(["store", str(template_id), *text])
if len(callback_data.encode("utf-8")) <= 64:
kb = types.InlineKeyboardMarkup()
kb.row(
types.InlineKeyboardButton(
"Store and publish", callback_data=callback_data
)
)
bot.send_photo(chat_id, make_meme(template_id, text), reply_markup=kb)
else: # size of callback_data exceeds telegram API limit (64 bytes)
bot.send_message(chat_id, "Unable to publish: The length of text is too long to store.")
bot.send_photo(chat_id, make_meme(template_id, text))
else:
bot.send_photo(chat_id, make_meme(template_id, text))
else:
bot.send_message(chat_id, "Template not found.")
# show template usage
@bot.message_handler(commands=["template"])
def receive_get_template(message):
chat_id = message.chat.id
try:
template_id = int(message.text.split(" ")[1])
except ValueError:
bot.send_message(chat_id, "Please enter a valid template id.")
except IndexError:
bot.send_message(chat_id, "Please enter a template ID.")
else:
if template_id in templates:
bot.send_photo(
chat_id, make_meme(template_id, generate_example_text(template_id))
)
else:
bot.send_message(chat_id, "Template not found.")
# get published meme by its ID
@bot.message_handler(commands=["publish"])
def receive_send_published(message):
chat_id = message.chat.id
try:
ID = message.text.split(" ")[1]
except IndexError:
bot.send_message(chat_id, "Please enter a meme ID.")
return
try:
ID = int(ID)
except ValueError:
bot.send_message(chat_id, "Please enter a valid meme ID.")
except IndexError:
bot.send_message(chat_id, "Please enter a meme ID.")
else:
meme = session.query(Memes).filter_by(ID=ID).first()
if meme is not None:
template_id = meme.template_id
text = [meme.text1, meme.text2, meme.text3, meme.text4]
bot.send_photo(chat_id, make_meme(template_id, text))
else:
bot.send_message(chat_id, "Meme not found.")
@bot.message_handler(commands=["rank"])
def receive_rank(message):
chat_id = message.chat.id
rank = []
msg = ""
records = session.query(TemplateTotalUse).all()
for record in records:
rank.append((record.template_id, record.use))
rank = sorted(rank, key=lambda record: record[1])
rank.reverse()
for i in range(len(rank)):
# msg format: 1. template [template_id] is used for [use] times
msg += " ".join(
[
str(i + 1) + ".",
"template",
str(rank[i][0]),
"is used for",
str(rank[i][1]),
"times",
]
)
msg += "\n"
bot.send_message(chat_id, msg)
# store meme callback, from receive_make_meme function
@bot.callback_query_handler(lambda call: "store" in call.data)
def receive_store_meme(call):
chat_id = call.message.chat.id
msg_id = call.message.message_id
meme = Memes(chat_id, *call.data.split(",")[1:])
session.add(meme)
session.commit()
session.refresh(meme)
kb = types.InlineKeyboardMarkup()
kb.row(types.InlineKeyboardButton("Success", callback_data="none"))
bot.edit_message_reply_markup(chat_id=chat_id, message_id=msg_id, reply_markup=kb)
bot.send_message(
chat_id,
"You can access this by typing `/publish %d`." % meme.ID,
parse_mode="Markdown",
)
bot.polling()