-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
111 lines (89 loc) · 4.58 KB
/
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
from telegram.ext import Updater
import logging
from telegram import Update, ParseMode
from telegram.ext import CallbackContext
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
from game import Game
from word import Word
from secretRepository import SecretRepository
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
secrets = SecretRepository()
updater = Updater(token=secrets.token, use_context=True)
dispatcher = updater.dispatcher
game = Game()
def start(update: Update, context: CallbackContext):
uuid = update.effective_chat.id
input = update.message.text[7:]
username = str(update.message.chat.username)
fullname = str(update.message.chat.first_name) + " " + str(update.message.chat.last_name) + " (" + username + ")"
if input.isnumeric():
try:
word = game.getWordById(input)
game.sessions.newSession(uuid, word)
logging.info("LOAD " + str(uuid) + " word: " + word.word + " id: " + str(word.id))
text = "¡La palabra de tu amig@ se ha cargado, suerte!\n\n⬛⬛⬛⬛⬛"
except:
text = "Identificador inválido o no encontrado."
else:
text = "¡Bienvenido al Chupiwordle!\n\n⬛⬛⬛⬛⬛"
context.bot.send_message(chat_id=update.effective_chat.id, text=text)
def tryWord(update: Update, context: CallbackContext):
uuid = update.effective_chat.id
input = update.message.text.upper()
username = str(update.message.chat.username)
fullname = str(update.message.chat.first_name) + " " + str(update.message.chat.last_name) + " (" + username + ")"
if not game.sessions.hasActiveSession(uuid):
word = game.getRandomWord()
session = game.sessions.newSession(uuid, word)
logging.info("NEW " + str(uuid) + " " + fullname + " " + word.word + " id: " + str(word.id))
try:
session = game.sessions.getSession(uuid)
except e:
logging.error("ERROR LOADING USER " + str(uuid) + " " +e)
context.bot.send_message(chat_id=uuid, text="There was an error " + e)
if len(input) != 5:
context.bot.send_message(chat_id=uuid, text="Palabras de cinco (5) letras.")
return
session.addTry()
if game.checkValidWord(input) is False:
context.bot.send_message(chat_id=uuid, text="❌ Esta palabra no es válida. \nLlevas " + str(session.getInvalidTries()) + " inválidas.")
return
word = session.getWord().word
wordId = session.getWord().id
won, out = game.checkWord(word, input)
lost = ((session.getTries() + 1) == game.getMaxTries() and won is False)
session.register(input, out, won, lost)
logging.info("TRY " + str(uuid) + " " + username + " " + word + " " + input + " " + out)
if won or lost:
if lost:
context.bot.send_message(chat_id=uuid, text="La palabra era: " + word)
out_text = "¡Ganaste!" if won else "¡Perdiste!"
out_text += " " + str(session.getTries()) + "/" + str(game.maxTries)
out_text += "\n" + "\n".join(session.getOutputs())
out_text += "\n❌: " + str(session.getInvalidTries()) + " inválida" + ("s" if session.getInvalidTries() != 1 else "")
out_text += "\n⌛: " + str(session.getTime())
out_text += "\n\n🕹️🎮: @ChupiLeBot"
out_text += "\nJuega la misma palabra con <a href='http://telegram.me/chupiLeBot?start=" + str(wordId) + "'>/start " + str(wordId) + "</a>"
context.bot.send_message(chat_id=uuid, text=out_text, parse_mode=ParseMode.HTML)
context.bot.send_message(chat_id=uuid, text="Se ha cargado una nueva palabra, ¡puedes seguir jugando!")
else:
out_text = "<code>" + session.getVerbose() + "</code>"
out_text += "\nIntento " + str(session.getTries()) + " de " + str(game.maxTries)
context.bot.send_message(chat_id=uuid, text=out_text, parse_mode=ParseMode.HTML)
def add(update: Update, context: CallbackContext):
uuid = update.effective_chat.id
words = update.message.text[5:].split(',')
writtenWords = game.writeWords(words)
out = "Palabras añadidas:\n"
for word in writtenWords:
out += word.word + " ID: " + str(word.id) + "\n"
context.bot.send_message(chat_id=update.effective_chat.id, text=out, parse_mode=ParseMode.HTML)
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
add_handler = CommandHandler('add', add)
dispatcher.add_handler(add_handler)
try_handler = MessageHandler(Filters.text & (~Filters.command), tryWord)
dispatcher.add_handler(try_handler)
updater.start_polling()