Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
4.5 kB
3
Indexable
Never
# 處理訊息
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    user_id = event.source.user_id
    if user_id not in user_states:
        user_states[user_id]={}     # if user states not exist, initiate the dictionary

    if 'mode' not in user_states[user_id]: # User selecting the mode 'positive/negative'
        if event.message.text.lower() == 'mode':
            buttons_template=ButtonsTemplate(
                title='Choose Mode',
                text='請選擇模式: ',
                actions=[
                    PostbackAction(label='正面評論', data='positive'),
                    PostbackAction(label='負面評論', data='negative')
                ]
            )
            template_message = TemplateSendMessage(alt_text='Choose Mode', template=buttons_template)
            line_bot_api.reply_message(event.reply_token, template_message)
            
        else:
            line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Send "mode" to choose a mode.'))
    elif 'mood' not in user_states[user_id]:
        moods = ['like','sadness','anger','fear']
        if event.message.text.lower() == 'mood':
            buttons_template=ButtonsTemplate(
                title='Choose Mood',
                text='請選擇情感: ',
                actions=[
                    PostbackAction(label='喜歡', data='like'),
                    PostbackAction(label='難過', data='sadness'),
                    PostbackAction(label='生氣', data='anger'),
                    PostbackAction(label='害怕', data='fear')
                ]
            )
            template_message = TemplateSendMessage(alt_text='Choose Mood', template=buttons_template)
            line_bot_api.reply_message(event.reply_token, template_message)
        else:
            line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Send "mood" to choose a mood.'))
    else:   # User already chose  mode and mood
        user_input = event.message.text  # get the yt url
        user_mode = user_states[user_id]['mode']
        user_mood = user_states[user_id]['mood']

        # scrap tile and description od video
        title, description=scrape_video_info(user_input)

        reply_text = f'你選擇了 {user_mode} mode 和 {user_mood} mood. 影片標題:[{title}]。影片敘述:[{description}]。 \n 生成評論中...'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text=reply_text))

        model_input=get_prompt(title,description,user_mood)
        try:
            answer = response(model_input)
            print(answer)
            line_bot_api.reply_message(event.reply_token, TextSendMessage(answer))
        except:
            print(traceback.format_exc())
            line_bot_api.reply_message(event.reply_token, TextSendMessage('Error occured, please wait...'))

    

@handler.add(PostbackEvent)
def handle_message(event):
    print(event.postback.data)
    user_id = event.source.user_id
    data = event.postback.data

    # 在這裡根據按鈕回傳的資料進行處理
    if data == 'positive':
        # 如果是正面評論,進行相應處理
        user_states[user_id]['mode'] = 'positive'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='You selected positive mode.'))
    elif data == 'negative':
        # 如果是負面評論,進行相應處理
        user_states[user_id]['mode'] = 'negative'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='You selected negative mode.'))
    elif data == 'like':
        user_states[user_id]['mood'] = 'like'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='You selected mood like.'))
    elif data == 'sadness':
        user_states[user_id]['mood'] = 'sadness'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='You selected mood sadness.'))
    elif data == 'anger':
        user_states[user_id]['mood'] = 'anger'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='You selected mood anger.'))
    elif data == 'fear':
        user_states[user_id]['mood'] = 'fear'
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='You selected mood fear.'))
    else:
        # 其他情況
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Unknown selection.'))

Leave a Comment