-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkalik_bot.py
123 lines (93 loc) · 3.04 KB
/
kalik_bot.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
import logging
import os.path
import pickle
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, ApplicationBuilder, CallbackQueryHandler, CommandHandler, ContextTypes
import settings
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
feedback = {}
TEXT_MODEL = None
def read_feedback():
global feedback
if os.path.exists("feedback"):
with open("feedback", "rb") as f:
feedback = pickle.load(f)
else:
feedback = {}
def read_model():
global TEXT_MODEL
with open("model.data", "rb") as f:
TEXT_MODEL = pickle.load(f)
async def bot_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.message.chat.id
await context.bot.send_message(
chat_id,
text="""
<b>Kalik Bot Commands:</b>
/help - show this help
/kalik - send Kalik-based message
""",
parse_mode="HTML",
)
def make_sentence():
sentence = None
while sentence is None:
sentence = TEXT_MODEL.make_sentence()
return sentence
async def send_kalik(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [
[
InlineKeyboardButton("I like it!😊", callback_data="like"),
InlineKeyboardButton("It's crap😒", callback_data="dislike"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
chat_id = update.message.chat.id
kalik_message = make_sentence()
logging.info(kalik_message)
await context.bot.send_message(
chat_id,
kalik_message,
reply_markup=reply_markup,
parse_mode="HTML",
)
except Exception as e:
logging.error(e)
async def vote(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
chat_id = query.message.chat.id
msg_id = query.message.message_id
user_id = query.from_user.id
messages = feedback.get(chat_id, {})
values = messages.get(msg_id, {})
if query.data == "like":
values[user_id] = 1
else:
values[user_id] = -1
messages[msg_id] = values
values["text"] = query.message.text
feedback[chat_id] = messages
await query.answer()
logging.info(feedback)
async def post_stop(application: Application) -> None:
with open("feedback", "wb") as f:
pickle.dump(feedback, f)
logging.info("Shutting down...")
def main():
read_feedback()
read_model()
application = ApplicationBuilder().token(settings.AUTH_TOKEN).post_stop(post_stop).build()
start_handler = CommandHandler("start", bot_help)
application.add_handler(start_handler)
help_handler = CommandHandler("help", bot_help)
application.add_handler(help_handler)
kalik_handler = CommandHandler("kalik", send_kalik)
application.add_handler(kalik_handler)
application.add_handler(CallbackQueryHandler(vote))
application.run_polling()
if __name__ == "__main__":
main()