Skip to content

Commit

Permalink
Merge pull request #5 from pythoncanarias/issue#3
Browse files Browse the repository at this point in the history
Issue#3
  • Loading branch information
PedroRamosRguez authored Nov 6, 2019
2 parents 679b907 + 585b483 commit c558d3c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from flask import Flask
from flask import request
from help import help_action
from welcome import welcome_action
from schedule import schedule_action
from sponsors import sponsors_action
Expand All @@ -13,6 +14,7 @@
log = app.logger

actions = {
'help': help_action,
'welcome': welcome_action,
'schedule': schedule_action,
'sponsors': sponsors_action,
Expand Down
38 changes: 38 additions & 0 deletions help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from parse_response import parse_response


def commands():
return ''.join(
'/track1 \n'
'/track2 \n'
'/agenda \n'
'/ponencias \n'
'/ponentes \n'
'/ayuda \n'
'/command \n'
'/commands \n'
)


def help_action(req: object = None) -> object:
"""help action
"""
command_list = commands()
result = {
"fulfillmentMessages": [
{
"payload": {
"telegram": {
"text": f"""Estos son algunos de los comandos que puedes utilizar. \n\n {command_list}\n\n Además, puedes hablar con el bot sin necesidad de solo utilizar comandos. Ej: ponencias del pyday""",
"parse_mode": "Markdown"
}
},
"platform": "TELEGRAM",
},
],
}
response = parse_response(result)
return response
47 changes: 41 additions & 6 deletions tracks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime
import requests
from parse_response import parse_response
BASE_URL = 'https://pythoncanarias.es'
Expand All @@ -26,26 +27,37 @@ def create_card(speaker_info: object) -> object:
"imageUri": BASE_URL + speaker_info['speakers'][0]['photo'],
"buttons": [
{
"text": "prueba button"
"text": f"{speaker_info['speakers'][0]['name']} {speaker_info['speakers'][0]['surname']}"
}
]
},
'platform': 'TELEGRAM',
}


def create_tracks_cards(tracks_information: list, selected_track: int = None):
def create_tracks_cards(tracks_information: list, selected_track: int = None, speaker_specific_hour: str = None) -> object:
if selected_track == 1:
track_one_information = tracks_information['result'][0]['schedule']
track_one = []
for track_info in track_one_information:
track_one.append(create_card(track_info))
print(track_info)
if speaker_specific_hour:
if datetime.datetime.strptime(speaker_specific_hour,'%H:%M') >= datetime.datetime.strptime(track_info['start'],'%H:%M') and datetime.datetime.strptime(speaker_specific_hour, '%H:%M') <= datetime.datetime.strptime(track_info['end'], '%H:%M'):
track_one.append(create_card(track_info))
break
else:
track_one.append(create_card(track_info))
track_cards = track_one
elif selected_track == 2:
track_two_information = tracks_information['result'][1]['schedule']
track_two = []
for track_info in track_two_information:
track_two.append(create_card(track_info))
if speaker_specific_hour:
if datetime.datetime.strptime(speaker_specific_hour,'%H:%M') >= datetime.datetime.strptime(track_info['start'],'%H:%M') and datetime.datetime.strptime(speaker_specific_hour, '%H:%M') <= datetime.datetime.strptime(track_info['end'], '%H:%M'):
track_one.append(create_card(track_info))
break
else:
track_two.append(create_card(track_info))
track_cards = track_two
else:
tracks = []
Expand Down Expand Up @@ -82,16 +94,39 @@ def create_tracks_cards(tracks_information: list, selected_track: int = None):
track_cards = tracks
return track_cards

def get_speaker_specific_hour():
return ''.join(str(datetime.datetime.now().hour) + ':' + str(datetime.datetime.now().minute))


def get_next_speaker():
return ''.join(str(datetime.datetime.now().hour + 1 ) + ':' + str(datetime.datetime.now().minute))


def tracks_action(req: object = None) -> object:
"""tracks action
"""
tracks_information = get_tracks_information()
if req['queryResult']['parameters']['number']:
selected_track = int(req['queryResult']['parameters']['number'])
tracks_cards = create_tracks_cards(tracks_information, selected_track)
if req['queryResult']['parameters']['now_entities']:
speaker_specific_hour = get_speaker_specific_hour()
tracks_cards = create_tracks_cards(tracks_information, selected_track, speaker_specific_hour)
else:
tracks_cards = create_tracks_cards(tracks_information, selected_track)
if req['queryResult']['parameters']['next_entities']:
speaker_specific_hour = get_next_speaker()
tracks_cards = create_tracks_cards(tracks_information, selected_track, speaker_specific_hour)
else:
tracks_cards = create_tracks_cards(tracks_information)
if req['queryResult']['parameters']['now_entities']:
speaker_specific_hour = get_speaker_specific_hour()
if req['queryResult']['parameters']['next_entities']:
speaker_specific_hour = get_next_speaker()
tracks_cards = create_tracks_cards(tracks_information, speaker_specific_hour)
else:
tracks_cards = create_tracks_cards(tracks_information)
if req['queryResult']['parameters']['next_entities']:
speaker_specific_hour = get_next_speaker()
tracks_cards = create_tracks_cards(tracks_information, selected_track, speaker_specific_hour)
result = {"fulfillmentMessages": []}
for track_card in tracks_cards:
result['fulfillmentMessages'].append(track_card)
Expand Down

0 comments on commit c558d3c

Please sign in to comment.