def my_coins(bot, update): """ Show user's crypto coins list with USD rates :param bot: :param update: :return: None """ user_coins = user.get_coins_list(update.message.chat_id) if not user_coins: msg = "Please add some coins using /add_coin command." else: msg = "Your favourite Crypto Coins:\n" all_coins = app.get_config().get('coins') for coin in user_coins: rate = "" coin_id = "" if coin in all_coins: coin_id = all_coins[coin] rate = helper.get_coin_rate(coin_id) rate = "$" + rate msg += "<b>[" + coin + "] " + coin_id.title( ) + ": " + rate + "</b>\n" bot.send_message(chat_id=update.message.chat_id, text=msg, parse_mode="HTML", reply_markup=default_keyboard())
def send_rates(bot, crypto_user): """ Send selected crypto coins rates to user :param bot: :param model.user crypto_user: :return: None """ user_coins = crypto_user['coins'] if user_coins: msg = "Your favourite Crypto Coins:\n" all_coins = app.get_config().get('coins') for coin in user_coins: rate = "" coin_id = "" if coin in all_coins: coin_id = all_coins[coin] rate = helper.get_coin_rate(coin_id) rate = "$" + rate msg += "<b>[" + coin + "] " + coin_id.title( ) + ": " + rate + "</b>\n" bot.send_message(chat_id=crypto_user['chat_id'], text=msg, parse_mode="HTML", reply_markup=default_keyboard())
def regex_coins_or(): """ Returns regex representation of OR for all coins in config 'coins' :return: regex coins or str """ coins_regex_or = str() all_coins = app.get_config().get('coins') for coin in all_coins: coins_regex_or += "\[" + coin + "\]|" return coins_regex_or[:-1]
def add_coin_start(bot, update): """ Shows add coin message and available for adding coins :param bot: :param update: :return: next conversation state """ all_coins = app.get_config().get('coins') user_coins = user.get_coins_list(update.message.chat_id) if len(all_coins) == len(user_coins): markup = default_keyboard() msg = "All available coins were added to your list. You can add more coins in the config.json file." result = ConversationHandler.END else: buttons = [] counter = 1 for coin in all_coins: # don't show more than 10 coins if counter >= 10: break if coin not in user_coins: buttons.append(KeyboardButton("[" + coin + "]")) counter += 1 buttons.append(KeyboardButton("/cancel")) markup = get_markup(buttons, cols_num=2) msg = "Please select Crypto Coin from the list bellow for adding." result = AddCoinStates.ADD bot.send_message(chat_id=update.message.chat_id, text=msg, reply_markup=markup) return result
import logging from telegram.ext import (Updater, CommandHandler, ConversationHandler, MessageHandler, RegexHandler, Filters) from log import bot_logger from cmd import base from cmd import coins from app_config import app # Init Bot try: updater = Updater(token=app.get_config().get('bot_token')) except ValueError as e: updater = None bot_logger.log(logging.FATAL, str(e)) exit("Can not init Bot Updater object") updater.dispatcher.add_handler(CommandHandler("help", base.help_msg)) updater.dispatcher.add_handler(CommandHandler("my_coins", coins.my_coins)) updater.dispatcher.add_handler(CommandHandler("show_rates", coins.my_coins)) hello_handler = ConversationHandler( entry_points=[CommandHandler('start', base.start)], states={ base.CmdStates.START_SAVE_USER: [ MessageHandler(Filters.location, base.update_user), RegexHandler('^Skip$', base.update_user) ], }, fallbacks=[CommandHandler('cancel', base.cancel)])
from pymongo import MongoClient from pymongo.errors import (ConnectionFailure, OperationFailure) from log import bot_logger from app_config import app __author__ = '*****@*****.**' def model_exit(log_msg): bot_logger.log(logging.FATAL, log_msg) exit("Can not connect to MongoDB") DB_CONNECTION = None db_config = app.get_config().get('db') try: client = MongoClient('mongodb://%s:%s@%s' % (urllib.parse.quote_plus(db_config.get('user')), urllib.parse.quote_plus(db_config.get('pass')), urllib.parse.quote_plus(db_config.get('server')))) try: client.server_info() DB_CONNECTION = client except OperationFailure as e: model_exit(str(e)) except ConnectionFailure as e: model_exit(str(e))
import pytz import logging from telegram.ext import (Updater) from datetime import datetime from model import user from log import bot_logger from cmd import coins from app_config import app # Init Bot try: updater = Updater(token=app.get_config().get('bot_token')) except ValueError as e: updater = None bot_logger.log(logging.FATAL, str(e)) exit("Can not init Bot Updater object") users = user.get_all() utc_now = pytz.utc.localize(datetime.utcnow()) notification_times = app.get_config().get('notification_times') for user in users: user_time = utc_now.astimezone(pytz.timezone( user['time_zone'])).strftime("%H:%M") if user_time in notification_times: coins.send_rates(updater.bot, user)