~josealberto4444/apodnasabot

apodnasabot/telegram_bot.py -rw-r--r-- 4.5 KiB
262d5437José Alberto Orejuela García Fix a new error in scrapping explanation 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright © 2019 José Alberto Orejuela García (josealberto4444)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# 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 api
import datetime
import logging
import telegram.ext

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_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.error:
        context.bot.send_message(chat_id=update.message.chat_id, text=apod.error)
    else:
        if apod.media_type == 'image':
            update.message.reply_photo(photo=apod.link, caption=apod.title)
        elif apod.media_type == 'video':
            text = apod.title + '\n' + apod.link
            update.message.reply_text(text)
        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: 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):
#    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: 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)

updater.start_polling()
updater.idle()