This commit is contained in:
cupcakearmy 2019-12-23 01:56:49 +01:00
parent 2ffff5ab0f
commit 29aa9cc366
7 changed files with 46 additions and 44 deletions

View File

@ -1,21 +1,17 @@
from enum import Enum
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove, ParseMode from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove, ParseMode
from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext
MENU, API_KEY, ENABLED = range(3) from constants import Section
MENU, API_KEY, ENABLED, DELETE = range(4)
class Section(Enum): def init(update: Update, context: CallbackContext):
Watchlist = 'watchlist' # The list of Stocks/ETF to watch context.bot.delete_message(
Code = 'code' # Market code for a given stock, etf, etc. chat_id=update.message.chat_id,
API_Key = 'api_key' # Alpha Vantage API Key message_id=update.message.message_id,
Running = 'running' # Currently sending updates. Avoid overloading the API )
Enabled = 'enabled' # Whether the bot should send automatic updates return show_menu(update, context)
Interval = 'interval' # Time axis of the graph
Frequency = 'frequency' # How ofter updates should be sent
LastRun = 'last_run' # Last time an update was sent to the user
CurrentToEdit = 'current_to_edit' # Current element to edit in the conversation handler
def show_menu(update: Update, context: CallbackContext): def show_menu(update: Update, context: CallbackContext):
@ -24,6 +20,7 @@ def show_menu(update: Update, context: CallbackContext):
[InlineKeyboardButton( [InlineKeyboardButton(
f'Turn {"off" if context.user_data.setdefault(Section.Enabled.value, True) else "on"} global auto updates', f'Turn {"off" if context.user_data.setdefault(Section.Enabled.value, True) else "on"} global auto updates',
callback_data=ENABLED)], callback_data=ENABLED)],
[InlineKeyboardButton('Delete all data', callback_data=DELETE)],
[InlineKeyboardButton('Done', callback_data=ConversationHandler.END)], [InlineKeyboardButton('Done', callback_data=ConversationHandler.END)],
] ]
update.effective_user.send_message( update.effective_user.send_message(
@ -38,23 +35,6 @@ def show_menu(update: Update, context: CallbackContext):
return MENU return MENU
def show_menu_api_key(update: Update, context: CallbackContext):
update.effective_user.send_message(
'Send me your API Key 🙂'
'\nor /cancel',
reply_markup=ReplyKeyboardRemove()
)
return API_KEY
def init(update: Update, context: CallbackContext):
context.bot.delete_message(
chat_id=update.message.chat_id,
message_id=update.message.message_id,
)
return show_menu(update, context)
def menu(update: Update, context: CallbackContext): def menu(update: Update, context: CallbackContext):
selected = int(update.callback_query.data) selected = int(update.callback_query.data)
@ -64,14 +44,23 @@ def menu(update: Update, context: CallbackContext):
) )
if selected == API_KEY: if selected == API_KEY:
return show_menu_api_key(update, context) return show_api_key(update, context)
elif selected == ENABLED: elif selected == ENABLED:
toggle_enabled(update, context) toggle_enabled(update, context)
else: else:
return ConversationHandler.END return ConversationHandler.END
def set_api_key(update, context): def show_api_key(update: Update, context: CallbackContext):
update.effective_user.send_message(
'Send me your API Key 🙂'
'\nor /cancel',
reply_markup=ReplyKeyboardRemove()
)
return API_KEY
def api_key(update, context):
reply = update.message.text reply = update.message.text
context.user_data[Section.API_Key.value] = reply context.user_data[Section.API_Key.value] = reply
update.message.reply_text(f'Saved {reply} 💾', reply_markup=ReplyKeyboardRemove()) update.message.reply_text(f'Saved {reply} 💾', reply_markup=ReplyKeyboardRemove())
@ -96,7 +85,7 @@ config_handler = ConversationHandler(
MENU: [CallbackQueryHandler(menu)], MENU: [CallbackQueryHandler(menu)],
API_KEY: [ API_KEY: [
CommandHandler('cancel', cancel), CommandHandler('cancel', cancel),
MessageHandler(Filters.all, set_api_key), MessageHandler(Filters.all, api_key),
], ],
}, },
fallbacks=[CommandHandler('cancel', cancel)] fallbacks=[CommandHandler('cancel', cancel)]

