Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Apod module #29

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
conn = psycopg2.connect(DB, sslmode='require')
server = Flask(__name__)

NASA_API_KEY = os.environ.get('NASA_API_KEY')
nasa_url = 'https://api.nasa.gov/planetary/apod?api_key={}'.format(NASA_API_KEY)

@bot.message_handler(commands=['start'])
def start(message):
Expand All @@ -37,6 +39,26 @@ def process_query(message):
log(conn, intent, json.dumps(entities), message.text, message.from_user.id, None)
reply(bot, message, intent, entities)

@bot.callback_query_handler(func=lambda call: True)
def test_callback(call):
if(call.data=='explanation'):
response = requests.get(nasa_url)
if(response.status_code==200):
bot.send_message(call.message.chat.id, 'Explanation: {}'.format(response.json()['explanation']))
else:
bot.send_message(call.message.chat.id, 'Something went wrong. Try again.')
elif(call.data=='date'):
response = requests.get(nasa_url)
if(response.status_code==200):
bot.send_message(call.message.chat.id, 'Date: {}'.format(response.json()['date']))
else:
bot.send_message(call.message.chat.id, 'Something went wrong. Try again.')
elif(call.data=='copyright'):
response = requests.get(nasa_url)
if(response.status_code==200):
bot.send_message(call.message.chat.id, 'Copyright: {}'.format(response.json()['copyright']))
else:
bot.send_message(call.message.chat.id, 'Something went wrong. Try again.')

@server.route('/' + TOKEN, methods=['POST'])
def getMessage():
Expand Down
17 changes: 15 additions & 2 deletions modules.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from telebot import types

import xkcd


import requests
import os
NASA_API_KEY = os.environ.get('NASA_API_KEY')
nasa_url = 'https://api.nasa.gov/planetary/apod?api_key={}'.format(NASA_API_KEY)
def reply(bot, message, intent, entities):
if intent == 'xkcd':
random_comic = xkcd.getRandomComic()
Expand All @@ -12,5 +14,16 @@ def reply(bot, message, intent, entities):
random_comic.getTitle() + '*\n' + random_comic.getAltText() +
'\n' + random_comic.getExplanation(), parse_mode='Markdown',
reply_to_message_id=message.message_id, reply_markup=markup)
elif intent.lower() == 'apod':
response = requests.get(nasa_url)
if(response.status_code==200):
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton('Explanation', callback_data='explanation'))
markup.add(types.InlineKeyboardButton('Date', callback_data='date'))
markup.add(types.InlineKeyboardButton('Copyright', callback_data='copyright'))
bot.send_photo(message.chat.id, response.json()['url'], caption= response.json()['title'],reply_to_message_id=message.message_id, reply_markup = markup)
else:

bot.reply_to(message, 'Something went wrong. Try again!')
else:
bot.reply_to(message, message.text)