def get_categorie_surprise(sender, text, ACCESS_TOKEN): toutes_categories = liste_surprise_categories() choix_categories = [] for key in toutes_categories: if key not in choix_categories: choix_categories += [key] dict_choix_categories = {"Musées de beaux-arts à Paris" : "Beaux Arts", "Histoire et cultures d'ailleurs" : "Histoire", "Les musées les plus visités" : "Les plus visités", "Les musées insolites de Paris" : "Insolite", "Musées d'art moderne et contemporain" : "Art moderne", "Les musées à découvrir en famille à Paris" : "Famille" } btns = [] for cat in dict_choix_categories : btns.append( { "content_type": "text", "title": dict_choix_categories[cat], "payload": "surprise_cat*-/{}".format(cat) } ) send_quick_rep(sender, text, btns, ACCESS_TOKEN)
def exhibition_display(num, sender, payload=""): time.sleep(1) if num == 0: msg = "Il y a plusieurs types d'expositions, qu'est-ce qui t'intéresse le plus ?" btns_genre = get_genre_exhib()[1] send_quick_rep(sender, msg, btns_genre, ACCESS_TOKEN) elif num in range(1, 4): cards = get_exhib(payload[:-2], int(payload[-1])) send_card(sender, cards, ACCESS_TOKEN) btns = [{ "content_type": "text", "title": "Voir plus d'expos", "payload": "{}-{}".format(payload[:-2], int(payload[-1]) + 1) }, { "content_type": "text", "title": "Un autre genre", "payload": "exhibition-0" }, { "content_type": "text", "title": "Merci Electre", "payload": "Thanks" }] send_quick_rep(sender, "Veux-tu voir d'autres expos ?", btns, ACCESS_TOKEN)
def film_display(num, sender, latest): """ returns cards with films from the "latest" var (extracted with API) """ time.sleep(1) if num == 0: send_msg(sender, 'Voici les meilleurs films en salle', ACCESS_TOKEN) cards = [] for i in range(num, num + 9): etoile = u'\U0001F31F' * int(round(latest[i]['pressRating'])) cards.append({ "title": latest[i]['title'], "image_url": latest[i]['img_url'], "subtitle": "Note Presse : {}/5 \n Genre: {}".format( etoile, ', '.join(latest[i]['genre'])), "buttons": [ { "type": "web_url", "url": latest[i]['film_url'], "title": "Voir sur Allociné" }, { "type": "postback", "title": "Résumé", # rajout du séparateur *-/, derrière il y a l'ID du film "payload": "Summary_cine*-/{}".format(latest[i]["ID"]) }, { "type": "postback", "title": "Match des Critiques", # rajout du séparateur *-/, derrière il y a l'ID du film "payload": "Critiques_cine*-/{}".format(latest[i]["ID"]) } ] }) send_card(sender, cards, ACCESS_TOKEN) btns = [{ "content_type": "text", "title": "Plus de films", "payload": "sorties_cine-10" }, { "content_type": "text", "title": "Choisir un genre", "payload": "genres_cine" }, { "content_type": "text", "title": "Merci Electre", "payload": "Thanks" }] if num == 0: time.sleep(4) send_quick_rep( sender, "Tu veux voir d'autres films ? Ou plutôt voir un genre de film particulier ?", btns, ACCESS_TOKEN)
def send_location(sender): send_msg( sender, "Pourriez-vous nous indiquer votre position en cliquant sur le bouton ci-dessous? ", ACCESS_TOKEN) btns = [{ "content_type": "location", "title": "Je me géocalise", "payload": "bouton_geocalisation" }] send_quick_rep( sender, "Merci beaucoup nous allons trouver les films qui se situent autour de vous!", btns, ACCESS_TOKEN)
def handle_event(): #NB: la requête API est effectuée à chaque msg entrant data = request.json # Verifie que c'est la 1e fois que le message est recu => empeche spam # with open("backend/others/msg_spam.json", "r") as f: # msg_spam = json.load(f) # if data['entry'][0]['time'] in msg_spam: # return "" logging.info("DATA: {}".format(data)) event = data['entry'][0]['messaging'][0] #Log user details in the database sender = event['sender']['id'] user = user_details(sender, ACCESS_TOKEN) typing_bubble(sender, ACCESS_TOKEN) if "message" in event: #handles quick replies (buttons at the bottom of the conversation) if 'quick_reply' in event['message']: payload = event['message']['quick_reply']['payload'] # Gestion du cas cinema if payload[:12] == "sorties_cine": latest = get_details_cinema() num = int( event['message']['quick_reply']['payload'].split("-")[1]) #on affiche les cartes des films film_display(num, sender, latest) elif payload[:12] == "cine_around": send_location(sender) send_msg(sender, " ", ACCESS_TOKEN) elif "genres_cine" in event['message']['quick_reply']['payload']: send_msg(sender, "Quel genre t'intéresse?", ACCESS_TOKEN) btns = get_genre_movie(sender)[1] send_quick_rep(sender, "Voici les genres possibles: ", btns, ACCESS_TOKEN) #boucle pour récupérer le genre du film: elif event['message']['quick_reply']['payload'] in [ genre for genre in get_genre_movie(sender)[2] if genre != "Not_interested" ]: #on récupère le genre du film pour obtenir une liste des derniers films sortis mais filtrée par le genre p = event['message']['quick_reply']['payload'][:-2] ranking = get_topmovies_genre(p) #on affiche les cartes des films triés par genre film_display_bygenre(sender, p) # Gestion des cas sur l'art elif payload[:3] == "art": art_buttons(sender, "Clique sur ce qui t'intéresse", ACCESS_TOKEN) # Si l'utilisateur veut une surprise elif payload == "surprise-0": get_categorie_surprise(sender, "Voila les catégories disponibles !", ACCESS_TOKEN) elif payload[:12] == "surprise_cat": category = payload.split("*-/")[1] get_musee_surprise(sender, category, ACCESS_TOKEN) # Si l'utilisateur veut une expo elif payload[:10] == "exhibition": num = int(payload[-1]) exhibition_display(num, sender) elif payload[:-2] in get_genre_exhib()[0]: num = int(payload[-1]) exhibition_display(num, sender, payload) elif payload == "Not_interested": send_msg( sender, "Je suis en train d'apprendre de nouvelles choses, mais pour l'instant je ne peux que te conseiller en cinéma et en expo ! Pour la peine, voici une dadjoke de consolation:", ACCESS_TOKEN) send_msg(sender, random_joke(), ACCESS_TOKEN) send_msg(sender, "A bientôt !", ACCESS_TOKEN) elif payload == "Thanks": send_msg(sender, "Ravie d'avoir pu t'aider ! A bientôt :)", ACCESS_TOKEN) #handles stickers sent by user. For the moment, only the like button is recognized elif 'sticker_id' in event['message']: sticker = event['message']['sticker_id'] if int(sticker) == 369239263222822: # Like button sticker send_msg(sender, "De rien ! ;)", ACCESS_TOKEN) else: send_msg(sender, "Nice sticker {} !".format(user[1]), ACCESS_TOKEN) #handles attachments (images, selfies, docs...) elif 'attachments' in event['message']: attachments = event['message']['attachments'][0] if attachments['type'] == 'image': if ".gif" in attachments['payload']['url']: send_msg(sender, "Super GIF !", ACCESS_TOKEN) else: send_msg(sender, "Jolie image :)", ACCESS_TOKEN) else: send_msg( sender, "J'ai bien reçu ton fichier, mais pour l'instant je ne peux pas le traiter !", ACCESS_TOKEN) #handles text sent by user (including unicode emojis 😰, 😀) else: message = event['message']['text'].lower() analyse_text(message, sender, user, ACCESS_TOKEN) elif "postback" in event: if event['postback']['payload'] == "first_conv": welcome(sender, user) elif "Summary_cine" in event['postback']['payload']: ID = event['postback']['payload'].split("*-/")[1] latest = get_details_cinema() result = [x for x in latest if x["ID"] == ID][0] send_msg( sender, "-- " + result['title'] + " -- Résumé -- \n\n" + result['summary'], ACCESS_TOKEN) time.sleep(4) start_buttons(sender, "Autre chose ?", ACCESS_TOKEN) elif "Critiques_cine" in event['postback']['payload']: ID = event['postback']['payload'].split("*-/")[1] latest = get_details_cinema() result = [x for x in latest if x["ID"] == ID][0] if "good_critique" in result: send_msg( sender, "-- " + result['title'] + " -- Critiques -- \n\n" + result['good_critique'], ACCESS_TOKEN) send_msg(sender, result['bad_critique'], ACCESS_TOKEN) else: send_msg(sender, "Je n'ai malheureusement pas trouvé de critiques", ACCESS_TOKEN) time.sleep(8) start_buttons(sender, "Autre chose ?", ACCESS_TOKEN) elif "surprise" in event['postback']['payload']: action, ID = event['postback']['payload'].split("*-/") info = get_details_surprise(ID, action) send_msg(sender, info, ACCESS_TOKEN) elif event['postback']['payload'][:12] == "Summary_expo": x = event['postback']['payload'].split("*-/") # x[0]: 'Summary_expo' / x[1]: exhib_ID data = get_detail_exhib(x[1]) send_msg(sender, "Description: " + data['summary'], ACCESS_TOKEN) send_msg(sender, "Horaires: " + data['timetable'], ACCESS_TOKEN) send_msg(sender, "Prix: " + data['price'], ACCESS_TOKEN) else: send_msg(sender, "Je n'ai pas compris ta demande... 😰", ACCESS_TOKEN) return "ok"