async def start_handler(message: types.Message): """ inserting users to db with user id Creating message greeting person and about bot; Next, sending this message. :param message: aiogram message which get message id and etc. :return: message to telegram user which sended message. """ user = User(id=message.from_user.id, full_name=message.from_user.full_name, user_name=message.from_user.mention, company=None, reminder_action=None) try: Info.insert_info_user(user) finally: hello = "Hello!\nI'm StockBot, and I can help you monitor popular companies and their market prices!" helps = "My command:\n" \ "Stock price your follow companies -- /stock\n" \ "Help, list of commands(this command) -- /help\n" \ "List of your selected companies -- /mycompany\n" \ "List of all companies -- /companies" await message.answer(text=hello) await message.answer(text=helps)
def stock_function(message: types.Message): """ Create dict for collect stock price; Collect stock price in variable ts from Alpha_vantage API; Choose company market name from the list; Add info about company in dict; Create text about all companies stock price; return text consist of info about companies; :param message: aiogram message which get message id and etc. :return: message to telegram user, about Tesla stock price now. """ users = Info().get_all_users() user_companies = [] for u in users: if u.id == str(message.from_user.id): user_companies = u.company ts = TimeSeries(key=config.API_STOCK, output_format='pandas') stock_keys = [k for k in user_companies] stock_values = [] for k in stock_keys: stock, _ = ts.get_intraday(symbol=k, interval='1min') stock_values.append(str(stock['4. close'][0])) stock_companies = dict(zip(stock_keys, stock_values)) global STOCK STOCK = "Shares cost in:\n>>" + \ "\n>>".join([k + ": " + v + " $" for k, v in stock_companies.items()])
async def companies(message: types.Message, state: FSMContext): """ Take answer which enter user; Collect answer in current state :param message: aiogram message which get message id and etc. :param state: aiogram state which has user :return: data from answer user """ user_companies = message.text column = 'company' Info.update_info_user(column, user_companies, message.from_user.id) await message.answer("Thank you!\n Your companies are added in your list!") await state.finish()
async def button_companies_handler(message: types.Message): """ Collecting info about all companies which are in list; Create list consist of all companies; Creating text for answer with info about companies; Sending a message consisting of all companies. :param message: aiogram message which get message id and etc. :return: message to telegram user, about companies which are in list. """ all_companies = Info().get_all_companies() await message.answer( text="\n Market name ~~ Company name\n | \t" + "\n | \t".join([ c.market_name + "\t~~\t".center(20) + c.company_name for c in all_companies ]))
async def button_user_handler(message: types.Message): """ Collection info about users from DB which consisting of users; Creating text for answer with info about users; Sending a message consisting of user info; :param message: aiogram message which get message id and etc. :return: message to telegram user, about user which are in the db. """ users = Info().get_all_users() answer_message = "User info:\n| " + \ "\n| ".join([u.full_name + " ("+", ".join(u.company)+")" for u in users]) await message.reply( text=answer_message, reply_markup=types.reply_keyboard.ReplyKeyboardRemove(), reply=False)
async def button_addcompany_handler(message: types.Message): """ Collection info which user enter; update info about user in db, column company; Sending a message consisting of about what all his company added. :param message: aiogram message which get message id and etc. :return: message to telegram user, about what all his company added. """ all_companies = Info().get_all_companies() await message.reply( text="Choose company which you need: " "\n Market name ~~ Company name\n | \t" + "\n | \t".join([ c.market_name + "\t~~\t".center(20) + c.company_name for c in all_companies ]), reply_markup=types.reply_keyboard.ReplyKeyboardRemove(), reply=False) await message.answer(text="Please enter >market name< company:") await AddCompany.Q1.set()
async def button_company_handler(message: types.Message): """ Collecting info about user company which he has; Create list consist of user company; Creating text for answer with info about users; Sending a message consisting of company which user has. :param message: aiogram message which get message id and etc. :return: message to telegram user, about companies which is user has. """ users = Info().get_all_users() user_companies = [] for u in users: if u.id == str(message.from_user.id): user_companies = u.company answer_message = "Your companies:\n | " + "\n | ".join(user_companies) await message.reply( text=answer_message, reply_markup=types.reply_keyboard.ReplyKeyboardRemove(), reply=False)