BP_2020_Matsunych/Bot.py

91 lines
2.4 KiB
Python
Raw Normal View History

2020-05-17 21:02:31 +00:00
from telegram import Bot
from telegram import Update
from telegram.ext import Updater
from telegram.ext import MessageHandler
from telegram.ext import Filters
from pickle import load
from keras.models import load_model
from keras.utils import to_categorical
from keras.preprocessing.sequence import pad_sequences
def generate_seq(model, mapping, seq_length, seed_text, n_chars):
in_text = seed_text
# generate a fixed number of characters
for _ in range(n_chars):
# encode the characters as integers
encoded = [mapping[char] for char in in_text]
# truncate sequences to a fixed length
encoded = pad_sequences([encoded], maxlen=seq_length, truncating='pre')
# one hot encode
encoded = to_categorical(encoded, num_classes=len(mapping))
# predict character
yhat = model.predict_classes(encoded, verbose=0)
# reverse map integer to character
out_char = ''
for char, index in mapping.items():
if index == yhat:
out_char = char
break
# append to input
if char == ' ':
char = '_'
in_text += char
return in_text
TG_TOKEN = "1011115574:AAHLaC4jgtkYGxL9wILnMjmTxsHLIqsGDZE"
BUFF = ''
def message_handler(bot: Bot, update: Update):
sim = 5
model = load_model('model.h5')
global BUFF
# load the mapping
mapping = load(open('mapping.pkl', 'rb'))
user = update.effective_user
bot.send_message(chat_id=update.effective_message.chat_id,
text="Введи начало никнейма")
text = update.effective_message.text
text_in = BUFF + text
nike = generate_seq(model, mapping, 10, text_in, sim)
nik = ''
iterator = (sim + len(text))*-1
while iterator != 0:
nik += nike[iterator]
iterator += 1
replay_text = f'{nik}'
bot.send_message(chat_id=update.effective_message.chat_id,
text=replay_text)
BUFF += nik
return
def main():
bot = Bot(
token=TG_TOKEN,
)
updater = Updater(
bot=bot,
)
hendler = MessageHandler(Filters.all, message_handler)
updater.dispatcher.add_handler(hendler)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()