Created
March 27, 2018 15:30
-
-
Save intgr/b51305e733619baacd32344eb3f6b53e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Vegebot, courtesy of Classy Kirjand""" | |
import config | |
from telegram import ReplyKeyboardMarkup, InlineKeyboardButton, InlineKeyboardMarkup | |
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler, | |
ConversationHandler) | |
import logging | |
# Enable logging | |
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
START, QUESTION, NOTVEGE = range(3) | |
clear = '\n\u00a0' * 5 | |
yesno_markup = ReplyKeyboardMarkup([["🍃 Jah", "🐔 Ei 🍗"]], one_time_keyboard=True) | |
start_markup = ReplyKeyboardMarkup([["💩 PROOVI UUESTI"]], one_time_keyboard=True) | |
def log(message): | |
logger.info(' '.join((message.from_user.username, message.from_user.first_name, message.from_user.last_name, message.text))) | |
def start(bot, update, user_data=None): | |
log(update.message) | |
update.message.reply_text('🍃🍃 Oled vege vä? 🍀🍀', reply_markup=yesno_markup) | |
return QUESTION | |
def question_choice(bot, update, user_data): | |
log(update.message) | |
text = update.message.text.lower() | |
if 'jah' in text: | |
update.message.reply_text('🤫 Ole vait ja bloki see bot!' + clear, reply_markup=start_markup) | |
return ConversationHandler.END | |
elif 'ei' in text: | |
update.message.reply_text('☘️ Mõni tuttav on vege vä? ☘️', reply_markup=yesno_markup) | |
return NOTVEGE | |
else: | |
update.message.reply_text('💩 Ei saanud aru, proovi uuesti', reply_markup=yesno_markup) | |
return QUESTION | |
def notvege_choice(bot, update, user_data): | |
log(update.message) | |
text = update.message.text.lower() | |
if 'jah' in text: | |
update.message.reply_text('🤪 Ei usu, et sul sõpru on.\nAga igaks juhuks käse tal vait olla!' + clear, reply_markup=start_markup) | |
return ConversationHandler.END | |
elif 'ei' in text: | |
update.message.reply_text('Tee mis tahad – alusta otsast või bloki see bot.' + clear, reply_markup=start_markup) | |
return ConversationHandler.END | |
else: | |
update.message.reply_text('💩Ei saanud aru, proovi uuesti', reply_markup=yesno_markup) | |
return NOTVEGE | |
def error(bot, update, error): | |
"""Log Errors caused by Updates.""" | |
logger.warning('Update "%s" caused error "%s"', update, error) | |
def main(): | |
# Create the Updater and pass it your bot's token. | |
updater = Updater(config.token) | |
# 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=[RegexHandler('.*', start)], | |
states={ | |
QUESTION: [MessageHandler(None, question_choice, pass_user_data=True) ], | |
NOTVEGE: [MessageHandler(None, notvege_choice, pass_user_data=True) ], | |
}, | |
fallbacks=[RegexHandler('', start, pass_user_data=True)] | |
) | |
dp.add_handler(conv_handler) | |
# log all errors | |
dp.add_error_handler(error) | |
# 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. | |
logger.info("STARTED!") | |
updater.idle() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment