~josealberto4444/apodnasabot

7529e0a8a34316b96bf21b78101617e121c8dd8c — José Alberto Orejuela García 2 years ago ec56641 0.1
Add apod function to the Telegram bot
1 files changed, 72 insertions(+), 47 deletions(-)

M telegram_bot.py
M telegram_bot.py => telegram_bot.py +72 -47
@@ 13,67 13,92 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import configparser
import api
import datetime
import logging
import telegram.ext

def read_config(section, key):
    config = configparser.ConfigParser()
    config.read('config.cfg')
    return config[section][key]

token = read_config('Telegram', 'token')
token = api.read_config('Telegram', 'token')
updater = telegram.ext.Updater(token=token, use_context=True)
dispatcher = updater.dispatcher

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

def send_link(update: telegram.Update, context: telegram.ext.CallbackContext):
    update.message.reply_text('https://www.youtube.com/embed/aiFD_LBx2nM')

send_link_handler = telegram.ext.CommandHandler('link', send_link)
dispatcher.add_handler(send_link_handler)

def image(update: telegram.Update, context: telegram.ext.CallbackContext):
    update.message.reply_photo(photo='https://apod.nasa.gov/apod/image/1908/PerseidsPloughCow.jpg', caption='This is a title.')

image_handler = telegram.ext.CommandHandler('image', image)
dispatcher.add_handler(image_handler)
def send_apod(update: telegram.Update, context: telegram.ext.CallbackContext):
    if context.args:
        date = context.args[0]
        if api.is_invalid(date):
            context.bot.send_message(chat_id=update.message.chat_id, text=api.is_invalid(date))
        else:
            apod = api.Apod(date)
    else:
        apod = api.Apod()
    if apod.media_type == 'image':
        update.message.reply_photo(photo=apod.link, caption=apod.title)
    elif apod.media_type == 'video':
        update.message.reply_text(apod.link)
    if apod.html:
        context.bot.send_message(chat_id=update.message.chat_id, text=apod.explanation, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True)
    else:
        context.bot.send_message(chat_id=update.message.chat_id, text=apod.explanation)

send_apod_handler = telegram.ext.CommandHandler('apod', send_apod)
dispatcher.add_handler(send_apod_handler)

#def date_time(update: telegram.Update, context: telegram.ext.CallbackContext):
#    print(update.message.date)
#
#date_time_handler = telegram.ext.CommandHandler('date', date_time)
#dispatcher.add_handler(date_time_handler)
#
#def send_link(update: telegram.Update, context: telegram.ext.CallbackContext):
#    update.message.reply_text('https://www.youtube.com/embed/aiFD_LBx2nM')
#
#send_link_handler = telegram.ext.CommandHandler('link', send_link)
#dispatcher.add_handler(send_link_handler)
#
#def image(update: telegram.Update, context: telegram.ext.CallbackContext):
#    update.message.reply_photo(photo='https://apod.nasa.gov/apod/image/1908/PerseidsPloughCow.jpg', caption='This is a title.')
#
#image_handler = telegram.ext.CommandHandler('image', image)
#dispatcher.add_handler(image_handler)

def start(update, context):
    context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
def start(update: telegram.Update, context: telegram.ext.CallbackContext):
    text = "I'm a bot, please talk to me!"
    context.bot.send_message(chat_id=update.message.chat_id, text=text)

start_handler = telegram.ext.CommandHandler('start', start)
dispatcher.add_handler(start_handler)

def echo(update, context):
    update.message.reply_text(update.message.text)

echo_handler = telegram.ext.MessageHandler(telegram.ext.Filters.text, echo)
dispatcher.add_handler(echo_handler)

def caps(update, context):
    print(context.args)
    text_caps = ' '.join(context.args).upper()
    context.bot.send_message(chat_id=update.message.chat_id, text=text_caps)

caps_handler = telegram.ext.CommandHandler('caps', caps)
dispatcher.add_handler(caps_handler)

def callback_alarm(context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id=context.job.context, text='BEEP')

def callback_timer(update: telegram.Update, context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id=update.message.chat_id,
                             text='Setting a timer for 1 minute!')

    context.job_queue.run_once(callback_alarm, 60, context=update.message.chat_id)

timer_handler = telegram.ext.CommandHandler('timer', callback_timer)
dispatcher.add_handler(timer_handler)
#def echo(update, context):
#    update.message.reply_text(update.message.text)
#
#echo_handler = telegram.ext.MessageHandler(telegram.ext.Filters.text, echo)
#dispatcher.add_handler(echo_handler)
#
#def caps(update, context):
#    print(context.args)
#    text_caps = ' '.join(context.args).upper()
#    context.bot.send_message(chat_id=update.message.chat_id, text=text_caps)
#
#caps_handler = telegram.ext.CommandHandler('caps', caps)
#dispatcher.add_handler(caps_handler)
#
#def callback_alarm(context: telegram.ext.CallbackContext):
#    context.bot.send_message(chat_id=context.job.context, text='BEEP')
#
#def callback_timer(update: telegram.Update, context: telegram.ext.CallbackContext):
#    context.bot.send_message(chat_id=update.message.chat_id,
#                             text='Setting a timer for 1 minute!')
#
#    context.job_queue.run_once(callback_alarm, 60, context=update.message.chat_id)
#
#timer_handler = telegram.ext.CommandHandler('timer', callback_timer)
#dispatcher.add_handler(timer_handler)

def unknown(update, context):
    context.bot.send_message(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command.")
def unknown(update: telegram.Update, context: telegram.ext.CallbackContext):
    text = "Sorry, I didn't understand that command."
    context.bot.send_message(chat_id=update.message.chat_id, text=text)

unknown_handler = telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown)
dispatcher.add_handler(unknown_handler)