-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
102 lines (74 loc) · 2.87 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
#!/usr/bin/env python3
"""Bot do Grupy-SP."""
from __future__ import print_function
import os
import sys
import logging
import logging.config
from locale import setlocale, LC_ALL
from dotenv import load_dotenv
from telegram.ext import Updater, CommandHandler
from events import get_events
# check python version
if sys.version_info.major != 3 or sys.version_info.minor < 6:
print('Esse programa so funciona com python 3.6 ou mais recente')
sys.exit()
# config stuff
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger(__name__)
load_dotenv()
setlocale(LC_ALL, 'pt_BR.UTF-8')
def start(update, context):
"""Echo some info"""
user_username = update.message.from_user['username']
user_id = update.message.from_user['id']
user_name = update.message.from_user['first_name']
logger.info(f'Usuário: {user_id} {user_username} - /start')
update.message.reply_text(f'Olá, {user_name}!')
def eventos(update, context, q=5):
"""Get the events on meetup website and send on chat"""
user_username = update.message.from_user['username']
user_id = update.message.from_user['id']
if q == 1:
logger.info(f'Usuário: {user_id} {user_username} - /evento')
else:
logger.info(f'Usuário: {user_id} {user_username} - /eventos')
events = get_events()
qtd = len(events)
if qtd == 0:
update.message.reply_text('Não há nenhum evento registrado no Meetup!')
else:
update.message.reply_text('Próximos eventos:\n')
for i in range(qtd):
event = events[i]
msg = ((
f"[{event['title']}]({event['url']})",
event['date'].strftime('Dia %d de %B de %Y, às %H:%M'),
'\n',
))
update.message.reply_text(
'\n'.join(msg),
disable_web_page_preview=False,
parse_mode='Markdown',
)
"""
This snipet would be used to send the location
However I couldn't get the location on the events listing page
One ideia is to get the individual event page
update.message.reply_venue(
latitude=lat, longitude=lon, title=location, address=address
)
"""
update.message.reply_text('#evento #Meetup')
def evento(bot, update):
eventos(bot, update, 1)
if __name__ == '__main__':
# instantiate the telegram bot inner works
updater = Updater(os.environ['TELEGRAM_TOKEN'], use_context=True)
# add functions that can be called from telegram chat
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('eventos', eventos))
updater.dispatcher.add_handler(CommandHandler('evento', evento))
# start loop that listen to chat
updater.start_polling()
updater.idle()