View File

@ -2,14 +2,14 @@ from asyncio import sleep, run
from datetime import datetime from datetime import datetime
from pytimeparse import parse from pytimeparse import parse
from telegram import Update, ParseMode, ReplyKeyboardRemove from telegram import Update
from telegram.ext import CallbackContext, ConversationHandler from telegram.ext import CallbackContext
from telegram.ext.dispatcher import run_async from telegram.ext.dispatcher import run_async
from commands.config import Section from constants import Section
from market import Market from market import Market
from text import INTRO_TEXT from text import INTRO_TEXT
from utils import persistence, updater, current_timestamp, delta_timestamp, update_updater_data from utils import persistence, updater, current_timestamp, update_updater_data
SENDING = False SENDING = False

View File

@ -1,10 +1,10 @@
from telegram import Update, InlineKeyboardButton, ParseMode, InlineKeyboardMarkup, ReplyKeyboardRemove from telegram import Update, InlineKeyboardButton, ParseMode, InlineKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import CallbackContext, ConversationHandler, CommandHandler, CallbackQueryHandler, MessageHandler, Filters from telegram.ext import CallbackContext, ConversationHandler, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
from commands.config import Section
from commands.other import send_update_to_user from commands.other import send_update_to_user
from constants import Section
from limited_dict import LimitedDict from limited_dict import LimitedDict
from utils import parse_command, config from utils import config
ALL, SINGLE, EDIT, ADD, DELETE, BACK, ENABLED, FREQUENCY, INTERVAL, DATA = map(chr, range(10)) ALL, SINGLE, EDIT, ADD, DELETE, BACK, ENABLED, FREQUENCY, INTERVAL, DATA = map(chr, range(10))
END = str(ConversationHandler.END) END = str(ConversationHandler.END)

13
src/constants.py Normal file
View File

@ -0,0 +1,13 @@
from enum import Enum
class Section(Enum):
Watchlist = 'watchlist' # The list of Stocks/ETF to watch
Code = 'code' # Market code for a given stock, etf, etc.
API_Key = 'api_key' # Alpha Vantage API Key
Running = 'running' # Currently sending updates. Avoid overloading the API
Enabled = 'enabled' # Whether the bot should send automatic updates
Interval = 'interval' # Time axis of the graph
Frequency = 'frequency' # How ofter updates should be sent
LastRun = 'last_run' # Last time an update was sent to the user
CurrentToEdit = 'current_to_edit' # Current element to edit in the conversation handler

View File

@ -1,6 +1,6 @@
from datetime import datetime
from io import BytesIO from io import BytesIO
from typing import BinaryIO from typing import BinaryIO
from datetime import datetime
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd

View File

@ -1,10 +1,11 @@
import matplotlib as mpl import matplotlib as mpl
from telegram.ext import CommandHandler from telegram.ext import CommandHandler
from utils import updater
from commands.config import config_handler from commands.config import config_handler
from commands.watchlist import watchlist_handler
from commands.other import data, send_updates, start_handler, help_handler, stop_handler, error_handler from commands.other import data, send_updates, start_handler, help_handler, stop_handler, error_handler
from commands.watchlist import watchlist_handler
from utils import updater
def main(): def main():

View File

@ -1,9 +1,8 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from enum import Enum
from yaml import load, Loader
from telegram import Update from telegram import Update
from telegram.ext import PicklePersistence, Updater from telegram.ext import PicklePersistence, Updater
from yaml import load, Loader
DB_FILE = './data.db' DB_FILE = './data.db'
CONFIG_FILE = './config.yml' CONFIG_FILE = './config.yml'