1
0
Fork 0

save in eigener Klasse

This commit is contained in:
Lukas Winkler 2016-08-01 09:27:15 +02:00
parent f288946d31
commit 1d5371cabe
2 changed files with 62 additions and 23 deletions

49
bot.py
View file

@ -19,10 +19,12 @@ import logging
from pprint import pprint from pprint import pprint
import yaml import yaml
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler
from config import * from config import *
from wienerLinien import WienerLinien from wienerLinien import WienerLinien
from save import PersistentData
# Enable logging # Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
@ -31,25 +33,20 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
wl = WienerLinien("stationen/cache/current.json") wl = WienerLinien("stationen/cache/current.json")
save = PersistentData()
SELECT, TEST = range(2) SELECT, TEST = range(2)
with open("save.yaml") as json_file:
save = yaml.load(json_file)
pprint(save)
# Define a few command handlers. These usually take the two arguments bot and # Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error. # update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update): def start(bot, update):
bot.sendMessage(update.message.chat_id, text='Hi!') bot.sendMessage(update.message.chat_id, text='Hallo!')
if update.message.chat_id not in save: save.user(update.message.chat_id)
save[update.message.chat_id] = {}
if "stations" not in save[update.message.chat_id]:
save[update.message.chat_id]["stations"] = []
def help_message(bot, update): def help_message(bot, update):
bot.sendMessage(update.message.chat_id, text='Help!') bot.sendMessage(update.message.chat_id, text='Hilfetext')
def echo(bot, update): def echo(bot, update):
@ -71,20 +68,24 @@ def getstations(bot, update, args):
userinput = " ".join(args) userinput = " ".join(args)
print(userinput) print(userinput)
choice = wl.fuzzy_stationname(userinput) choice = wl.fuzzy_stationname(userinput)
save[update.message.chat_id]["choice"] = choice
save.save_choice(update.message.chat_id, choice)
pprint(choice) pprint(choice)
message = "Es wurden mehrere Stationen gefunden.\nBitte gib die Nummer der gewünschten Station an:\n" message = "Es wurden mehrere Stationen gefunden.\nBitte gib die Nummer der gewünschten Station an:\n"
prev_percentage = choice[0][1] prev_percentage = choice[0][1]
i = 1 i = 1
keyboard = []
for name, percentage, stationId in choice: for name, percentage, stationId in choice:
if prev_percentage - percentage >= 10 or percentage <= 50: if prev_percentage - percentage >= 10 or percentage <= 50:
break break
message += str(i) + ": " + name + "\n" message += str(i) + ": " + name + "\n"
keyboard.append([InlineKeyboardButton(name, callback_data=str(i))])
i += 1 i += 1
prev_percentage = percentage prev_percentage = percentage
print(message) print(message)
if message and i > 2: if message and i > 2:
bot.sendMessage(update.message.chat_id, text=message) reply_markup = InlineKeyboardMarkup(keyboard)
bot.sendMessage(update.message.chat_id, text=message, reply_markup=reply_markup)
elif i == 2: elif i == 2:
bot.sendMessage(update.message.chat_id, text=message) bot.sendMessage(update.message.chat_id, text=message)
return ConversationHandler.END return ConversationHandler.END
@ -95,13 +96,14 @@ def getstations(bot, update, args):
def select(bot, update): def select(bot, update):
if update.message.text.isdigit(): query = update.callback_query
selected_station = save[update.message.chat_id]["choice"][int(update.message.text) - 1] if query.data.isdigit():
selected_station = save.get_choice(query.message.chat_id)[int(query.data) - 1]
pprint(selected_station) pprint(selected_station)
save[update.message.chat_id]["stations"].append({"name": selected_station[0], "id": selected_station[2]}) save.add_station(query.message.chat_id, {"name": selected_station[0], "id": selected_station[2]})
bot.sendMessage(update.message.chat_id, bot.editMessageText(text="Station '{station}' hinzugefügt".format(station=selected_station[0]),
text="Station '{station}' hinzugefügt".format(station=selected_station[0])) chat_id=query.message.chat_id,
del save[update.message.chat_id]["choice"] message_id=query.message.message_id)
return ConversationHandler.END return ConversationHandler.END
else: else:
@ -111,14 +113,15 @@ def select(bot, update):
def cancel(bot, update): def cancel(bot, update):
bot.sendMessage(update.message.chat_id, text="Aktion abgebrochen") bot.sendMessage(update.message.chat_id, text="Aktion abgebrochen")
del save[update.message.chat_id]["choice"] save.delete_choice(update.message.chat_id)
return ConversationHandler.END return ConversationHandler.END
def list_stations(bot, update): def list_stations(bot, update):
if save[update.message.chat_id]["stations"]: stations = save.get_stations(update.message.chat_id)
if stations:
message = "" message = ""
for station in save[update.message.chat_id]["stations"]: for station in stations:
message += station["name"] + "\n" message += station["name"] + "\n"
bot.sendMessage(update.message.chat_id, text=message) bot.sendMessage(update.message.chat_id, text=message)
else: else:
@ -139,6 +142,7 @@ def main():
dp.add_handler(CommandHandler("help", help_message)) dp.add_handler(CommandHandler("help", help_message))
# dp.add_handler(CommandHandler("station", getstations)) # dp.add_handler(CommandHandler("station", getstations))
dp.add_handler(CommandHandler("list", list_stations)) dp.add_handler(CommandHandler("list", list_stations))
updater.dispatcher.add_handler(CallbackQueryHandler(select))
conv_handler = ConversationHandler( conv_handler = ConversationHandler(
entry_points=[CommandHandler('add', getstations, pass_args=True)], entry_points=[CommandHandler('add', getstations, pass_args=True)],
@ -174,8 +178,7 @@ def main():
# start_polling() is non-blocking and will stop the bot gracefully. # start_polling() is non-blocking and will stop the bot gracefully.
updater.idle() updater.idle()
print("------------------------------------------------------------------------") # print("------------------------------------------------------------------------") #
with open('save.yaml', 'w') as outfile: save.export()
outfile.write(yaml.dump(save, default_flow_style=False))
if __name__ == '__main__': if __name__ == '__main__':

36
save.py Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python3
import yaml
from pprint import pprint
class PersistentData:
def __init__(self):
with open("save.yaml") as json_file:
self.save = yaml.load(json_file)
def export(self):
with open('save.yaml', 'w') as outfile:
outfile.write(yaml.dump(self.save, default_flow_style=False))
def user(self, chat_id):
if chat_id not in self.save:
self.save[chat_id] = {}
if "stations" not in self.save[chat_id] or self.save[chat_id]["stations"] is None:
self.save[chat_id]["stations"] = []
def save_choice(self, chat_id, choice):
self.save[chat_id]["choice"] = choice
def get_choice(self, chat_id):
return self.save[chat_id]["choice"]
def add_station(self, chat_id, station):
pprint(self.save[chat_id])
self.save[chat_id]["stations"].append(station)
self.delete_choice(chat_id)
def delete_choice(self, chat_id):
del self.save[chat_id]["choice"]
def get_stations(self, chat_id):
return self.save[chat_id]["stations"]