Skip to content

Commit 69e16e9

Browse files
authored
Merge pull request #43 from PyAr/telegram-bot-upgrade
Updated telegram bot and refactored all the project.
2 parents a3205b8 + a634ebf commit 69e16e9

File tree

11 files changed

+182
-184
lines changed

11 files changed

+182
-184
lines changed

bin/run_bot.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import os
3-
from telegram.ext import (Updater)
3+
from telegram.ext import Application
44
from pycamp_bot.commands import auth
55
from pycamp_bot.commands import voting
66
from pycamp_bot.commands import manage_pycamp
@@ -19,28 +19,28 @@
1919
logger = logging.getLogger(__name__)
2020

2121

22-
def set_handlers(updater):
23-
base.set_handlers(updater)
24-
auth.set_handlers(updater)
25-
wizard.set_handlers(updater)
26-
voting.set_handlers(updater)
27-
manage_pycamp.set_handlers(updater)
28-
projects.set_handlers(updater)
29-
raffle.set_handlers(updater)
30-
schedule.set_handlers(updater)
31-
announcements.set_handlers(updater)
22+
def set_handlers(application):
23+
base.set_handlers(application)
24+
auth.set_handlers(application)
25+
wizard.set_handlers(application)
26+
voting.set_handlers(application)
27+
manage_pycamp.set_handlers(application)
28+
projects.set_handlers(application)
29+
raffle.set_handlers(application)
30+
schedule.set_handlers(application)
31+
announcements.set_handlers(application)
3232

3333

3434
if __name__ == '__main__':
3535
logger.info('Starting PyCamp Bot')
3636

3737
if 'TOKEN' in os.environ.keys():
38-
updater = Updater(token=os.environ['TOKEN'])
39-
4038
models_db_connection()
41-
set_handlers(updater)
4239

43-
updater.start_polling()
40+
application = Application.builder().token(os.environ['TOKEN']).build()
41+
# application.add_handler(CommandHandler("start", start))
42+
set_handlers(application)
43+
application.run_polling()
4444

4545
else:
4646
logger.info('Token not defined. Exiting.')

src/pycamp_bot/commands/announcements.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
logger = logging.getLogger(__name__)
88

99

10-
def announce(bot, update):
10+
async def announce(update, context):
1111
username = update.message.from_user.username
1212
admins = get_admins_username()
1313
project_name = update.message.text.split()[1:]
@@ -16,7 +16,7 @@ def announce(bot, update):
1616
project = Project.select().where(Project.name == project_name)
1717

