Repository navigation
Expand file tree
/
Copy pathbotFile.py
More file actions
152 lines (109 loc) · 5.19 KB
/
Copy pathbotFile.py
File metadata and controls
152 lines (109 loc) · 5.19 KB
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
151
152
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
ConversationHandler)
import logging
from googleCalendar import selecionarUmEvento, criaEvento, alterarEvento, deletarEvento
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
MAPEAR_ACAO, CRIAR, CONSULTAR, ALTERAR, DELETAR, CANCELAR = range(6)
def start(bot, update):
reply_keyboard = [['Criar', 'Consultar','Alterar', 'Deletar']]
update.message.reply_text(
'Olá! Eu sou o Schedule Bot.Vim ajudar-te a controlar sua agenda.\n\n'
'O que deseja fazer',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
return MAPEAR_ACAO
def acao(bot, update):
# user = update.message.from_user
# logger.info("Gender of %s: %s", user.first_name, update.message.text)
# update.message.reply_text('I see! Please send me a photo of yourself, '
# 'so I know what you look like, or send /skip if you don\'t want to.',
# reply_markup=ReplyKeyboardRemove())
acao = update.message.text
if(acao == 'Criar'):
update.message.reply_text('Digite as informações do evento que deseja criar no formato:\n'
'nome, ano , mês , dia, horaInicio, minutosInicio, horaFinal, MinutoFinal' ,
reply_markup=ReplyKeyboardRemove())
return CRIAR
elif (acao == 'Consultar'):
update.message.reply_text('Digite o nome do evento que deseja consultar',
reply_markup=ReplyKeyboardRemove())
return CONSULTAR
elif (acao == 'Alterar'):
update.message.reply_text('Digite as informações do evento que deseja alterar',
reply_markup=ReplyKeyboardRemove())
return ALTERAR
elif (acao == 'Deletar'):
update.message.reply_text('Digite o nome do evento que deseja deletar',
reply_markup=ReplyKeyboardRemove())
return DELETAR
return CANCELAR
def criar(bot, update):
user = update.message.from_user
logger.info("Criar %s: %s", user.first_name, update.message.text)
evento = update.message.text
evento = evento.split(',')
link = criaEvento(evento[0],int(evento[1]),int(evento[2]),int(evento[3]),int(evento[4]),int(evento[5]),int(evento[6]),int(evento[7]))
update.message.reply_text(link)
return ConversationHandler.END
def consultar(bot, update):
user = update.message.from_user
logger.info("Consultar %s: %s", user.first_name, update.message.text)
event = selecionarUmEvento(update.message.text)
update.message.reply_text(event)
return ConversationHandler.END
def alterar(bot, update):
user = update.message.from_user
logger.info("Criar %s: %s", user.first_name, update.message.text)
update.message.reply_text('O usuário deseja alterar um evento.')
return ConversationHandler.END
def deletar(bot, update):
user = update.message.from_user
logger.info("Criar %s: %s", user.first_name, update.message.text)
deletarEvento(update.message.text)
update.message.reply_text('Evento excluído')
return ConversationHandler.END
def cancel(bot, update):
user = update.message.from_user
logger.info("User %s canceled the conversation.", user.first_name)
update.message.reply_text('Tchau! Espero ter ajudado.',
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater("")
# Get the dispatcher to register handlers
dp = updater.dispatcher
# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
MAPEAR_ACAO: [RegexHandler('^(Criar|Consultar|Alterar|Deletar)$', acao)],
CRIAR: [MessageHandler(Filters.text, criar),
CommandHandler('cancel', cancel)],
CONSULTAR: [MessageHandler(Filters.text, consultar),
CommandHandler('cancel', cancel)],
ALTERAR: [MessageHandler(Filters.text, alterar),
CommandHandler('cancel', cancel)],
DELETAR: [MessageHandler(Filters.text, deletar),
CommandHandler('cancel', cancel)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
dp.add_handler(conv_handler)
# log all errors
dp.add_error_handler(error)
print("Bot rodando")
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()