コード例 #1
0
def main():
    module_logger.info("Start the @CryptoCoinsInfoBot bot!")

    # create an object "bot"
    updater = Updater(token=TOKEN_BOT, use_context=True)
    dispatcher = updater.dispatcher

    # bot's error handler
    dispatcher.add_error_handler(error)

    # bot's command handlers
    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)

    # bot's text handlers
    text_update_handler = MessageHandler(Filters.text, filter_text_input)
    dispatcher.add_handler(text_update_handler)

    #
    # *** here put the job for the bot ***
    #
    # add tasks to parse APIs from sites-aggregators to local JSON-files, is used time interval, coz
    # APIs (CMC) have pricing plans with limits
    job_queue = updater.job_queue
    job_queue.run_repeating(download_api_coinslists_handler, TIME_INTERVAL, 5, context='coinmarketcap')
    job_queue.run_repeating(download_api_coinslists_handler, TIME_INTERVAL, 10, context='cryptocompare')

    # Start the Bot start_polling() method
    # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT
    updater.start_polling()
    updater.idle()
コード例 #2
0
def main():
    module_logger.info("Start the @CryptoCoinsInfoBot bot!")

    # create an object "bot"
    updater = Updater(token=TOKEN_BOT)
    dispatcher = updater.dispatcher

    # bot's error handler
    dispatcher.add_error_handler(error)

    # bot's command handlers
    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)

    # bot's text handlers
    text_update_handler = MessageHandler(Filters.text, filter_text_input)
    dispatcher.add_handler(text_update_handler)

    # here put the job for the bot
    job_queue = updater.job_queue
    job_queue.run_repeating(download_api_coinslists_handler, TIME_INTERVAL, 10, context='coinmarketcap')
    job_queue.run_repeating(download_api_coinslists_handler, TIME_INTERVAL, 40, context='cryptocompare')

    # Start the Bot start_polling() method
    # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT
    updater.start_polling()
    updater.idle()
コード例 #3
0
def filter_text_input(bot, update):

    message_info(update)

    usr_msg_text = update.effective_message.text
    usr_chat_id = update.message.chat_id

    # string for response
    text_response = ''

    # to work with a text request
    dict_to_request = text_simple(usr_msg_text)

    # if there is a reply_markup keyboard in a response from function - it's a menu request
    if dict_to_request['menutextresponse']:
        text_response = str(dict_to_request['menutextresponse'])

    # a simple request for a bot (coin name o coin ticket)
    elif dict_to_request['apiresponse1'] or dict_to_request['apiresponse2']:
        text_response = dict_to_request['apiresponse1'] + dict_to_request[
            'apiresponse2']
    """
    if text_response is not empty, bot send a response to user
    """
    if text_response:
        bot.send_message(usr_chat_id,
                         text_response,
                         parse_mode="Markdown",
                         reply_markup=dict_to_request['replymarkupresponse'])
        module_logger.info("Had send a message to a channel %s", usr_chat_id)

    else:
        module_logger.info(
            "Don't send a message for had recieve the message %s",
            usr_msg_text)
コード例 #4
0
def download_api_coinslists_handler(context):
    """
    the function to download API from the agregators sites to local file

    :param  bot: a telegram bot main object
    :type   bot: Bot

    :param  job: job.context is a name of the site-agregator, which has been send from job_queue.run_repeating... method
    :type   job: Job
    """

    job = context.job

    module_logger.info('Start a request to %s API', job.context)

    url = ''

    if job.context == 'coinmarketcap':
        url = COINMARKET_API_URL_COINLIST.format(CMC_API_KEY)
        fileoutputname = FILE_JSON_COINMARKET

    elif job.context == 'cryptocompare':
        url = CRYPTOCOMPARE_API_URL_COINLIST
        fileoutputname = FILE_JSON_CRYPTOCOMPARE

    response = requests.get(url)

    # extract a json from response to a class 'dict' or 'list'
    response_dict_list = response.json()

    if response.status_code == requests.codes.ok:

        # check if one of the APIs response is an error
        if (('status' in response_dict_list
             and response_dict_list['status']['error_code'] != 0)
                or (('Response' in response_dict_list)
                    and response_dict_list['Response'] is 'Error')):

            error_msg = ''
            if job.context == 'coinmarketcap':
                error_msg = response_dict_list['status']['error_message']

            elif job.context == 'cryptocompare':
                error_msg = response_dict_list['Message']

            module_logger.error('%s error message: %s' %
                                (job.context, error_msg))

        else:
            module_logger.info('Success download the coinslist from %s',
                               job.context)

            with open(fileoutputname, 'w') as outfile:
                json.dump(response_dict_list, outfile)
                module_logger.info('Success save it to %s', fileoutputname)

            # save a json to variable
            if job.context == 'coinmarketcap':
                jsonfiles.update_cmc_json(response_dict_list)

            elif job.context == 'cryptocompare':
                jsonfiles.update_cc_json(response_dict_list)

    else:
        module_logger.error('%s not successfully response', job.context)