1818
if len(project) <= 0:
19-
bot.send_message(
19+
await context.bot.send_message(
2020
chat_id=update.message.chat_id,
2121
text=f"El proyecto '{project_name}' no existe o esta mal escroto.\n"
2222
"El formato de este comando es:\n"
@@ -25,7 +25,7 @@ def announce(bot, update):
2525
return
2626

2727
if not (project.get().owner.username == username or username in admins):
28-
bot.send_message(
28+
await context.bot.send_message(
2929
chat_id=update.message.chat_id,
3030
text="No sos ni admin ni el owner de este proyecto, Careta."
3131
)
@@ -36,15 +36,15 @@ def announce(bot, update):
3636
chat_id_list = [user.pycampista.chat_id for user in pycampistas]
3737

3838
for chat_id in chat_id_list:
39-
bot.send_message(
39+
await context.bot.send_message(
4040
chat_id=chat_id,
4141
text=f"Esta por empezar {project_name} a cargo de @{project.get().owner.username}."
4242
)
43-
bot.send_message(
43+
await context.bot.send_message(
4444
chat_id=update.message.chat_id,
4545
text="Anunciado!"
4646
)
4747

4848

49-
def set_handlers(updater):
50-
updater.dispatcher.add_handler(CommandHandler('anunciar', announce))
49+
def set_handlers(application):
50+
application.add_handler(CommandHandler('anunciar', announce))

src/pycamp_bot/commands/auth.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_admins_username():
1616
return admins
1717

1818

19-
def is_admin(bot, update):
19+
def is_admin(update, context):
2020
"""Checks if the user is authorized as admin"""
2121
username = update.message.from_user.username
2222
authorized = get_admins_username()
@@ -30,27 +30,27 @@ def is_admin(bot, update):
3030

3131

3232
def admin_needed(f):
33-
def wrap(*args, **kargs):
33+
async def wrap(*args, **kargs):
3434
logger.info('Admin nedeed wrapper')
35-
bot, update = args
35+
update, context = args
3636
if is_admin(*args):
37-
return f(*args)
37+
return await f(*args)
3838
else:
39-
bot.send_message(
39+
await context.bot.send_message(
4040
chat_id=update.message.chat_id,
4141
text="No estas Autorizadx para hacer esta acción"
4242
)
4343
return wrap
4444

4545

46-
def grant_admin(bot, update):
46+
async def grant_admin(update, context):
4747
username = update.message.from_user.username
4848
chat_id = update.message.chat_id
4949
text = update.message.text
5050

5151
parameters = text.split(' ')
5252
if not len(parameters) == 2:
53-
bot.send_message(chat_id=chat_id,
53+
await context.bot.send_message(chat_id=chat_id,
5454
text='Parametros incorrectos.')
5555
return
5656

@@ -69,17 +69,17 @@ def grant_admin(bot, update):
6969
logger.error('PYCAMP_BOT_MASTER_KEY env not set.')
7070
rply_msg = 'Hay un problema en el servidor, avisale a un admin.'
7171

72-
bot.send_message(chat_id=chat_id, text=rply_msg)
72+
await context.bot.send_message(chat_id=chat_id, text=rply_msg)
7373

7474

7575
@admin_needed
76-
def revoke_admin(bot, update):
76+
async def revoke_admin(update, context):
7777
chat_id = update.message.chat_id
7878
text = update.message.text
7979

8080
parameters = text.split(' ')
8181
if not len(parameters) == 2:
82-
bot.send_message(chat_id=chat_id,
82+
await context.bot.send_message(chat_id=chat_id,
8383
text='Parametros incorrectos.')
8484
return
8585

@@ -88,11 +88,11 @@ def revoke_admin(bot, update):
8888
user = Pycampista.select().where(Pycampista.username == fallen_admin)[0]
8989
user.admin = False
9090
user.save()
91-
bot.send_message(chat_id=chat_id,
91+
await context.bot.send_message(chat_id=chat_id,
9292
text='Un admin a caido --{}--.'.format(fallen_admin))
9393

9494

95-
def list_admins(bot, update):
95+
async def list_admins(update, context):
9696
chat_id = update.message.chat_id
9797

9898
admins = get_admins_username()
@@ -102,10 +102,10 @@ def list_admins(bot, update):
102102
rply_msg += admin
103103
rply_msg += '\n'
104104

105-
bot.send_message(chat_id=chat_id, text=rply_msg)
105+
await context.bot.send_message(chat_id=chat_id, text=rply_msg)
106106

107107

108-
def set_handlers(updater):
109-
updater.dispatcher.add_handler(CommandHandler('su', grant_admin))
110-
updater.dispatcher.add_handler(CommandHandler('degradar', revoke_admin))
111-
updater.dispatcher.add_handler(CommandHandler('admins', list_admins))
108+
def set_handlers(application):
109+
application.add_handler(CommandHandler('su', grant_admin))
110+
application.add_handler(CommandHandler('degradar', revoke_admin))
111+
application.add_handler(CommandHandler('admins', list_admins))

src/pycamp_bot/commands/base.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@
66
logger = logging.getLogger(__name__)
77

88

9-
def msg_to_active_pycamp_chat(bot, text):
9+
async def msg_to_active_pycamp_chat(bot, text):
1010
chat_id = -220368252 # Prueba
11-
bot.send_message(
11+
await bot.send_message(
1212
chat_id=chat_id,
1313
text=text
1414
)
1515

1616

17-
def start(bot, update):
17+
async def start(update, context):
1818
logger.info('Start command')
1919
chat_id = update.message.chat_id
2020

2121
if update.message.from_user.username is None:
22-
bot.send_message(
22+
await context.bot.send_message(
2323
chat_id=chat_id,
2424
text="""Hola! Necesitas tener un username primero.
2525
\nCreate uno siguiendo esta guia: https://ewtnet.com/technology/how-to/how-to-add-a-username-on-telegram-android-app.
2626
Y despues dame /start the nuevo :) """)
2727

2828
elif update.message.from_user.username:
29-
bot.send_message(
29+
await context.bot.send_message(
3030
chat_id=chat_id,
3131
text='Hola ' + update.message.from_user.username +
3232
'! Bienvenidx'
3333
)
3434

3535

36-
def help(bot, update):
36+
async def help(update, context):
3737
logger.info('Returning help message')
38-
bot.send_message(chat_id=update.message.chat_id, text=get_help(bot, update))
38+
await context.bot.send_message(chat_id=update.message.chat_id, text=get_help(update, context))
3939

4040

41-
def error(bot, update, error):
42-
'''Log Errors caused by Updates.'''
43-
logger.warning('Update {} caused error {}'.format(update, error))
41+
# async def error(update, context):
42+
# '''Log Errors caused by Updates.'''
43+
# logger.warning('Update {} caused error {}'.format(update, context.error))
4444

4545

46-
def set_handlers(updater):
47-
updater.dispatcher.add_error_handler(error)
46+
def set_handlers(application):
47+
# application.add_error_handler(error)
4848

49-
updater.dispatcher.add_handler(CommandHandler('start', start))
50-
updater.dispatcher.add_handler(CommandHandler('ayuda', help))
49+
application.add_handler(CommandHandler('start', start))
50+
application.add_handler(CommandHandler('ayuda', help))

src/pycamp_bot/commands/help_msg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
''' + user_commands_help
7272

7373

74-
def get_help(bot, update):
75-
if is_admin(bot, update):
74+
def get_help(update, context):
75+
if is_admin(update, context):
7676
return HELP_MESSAGE_ADMIN
7777
return HELP_MESSAGE

0 commit comments

Comments
 (0)