()
Main event of app
Main event of app
Main event of app
def event():
    """Main event of app"""
    log = logging.getLogger("/event")
    try:
        message = json.loads(request.data).get('text', None)

        if not message:
            return ExpectationFailed()

        # Check if message matches with pattern
        result = re.findall(r'время (в|во) (.*)', message)

        if not result:
            return ExpectationFailed()

        city = result[0][1]

        # Get places by city name (``query`` arg for ``places`` method)
        places = gmaps_api.places(city)

        if places['status'] == 'OK':
            # Get timezone by location
            tz = gmaps_api.timezone(
                places['results'][0]['geometry']['location']
            )
            if tz['status'] == 'OK':
                # Load timezone
                location = timezone(tz['timeZoneId'])
                localized_time = datetime.datetime.now(location)
                return json.dumps({
                    'text': 'Местное время в {city} сейчас {time}'.format(
                        city=places['results'][0]['formatted_address'],
                        time=localized_time.strftime(app.config['TIME_FORMAT'])
                    ),
                    'bot': app.config['BOT_NAME']
                }, ensure_ascii=False), 201, {
                           'Content-Type': 'application/json; charset=utf-8'
                       }
            else:
                return json.dumps({
                    'text': 'Не могу получить данные о часовом поясе',
                    'bot': app.config['BOT_NAME']
                }, ensure_ascii=False), 201, {
                           'Content-Type': 'application/json; charset=utf-8'
                       }
        else:
            return json.dumps({
                'text': 'Не могу найти город по запросу "{city}"'.format(
                    city=city
                ),
                'bot': app.config['BOT_NAME']
            }, ensure_ascii=False), 201, {
                       'Content-Type': 'application/json; charset=utf-8'
                   }

    # JSONDecodeError or have no connection with Google API services
    except Exception as e:
        log.exception(e)
        return ExpectationFailed()
def event ( ) : log = logging . getLogger ( "/event" ) try : message = json . loads ( request . data ) . get ( 'text' , None ) if not message : return ExpectationFailed ( ) # Check if message matches with pattern result = re . findall ( r'время (в|во) (.*)', messag e if not result : return ExpectationFailed ( ) city = result [ 0 ] [ 1 ] # Get places by city name (``query`` arg for ``places`` method) places = gmaps_api . places ( city ) if places [ 'status' ] == 'OK' : # Get timezone by location tz = gmaps_api . timezone ( places [ 'results' ] [ 0 ] [ 'geometry' ] [ 'location' ] ) if tz [ 'status' ] == 'OK' : # Load timezone location = timezone ( tz [ 'timeZoneId' ] ) localized_time = datetime . datetime . now ( location ) return json . dumps ( { 'text' : 'Местное время в {city} сейчас {time}'.format( city = places [ 'results' ] [ 0 ] [ 'formatted_address' ] , time = localized_time . strftime ( app . config [ 'TIME_FORMAT' ] ) ) , 'bot' : app . config [ 'BOT_NAME' ] } , ensure_ascii = False ) , 201 , { 'Content-Type' : 'application/json; charset=utf-8' } else : return json . dumps ( { 'text' : 'Не могу получить данные о часовом поясе', 'bot' : app . config [ 'BOT_NAME' ] } , ensure_ascii = False ) , 201 , { 'Content-Type' : 'application/json; charset=utf-8' } else : return json . dumps ( { 'text' : 'Не могу найти город по запросу "{city}"'.format( city = city ) , 'bot' : app . config [ 'BOT_NAME' ] } , ensure_ascii = False ) , 201 , { 'Content-Type' : 'application/json; charset=utf-8' } # JSONDecodeError or have no connection with Google API services except Exception as e : log . exception ( e ) return ExpectationFailed ( )