コード例 #1
0
def process_message(results, cli):
    print('WEATHER REQUEST')
    (context, output, message) = results

    json_context = json.dumps(context, indent=2)
    Client.update_client_context(cli.id, json_context)

    for m in output:
        if m == '[OK] Process request':
            print(f"LOCATION = {context['location']}")
            coordinates = (context['location'] == ' or coordinates')
            if not coordinates:
                context['location'] = process_keyword(context['location']) 
                print(f"LOCATION = {context['location']}")

            if context['date'] is None and context['time'] is None:
                if context['type'] == 'weather':
                    current_weather(context, cli, coordinates)
                elif context['type'] == 'forecast':
                    next_days_forecast(context, cli, coordinates)
            elif context['time'] is not None:
                simple_forecast(context, cli, coordinates, True)
            else:
                simple_forecast(context, cli, coordinates)
        elif m == 'Can you give me a location?':
            FacebookAPI.send_quick_replies(cli.id, WeatherMB.quick_reply_location(), m)
        elif m != '':
            FacebookAPI.send_message(cli.id, m)
コード例 #2
0
def process_message(results, cli):
    print('REMINDER REQUEST')
    (context, output, _) = results

    json_context = json.dumps(context, indent=2)
    Client.update_client_context(cli.id, json_context)

    for m in output:
        if m == '[OK] Process request':
            datetime = f"{context['date']} {context['time']}"
            word = context['word']
            description = context['description']
            print(datetime)

            SchedulerAPI.schedule_remind(datetime, cli.id, word, description)
            if context['flag'] == 'schedule':
                FacebookAPI.send_message(
                    cli.id,
                    f'I received your request to remind you {word} {description}{ReminderMB.getDatetimeDescription(datetime)}.'
                )
            elif context['flag'] == 'reschedule':
                FacebookAPI.send_message(
                    cli.id,
                    f'I\'ll remind you {word} {description}{ReminderMB.getDatetimeDescription(datetime)} then.'
                )
            Client.update_client_context(cli.id, None)
            cli.context = None

        elif m != '':
            FacebookAPI.send_message(cli.id, m)
コード例 #3
0
def send_reminder(cli, word, description):
    new_context = {'word': word, 'description': description}
    json_context = json.dumps(new_context, indent=2)
    Client.update_client_context(cli, json_context)
    print(json_context)
    FacebookAPI.send_quick_replies(
        cli, ReminderMB.reminder_quick_replies(),
        ReminderMB.getDescription(word, description))
コード例 #4
0
def next_days_forecast(context, cli, coordinates):
    if coordinates:
        prevision = WeatherAPI.getForecastCoordinates(context['lat'], context['long'])
    else:
        prevision = WeatherAPI.getForecastCity(context['location'])
    elements = WeatherMB.generateForecastPosts(prevision)
    FacebookAPI.send_list(cli.id, elements)
    Client.update_client_context(cli.id, None)
コード例 #5
0
def send_info(results, cli):
    print('INFO REQUEST')
    (context, output, _) = results

    for m in output:
        if m != '':
            FacebookAPI.send_message(cli.id, m)
            Client.update_client_context(cli.id, None)
            cli.context = None
コード例 #6
0
def __invalid_request(results, cli):
    print('ANYTHING ELSE')
    (newContext, output, _) = results
    json_newContext = json.dumps(newContext, indent=2)
    Client.update_client_context(cli.id, json_newContext)

    for m in output:
        FacebookAPI.send_message(cli.id, m)

    FacebookAPI.send_quick_replies(cli.id, NLPMB.quick_reply_features(),
                                   "Here's all the features, enjoy!")
コード例 #7
0
def process_message(results, cli):
    print('NEWS REQUEST')
    (newContext, output, message) = results

    json_newContext = json.dumps(newContext, indent=2)
    Client.update_client_context(cli.id, json_newContext)

    for m in output:
        if m == '[OK] Process request':
            print(f"ORIGINAL KEYWORD: {newContext['keyword']}")
            keyword = process_keyword(newContext['keyword'])
            print(f'RESULT KEYWORD: {keyword}')
            send_news(keyword, cli)
        elif m != '':
            FacebookAPI.send_message(cli.id, m)
コード例 #8
0
def send_news(keyword, cli):
    if keyword:
        print(f"KEYWORD: {keyword}")
        news = NewsAPI.getTopHeadlines(keyword)
    else:
        print(f"KEYWORD: NULL")
        news = NewsAPI.getTopHeadlines()

    elements = NewsMB.generateNewsPosts(news)
    if len(elements) == 0:
        FacebookAPI.send_message(
            cli.id,
            f"Sorry but I couldn't find anything related to your keyword: {keyword}"
        )
    else:
        FacebookAPI.send_carousel(cli.id, elements)
    Client.update_client_context(cli.id, None)
    cli.context = None
コード例 #9
0
def process_message(results, cli):
    print('EMAIL REQUEST')
    (newContext, output, message) = results

    json_newContext = json.dumps(newContext, indent=2)
    session = Session.get_session(cli.id)

    if not session:
        msg = GmailMB.generate_login(cli.id)
        FacebookAPI.send_carousel(cli.id, msg)
        Client.update_client_last_msg(cli.id, message)
    else:
        Client.update_client_context(cli.id, json_newContext)
        for m in output:
            if m == '[OK] Process request':
                send_mail(newContext, cli, session)
            elif m != '':
                FacebookAPI.send_message(cli.id, m)
コード例 #10
0
def send_mail(context, cli, session):
    # Load credentials from the session.
    credentials = google.oauth2.credentials.Credentials(**session.credentials)
    Session.update_session(cli.id, credentials)

    user_info_service = __start_service('oauth2', 'v2', credentials)
    user_email, user_name = GmailAPI.user_email(
        user_info_service)  # get user email

    gmail_service = __start_service('gmail', 'v1', credentials)
    (msg, subject, info) = GmailMB.generate_email(user_email, user_name,
                                                  context)
    GmailAPI.send_mail(gmail_service, 'me', msg)

    img_path = url_for('static',
                       filename=f'assets/img/mail/send.png',
                       _external=True)
    FacebookAPI.send_picture(cli.id, img_path, subject, info)
    FacebookAPI.send_message(cli.id, 'E-mail sent :)')
    Client.update_client_context(cli.id, None)
コード例 #11
0
def simple_forecast(context, cli, coordinates, hour=False):
    if coordinates:
        prevision = WeatherAPI.getForecastCoordinates(context['lat'], context['long'])
    else:
        prevision = WeatherAPI.getForecastCity(context['location'])

    if hour:
        icon, title, subtitle = WeatherMB.generateSimpleForecastDay(prevision, context['date'], context['time'])
        FacebookAPI.send_picture(cli.id, icon, title, subtitle)
    else:
        current_day = datetime.date.today().strftime('%y-%m-%d')
        current_hour = datetime.datetime.now().strftime('%H')

        if current_day in context['date']:
            if current_hour < '03:00:00':
                elements = WeatherMB.generateForecastDay(prevision, context['date'])
            else:
                elements = WeatherMB.generateForecastDay(prevision, context['date'], True)
        else:
            elements = WeatherMB.generateForecastDay(prevision, context['date'])
        FacebookAPI.send_list(cli.id, elements)
    Client.update_client_context(cli.id, None)