-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
150 lines (116 loc) · 5.2 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
import logging
import requests
import os
import messages as responses
from github import Github
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
### Configurar Login
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
### Variables para el Telegram BOT
PORT = int(os.environ.get('PORT', 8443))
MODE = os.environ.get('MODE')
# Bifurcacion para produccion y desarrollo
if MODE == 'prod':
API_TOKEN = os.environ.get('TOKEN')
APP_NAME = os.environ.get('NAME')
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')
def run(updater):
updater.start_webhook(listen="0.0.0.0",
port= PORT,
url_path= API_TOKEN,
webhook_url= f"https://{APP_NAME}.herokuapp.com/{API_TOKEN}")
return updater
else:
API_TOKEN = '' #Copiar aca el token
dusty_token = '///gh//p_w//zlsG//PbF//5X2nA//mmy//ySzhM//TEmF//137//QY2v//tKNN/'
GITHUB_TOKEN = dusty_token.replace('/','')
def run(updater):
updater.start_polling()
return updater
### Variables para GitHub API
g = Github(GITHUB_TOKEN)
REPO = g.get_repo('gnuno/algoritmia')
### Comandos del BOT
def start(update, context):
"""Mensaje de bienvenida al iniciar"""
logger.info(f"USER {update.message.from_user.id} /start")
user = update.effective_user
update.message.reply_markdown_v2(f'Hola *{user.mention_markdown_v2()}*\!\nPara ver los comandos disponebles escribí */help*')
def help_command(update, context):
"""Mensaje de ayuda al usar /help"""
logger.info(f"USER {update.message.from_user.id} /help")
update.message.reply_markdown_v2(responses.help_message())
def getChallenge(id_challenge):
"""Recibe un ID y busca en el repo tal challenge o el ultimo si no existe"""
contents = REPO.get_contents("")
for content_file in contents:
splitted_path = content_file.path.split("-")
id = splitted_path[0] if len(splitted_path) > 1 else None
if id != None and int(id) == int(id_challenge):
return content_file
if challenge == None:
return REPO.get_contents("")[-3]
def actualChallenge(update, context):
"""Busca el reto actual o por id de challenge"""
splitted = update.message.text.split()
id_challenge = splitted[1] if len(splitted) > 1 else None
challenge = None
if id_challenge:
logger.info(f"USER {update.message.from_user.id} /challenge {id_challenge}")
challenge = getChallenge(id_challenge)
else:
logger.info(f"USER {update.message.from_user.id} /challenge")
# Accedemos a la ultima carpeta (menos README y menos LICENSE = ultima carpeta)
challenge = REPO.get_contents("")[-3]
# Accedemos al readme
readme_url = REPO.get_contents(f"{challenge.path}/README.md").download_url
readme = requests.get(readme_url).text
# Replace para parsear markdown normal al de telegram
readme = readme.replace('**','*').replace('#',' ')
update.message.reply_markdown_v2(responses.normalize_markdown(readme))
def allChallenges(update, context):
"""Lista todos los challenges disponibles para luego acceder por id"""
logger.info(f"USER {update.message.from_user.id} /all")
challenges = REPO.get_contents("")
keyboard = []
for challenge in challenges:
splitted = challenge.path.split("-")
id = splitted[0] if len(splitted) > 1 else None
if id != None:
keyboard.append([InlineKeyboardButton(f"{id} - {splitted[1]}", callback_data = id.__str__())])
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(chat_id= update.message.chat.id, reply_markup= reply_markup, text= 'Todos los desafios disponibles:')
def selectedChallenge(update, context):
"""Parses the CallbackQuery and updates the message text."""
query = update.callback_query
query.answer()
challenge = getChallenge(query.data)
readme_url = REPO.get_contents(f"{challenge.path}/README.md").download_url
readme = requests.get(readme_url).text
# Replace para parsear markdown normal al de telegram
readme = readme.replace('**','*').replace('#',' ')
update.callback_query.message.reply_markdown_v2(responses.normalize_markdown(readme))
#query.edit_message_text(text= responses.normalize_markdown(readme))
def error(update, context):
"""Loggea los errores causados por el Updater"""
logger.warning(f'Update {update} caused error {context.error}')
### Inicio del bot y handlers
def main():
"""Inicia el bot."""
updater = Updater(API_TOKEN)
dispatcher = updater.dispatcher
# Handler para cada comando
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))
dispatcher.add_handler(CommandHandler("challenge", actualChallenge))
dispatcher.add_handler(CommandHandler("list", allChallenges))
dispatcher.add_handler(CallbackQueryHandler(selectedChallenge))
# Handler para errores
dispatcher.add_error_handler(error)
# Iniciar bot
updater = run(updater)
updater.idle()
if __name__ == '__main__':
main()