Skip to content

Commit

Permalink
Merge pull request #1 from stephenhky/develop
Browse files Browse the repository at this point in the history
Inline button
  • Loading branch information
stephenhky authored Apr 8, 2023
2 parents 515a265 + ecc6afa commit 45da4f1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
92 changes: 76 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
CMD_SP500_MA = ['sp500ma']
CMD_NASDAQ_MA = ['nasdaqma']
CMD_DJI_MA = ['djima']
CMD_MAPLOT = ['maplot']


# polling flag
Expand All @@ -88,6 +89,32 @@ def add_modify_user(message):
raise e


keyboard_indices = {
'sp500': 'S&P 500',
'nasdaq': 'NASDAQ',
'dji': 'Dow Jone Index'
}

def makeMAKeyboard():
markup = telebot.types.InlineKeyboardMarkup()

for key, value in keyboard_indices.items():
markup.add(
telebot.types.InlineKeyboardButton(text=value, callback_data='button_maplot_{}'.format(key))
)

return markup


@bot.message_handler(commands=CMD_MAPLOT)
def display_ma_keyboard(message):
msg = bot.send_message(chat_id=message.chat.id,
text='Choose one index',
reply_markup=makeMAKeyboard(),
parse_mode='HTML')
bot.register_next_step_handler(msg, handle_maplot_callback_query)


@bot.message_handler(commands=CMD_START)
def start(message):
logging.info(message)
Expand Down Expand Up @@ -428,6 +455,29 @@ def sending_index_ma(message):
}


def handle_maplot_callback_query(call):
callbackstr = call.data

if callbackstr == 'button_maplot_sp500':
index = '^GSPC'
plottitle = 'S&P 500 (^GSPC)'
elif callbackstr == 'button_maplot_nasdaq':
index = '^IXIC'
plottitle = 'NASDAQ (^IXIC)'
elif callbackstr == 'button_maplot_dji':
index = '^DJI'
plottitle = 'Dow Jones (^DJI)'
else:
return {}

plot_info = plotting_index_ma(index, plottitle)
f = urllib.request.urlopen(plot_info['plot']['url'])
bot.send_photo(call.from_user.id, f)
return {
'ploturl': plot_info['plot']['url']
}


def lambda_handler(event, context):
message = json.loads(event['body'])
logging.info(message)
Expand All @@ -443,28 +493,38 @@ def lambda_handler(event, context):
update = telebot.types.Update.de_json(message)
logging.info(update)
print(update)
message = update.message
try:
add_modify_user(message)
except AttributeError:
pass

try:
bot.process_new_messages([message])
print('Processed.')
return {
'statusCode': 200,
'body': json.dumps({'approach': 'webhook'})
}
except AttributeError:
print('Telegram error.', file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
if update.message is not None:
message = update.message
try:
add_modify_user(message)
except AttributeError:
pass

try:
bot.process_new_messages([message])
print('Processed.')
return {
'statusCode': 200,
'body': json.dumps({'approach': 'webhook'})
}
except AttributeError:
print('Telegram error.', file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
return {
'statusCode': 200,
'body': json.dumps({'approach': 'webhook'})
}
elif update.callback_query is not None:
callback_cmd = update.callback_query.data
if callback_cmd.startswith('button_maplot_'):
handle_maplot_callback_query(update.callback_query)
return {
'statusCode': 200,
'body': json.dumps({'approach': 'webhook'})
}



if __name__ == '__main__':
ispolling = True
bot.polling()
Expand Down
1 change: 1 addition & 0 deletions messagetemplates/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/stockgma50: Computing the statistics of a symbol, and plotting the 50-day moving average (Arguments: symbol [startdate] [enddate]) (date format: YYYY-mm-dd)
/stockgma200: Computing the statistics of a symbol, and plotting the 200-day moving average (Arguments: symbol [startdate] [enddate]) (date format: YYYY-mm-dd)
/sp500ma: Plotting the S&P 500 index and its 50- and 200-day moving average.
/maplot: Plotting indices of your choice and its 50- and 200-day moving average.
2 changes: 2 additions & 0 deletions misc/telebot_inline_commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
help - Get a list of commands.
maplot - Plotting indices of your choice and its 50- and 200-day moving average.

0 comments on commit 45da4f1

Please sign in to comment.