botcontrol.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:ShakiraChatbot 作者: imTachu 项目源码 文件源码
def sing_a_song(intent_request):
    logger.info(intent_request)

    slots = get_slots(intent_request)
    song_slot = slots['song']
    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
    song_from_session = output_session_attributes.get('song')
    if song_from_session is None and song_slot is None:
        return elicit_slot(output_session_attributes,
                           intent_request['currentIntent']['name'],
                           slots, 'song', 'What song do you want me to sing?')
    if song_slot is not None:
        song = song_slot
    else:
        song = song_from_session
    if str(song).lower() == 'Hips don\'t lie'.lower():
        response = 'This is your lucky day! I can sing that one :)'
    else:
        response = 'I still don\'t know {}, but I can sing another one ;)'.format(song)
    filename = 'singing_files/shak_{}.mp3'.format(secure_random.random())
    try:
        audio_stream = polly.synthesize_speech(
            Text='And I\'m on tonight, you know my hips don\'t lie and I\'m starting to feel it\'s right. All the attraction, the tension, don\'t you see baby, this is perfection.',
            OutputFormat='mp3',
            VoiceId='Joanna')
        with closing(audio_stream['AudioStream']) as stream:
            bucket.put_object(Key=filename, Body=stream.read(), ACL='public-read')
    except BotoCoreError as error:
        logging.error(error)

    return close_with_response_card(intent_request['sessionAttributes'], 'Fulfilled', response, 'Click it, that\'s me singing', None, '{}/{}/{}'.format(s3_client.meta.endpoint_url, bucket.name, filename), 'https://s3.amazonaws.com/shakirachatbot/play_icon.png')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号