def creat_user_flex_message(userProfile): global userName, userGender, userWeight, userAge, userId result = lineUser.objects.get(userId=userProfile.user_id) userName = result.userName userGender = result.userGender userWeight = result.userWeight userHeight = result.userHeight userAge = result.userAge userId = result.userId print(userName) userBmi = round(userWeight / (userHeight / 100)**2, 2) bubble = BubbleContainer( size='kilo', hero=ImageComponent( url=userProfile.picture_url, size='full', aspectRatio="20:13", aspectMode="cover", ), body=BoxComponent( layout='vertical', size='sm', contents=[ BoxComponent( layout='vertical', margin='sm', spacing='sm', contents=[ TextComponent(text='個人基本資料', weight="bold", size="lg", style="normal", decoration="none", gravity="center", align="center"), BoxComponent(layout='vertical', margin='sm', spacing='sm', contents=[ BoxComponent( layout="baseline", pacing="sm", contents=[ TextComponent(type="text", text="用戶名:", color="#aaaaaa", size="sm", flex=2), TextComponent( type='text', text=str(userName), wrap=True, color="#666666", size="sm", flex=3, ) ]) ]), BoxComponent(layout='vertical', margin='sm', spacing='sm', contents=[ BoxComponent( layout="baseline", spacing="sm", contents=[ TextComponent(type="text", text="性別:", color="#aaaaaa", size="sm", flex=2), TextComponent( type='text', text=str(userGender), wrap=True, color="#666666", size="sm", flex=5, ) ]) ]), BoxComponent(layout='vertical', margin='sm', spacing='sm', contents=[ BoxComponent( layout="baseline", spacing="sm", contents=[ TextComponent(type="text", text="年齡:", color="#aaaaaa", size="sm", flex=2), TextComponent( type='text', text=str(userAge), wrap=True, color="#666666", size="sm", flex=5, ) ]) ]), BoxComponent(layout='vertical', margin='sm', spacing='sm', contents=[ BoxComponent( layout="baseline", spacing="sm", contents=[ TextComponent(type="text", text="身高:", color="#aaaaaa", size="sm", flex=2), TextComponent( type='text', text=str(userHeight), wrap=True, color="#666666", size="sm", flex=5, ) ]) ]), BoxComponent(layout='vertical', margin='sm', spacing='sm', contents=[ BoxComponent( layout="baseline", spacing="sm", contents=[ TextComponent(type="text", text="體重:", color="#aaaaaa", size="sm", flex=2), TextComponent( type='text', text=str(userWeight), wrap=True, color="#666666", size="sm", flex=5, ) ]) ]), BoxComponent(layout='vertical', margin='sm', spacing='sm', contents=[ BoxComponent( layout="baseline", spacing="sm", contents=[ TextComponent(type="text", text="BMI:", color="#aaaaaa", size="sm", flex=2), TextComponent( type="text", text=str(userBmi), wrap=True, color="#666666", size="sm", flex=5, ) ]) ]), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ ButtonComponent( style="primary", height="sm", action=URIAction(label="編輯", uri=host + "/nutritionweb/edit/" + userId + "/edit"), color="#EA8244", gravity="center", ), SpacerComponent(size='sm'), ], flex=0, )) return FlexSendMessage(alt_text="檢視個人基本資料", contents=bubble)
def create_carousel(items): # Flaskのロガーを取得 logger = logging.getLogger('flask.app') logger.debug(len(items)) bubbles = [] for item in items[:10]: hero = ImageComponent(url=item.img, size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri=item.url, label='label')) # body title_text_component = TextComponent( text=item.title, weight='bold', size='md', color='#5c5752', ) # tagをTextComponentの配列に整形 tag_components = [] for tag in item.tags: # if i == 0: # tag_components.append( # TextComponent( # text=tag, # size='sm', # color='#5c5752', # flex=0, # ) # ) # continue tag_components.append( TextComponent( text=tag, size='sm', color='#5c5752', flex=1, )) # 調理方法をiconComponentとtextComponentの配列に整形 cooking_method_components = [] for method in item.cooking_methods: cooking_method_components.append( IconComponent(url=DOMAIN + method['icon'], size='sm', color='#5c5752')) cooking_method_components.append( TextComponent(text=method['name'], size='sm', color='#5c5752', flex=1)) # 組み立て body = BoxComponent( layout='vertical', margin='xl', spacing='xl', contents=[ # title title_text_component, # tags, cooking methods BoxComponent(layout='vertical', spacing='md', contents=[ BoxComponent(layout='baseline', spacing='xl', contents=tag_components), BoxComponent( layout='baseline', spacing='sm', contents=cooking_method_components) ]), ], ) footer = BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent(style='link', height='sm', action=URIAction( label='WEBSITE', uri="https://example.com")) ]) bubble = BubbleContainer( direction='ltr', hero=hero, body=body, footer=footer, ) bubbles.append(bubble) carousel = CarouselContainer(bubbles) return carousel
def flex_send_message_entity(self, entity): if not entity.match: return False value = _Tabelog_Value() value.set_value_tp(self.select_tabelog_entity(entity.name)) if value.name == '': return False image_url = my_s3_link_url(value.image_key) map_url = value.url + 'dtlmap/' stars_url = self._review_stars_url(value.score) bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url=image_url, size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction( uri=value.url ) ), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text=value.name, weight='bold', size='xl' ), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent(size='sm', url=stars_url[0]), IconComponent(size='sm', url=stars_url[1]), IconComponent(size='sm', url=stars_url[2]), IconComponent(size='sm', url=stars_url[3]), IconComponent(size='sm', url=stars_url[4]), TextComponent( text='{:.2f}'.format(value.score), size='md', color='#999999', margin='md',flex=0) ] ), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='最寄り駅', color='#aaaaaa', size='xs', flex=1 ), TextComponent( text=value.station, wrap=True, color='#666666', size='sm', flex=3 ) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='ジャンル', color='#aaaaaa', size='xs', flex=1 ), TextComponent( text=value.genre, wrap=True, color='#666666', size='sm', flex=3 ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ SpacerComponent(size='sm'), ButtonComponent( style='primary', height='sm', action=URIAction(label='地図を見る', uri=map_url), ), # SeparatorComponent(), ButtonComponent( style='link', height='sm', action=MessageAction(label='ねこ', text='ねこ') ) ] ), ) message = FlexSendMessage(alt_text="tabelog flex", contents=bubble) return message
def handle_text_message(event): text = event.message.text profile = line_bot_api.get_profile(event.source.user_id) userInfo = checkLineUserID(event.source.user_id) if text == 'info': if isinstance(event.source, SourceUser): if userInfo: # line_bot_api.reply_message( # event.reply_token, [ # ImageSendMessage( # original_content_url=ifNoneValue(profile.picture_url,"https://www.aaa.com/aa"), # preview_image_url=ifNoneValue(profile.picture_url,"https://www.aaa.com/aa"), # ) # ] # ) f = open("ChatBotTemplate/user_info.json", "r") bubble_string = f.read() f.close() bubble_string = bubble_string.replace( "/*picture_url*/", ifNoneValue(profile.picture_url, "https://www.aaa.com/aa")) bubble_string = bubble_string.replace( "/*first_name*/", ifNoneValue(userInfo["first_name"], " ")) bubble_string = bubble_string.replace( "/*last_name*/", ifNoneValue(userInfo["last_name"], " ")) bubble_string = bubble_string.replace( "/*email*/", ifNoneValue(userInfo["email"], "-")) bubble_string = bubble_string.replace( "/*phone*/", ifNoneValue(userInfo["phone"], "-")) message = FlexSendMessage(alt_text="User Info", contents=json.loads(bubble_string)) line_bot_api.reply_message(event.reply_token, message) # line_bot_api.push_message( # event.source.user_id, [ # TextSendMessage( # text='คุณ {} {}\nEmail {}\nTel {}' # .format(userInfo["first_name"],userInfo["last_name"],userInfo["email"],userInfo["phone"]) # ) # ] # ) else: line_bot_api.reply_message( event.reply_token, TextMessage(text="Bot can't use profile API without user ID")) elif text == 'tel': if isinstance(event.source, SourceUser): line_bot_api.reply_message(event.reply_token, TextMessage(text="tel:0868905463")) else: line_bot_api.reply_message( event.reply_token, TextMessage(text="Bot can't use profile API without user ID")) elif text == 'Clinical': if isinstance(event.source, SourceUser): labOrdersUser = getClinicalUserID(event.source.user_id) if labOrdersUser: first_name = '' lastt_name = '' if userInfo: first_name = userInfo["first_name"] # last_name = userInfo["last_name"] # line_bot_api.push_message( # event.source.user_id, [ # ImageSendMessage( # original_content_url=profile.picture_url, # preview_image_url=profile.picture_url # ) # ] # ) # line_bot_api.push_message( # event.source.user_id, [ # TextMessage(text="คุณ {} {}\nผล Clinical 5 ครั้งล่าสุด".format(userInfo["first_name"],userInfo["last_name"])) # ] # ) for labOrder in labOrdersUser: f = open("ChatBotTemplate/clinical_result.json", "r") bubble_string = f.read() f.close() bubble_string = bubble_string.replace( "/*order_id*/", labOrder["order_id"]) bubble_string = bubble_string.replace( "/*patient_name*/", labOrder["patient_name"]) bubble_string = bubble_string.replace( "/*received_date*/", labOrder["received_date"].strftime( '%Y-%m-%d-%H:%M:%S')) bubble_string = bubble_string.replace( "/*hn*/", ifNoneValue(labOrder["hn"], " ")) bubble_string = bubble_string.replace( "/*sex_desc*/", labOrder["sex_desc"]) bubble_string = bubble_string.replace( "/*test_item_desc_list*/", labOrder["test_item_desc_list"]) message = FlexSendMessage( alt_text="Lab Result", contents=json.loads(bubble_string)) # line_bot_api.push_message( # event.source.user_id,message) line_bot_api.reply_message(event.reply_token, message) # TextSendMessage( # text='Lab Order {}\nReceived Date {}\nline://app/1654084194-9JVnOQEb?order_id={}' # .format(labOrder["order_id"],labOrder["received_date"],labOrder["order_id"],labOrder["order_id"])) else: first_name = '' lastt_name = '' if userInfo: first_name = userInfo["first_name"] last_name = userInfo["last_name"] line_bot_api.reply_message( event.reply_token, TextMessage(text="คุณ {} {}\nไม่มีผล Clinical".format( userInfo["first_name"], userInfo["last_name"]))) else: line_bot_api.reply_message( event.reply_token, TextMessage(text="Bot can't use profile API without user ID")) elif text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + str(profile.status_message)) ]) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text="Bot can't use profile API without user ID")) elif text == 'quota': quota = line_bot_api.get_message_quota() line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='type: ' + quota.type), TextSendMessage(text='value: ' + str(quota.value)) ]) elif text == 'quota_consumption': quota_consumption = line_bot_api.get_message_quota_consumption() line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='total usage: ' + str(quota_consumption.total_usage)), ]) elif text == 'push': line_bot_api.push_message(event.source.user_id, [ TextSendMessage(text='PUSH!'), ]) elif text == 'multicast': line_bot_api.multicast([event.source.user_id], [ TextSendMessage(text='THIS IS A MULTICAST MESSAGE'), ]) elif text == 'broadcast': line_bot_api.broadcast([ TextSendMessage(text='THIS IS A BROADCAST MESSAGE'), ]) elif text.startswith('broadcast '): # broadcast 20190505 date = text.split(' ')[1] print("Getting broadcast result: " + date) result = line_bot_api.get_message_delivery_broadcast(date) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Number of sent broadcast messages: ' + date), TextSendMessage(text='status: ' + str(result.status)), TextSendMessage(text='success: ' + str(result.success)), ]) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'image': url = request.url_root + '/static/logo.png' app.logger.info("url=" + url) line_bot_api.reply_message(event.reply_token, ImageSendMessage(url, url)) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage(alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction( label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ]), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Place', color='#aaaaaa', size='sm', flex=1), TextComponent(text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Time', color='#aaaaaa', size='sm', flex=1), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent(style='link', height='sm', action=URIAction( label='WEBSITE', uri="https://example.com")) ]), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif text == 'flex_update_1': bubble_string = """ { "type": "bubble", "body": { "type": "box", "layout": "vertical", "contents": [ { "type": "image", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip3.jpg", "position": "relative", "size": "full", "aspectMode": "cover", "aspectRatio": "1:1", "gravity": "center" }, { "type": "box", "layout": "horizontal", "contents": [ { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": "Brown Hotel", "weight": "bold", "size": "xl", "color": "#ffffff" }, { "type": "box", "layout": "baseline", "margin": "md", "contents": [ { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png" }, { "type": "text", "text": "4.0", "size": "sm", "color": "#d6d6d6", "margin": "md", "flex": 0 } ] } ] }, { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": "¥62,000", "color": "#a9a9a9", "decoration": "line-through", "align": "end" }, { "type": "text", "text": "¥42,000", "color": "#ebebeb", "size": "xl", "align": "end" } ] } ], "position": "absolute", "offsetBottom": "0px", "offsetStart": "0px", "offsetEnd": "0px", "backgroundColor": "#00000099", "paddingAll": "20px" }, { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": "SALE", "color": "#ffffff" } ], "position": "absolute", "backgroundColor": "#ff2600", "cornerRadius": "20px", "paddingAll": "5px", "offsetTop": "10px", "offsetEnd": "10px", "paddingStart": "10px", "paddingEnd": "10px" } ], "paddingAll": "0px" } } """ message = FlexSendMessage(alt_text="hello", contents=json.loads(bubble_string)) line_bot_api.reply_message(event.reply_token, message) elif text == 'quick_reply': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply(items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1")), QuickReplyButton( action=MessageAction(label="label2", text="text2")), QuickReplyButton(action=DatetimePickerAction( label="label3", data="data3", mode="date")), QuickReplyButton(action=CameraAction(label="label4")), QuickReplyButton(action=CameraRollAction(label="label5")), QuickReplyButton(action=LocationAction(label="label6")), ]))) elif text == 'link_token' and isinstance(event.source, SourceUser): link_token_response = line_bot_api.issue_link_token( event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='link_token: ' + link_token_response.link_token) ]) elif text == 'insight_message_delivery': today = datetime.date.today().strftime("%Y%m%d") response = line_bot_api.get_insight_message_delivery(today) if response.status == 'ready': messages = [ TextSendMessage(text='broadcast: ' + str(response.broadcast)), TextSendMessage(text='targeting: ' + str(response.targeting)), ] else: messages = [TextSendMessage(text='status: ' + response.status)] line_bot_api.reply_message(event.reply_token, messages) elif text == 'insight_followers': today = datetime.date.today().strftime("%Y%m%d") response = line_bot_api.get_insight_followers(today) if response.status == 'ready': messages = [ TextSendMessage(text='followers: ' + str(response.followers)), TextSendMessage(text='targetedReaches: ' + str(response.targeted_reaches)), TextSendMessage(text='blocks: ' + str(response.blocks)), ] else: messages = [TextSendMessage(text='status: ' + response.status)] line_bot_api.reply_message(event.reply_token, messages) elif text == 'insight_demographic': response = line_bot_api.get_insight_demographic() if response.available: messages = [ "{gender}: {percentage}".format(gender=it.gender, percentage=it.percentage) for it in response.genders ] else: messages = [TextSendMessage(text='available: false')] line_bot_api.reply_message(event.reply_token, messages) else: line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))
def handle_message(self, event): self._message_data_save(event) text = event.message.text if text == 'finished': try: user_data = LineInputData.objects.filter(user_id=event.source.user) user = User.create( first_name=user_data.object.filter(question='first_name').answer, last_name=user_data.object.filter(question='last_name').answer, ) user_info = UserInfo.create( user=user, motive=user_data.object.filter(question='motive').answer, ) job_histories_count = user_data.filter(question__startwith='job_history').count for i in range(job_histories_count): JobHistory.create( user_info=user_info, start_date=user_data.object.filter(question=f'start_date_{i}').answer, end_date=user_data.object.filter(question=f'end_date_{i}').answer, history=user_data.object.filter(question=f'job_history_{i}').answer, ) logger.info(f'Create user:{user.id} user_info:{user_info.id}') except Exception: raise DataError elif text == 'profile': if isinstance(event.source, SourceUser): profile = self.line_bot_api.get_profile(event.source.user_id) self.line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + profile.status_message) ] ) else: self.line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't use profile API without user ID")) elif text == 'bye': if isinstance(event.source, SourceGroup): self.line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Leaving group')) self.line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): self.line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Leaving group')) self.line_bot_api.leave_room(event.source.room_id) else: self.line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage( alt_text='Confirm alt text', template=confirm_template) self.line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage( alt_text='Buttons alt text', template=buttons_template) self.line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage( alt_text='Carousel alt text', template=carousel_template) self.line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) self.line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label') ), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ] ), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Place', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5 ) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Time', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent( style='link', height='sm', action=URIAction( label='WEBSITE', uri="https://example.com") ) ] ), ) message = FlexSendMessage(alt_text="hello", contents=bubble) self.line_bot_api.reply_message( event.reply_token, message ) elif text == 'quick_reply': self.line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply( items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1") ), QuickReplyButton( action=MessageAction(label="label2", text="text2") ), QuickReplyButton( action=DatetimePickerAction(label="label3", data="data3", mode="date") ), QuickReplyButton( action=CameraAction(label="label4") ), QuickReplyButton( action=CameraRollAction(label="label5") ), QuickReplyButton( action=LocationAction(label="label6") ), ]))) elif text == "liff": self.line_bot_api.reply_message( event.reply_token, [ TextSendMessage( text='https://line.me/R/app/1615588360-p4vKyQMV'), ] ) elif text == "purchase": previous_bot_message = "question" self.line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='商品の購入ですね!\nそれではいくつか質問させてもらいますね。'), TextSendMessage(text='これから購入のための情報をお聞きしますが、'), ConfirmTemplate( action=[ MessageAction( label='chat', text='ちゃっとで答える' ), MessageAction( label='webpage', text='Webページで入力する' ), ] ) ] ) elif previous_bot_message == "question" and text == "chat": previous_bot_message = "name" self.line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='ありがとうございます!!'), TextSendMessage(text='さっそくお名前を教えてもらえますか?'), ] )
def handle_content_message(event): if isinstance(event.message, ImageMessage): ext = 'jpg' elif isinstance(event.message, VideoMessage): ext = 'mp4' elif isinstance(event.message, AudioMessage): ext = 'm4a' else: return #save jpg message_content = line_bot_api.get_message_content(event.message.id) #save file.jpg with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix=ext + '-', delete=False) as tf: for chunk in message_content.iter_content(): tf.write(chunk) tempfile_path = tf.name dist_path = tempfile_path + '.' + ext dist_name = os.path.basename(dist_path) os.rename(tempfile_path, dist_path) message_content = os.path.join('static', 'tmp', dist_name) with open(message_content, 'rb') as ff: message_content = ff.read() print get_prediction(message_content, 'v3-219812', 'ICN7521245635834906') abc = get_prediction(message_content, 'v3-219812', 'ICN7521245635834906') a1 = str(abc) result = re.search('(?<=score: ).*(?=\n)', a1).group() result2 = re.search('(?<=display_name: ").*(?="\n)', a1).group() b01 = float(result) a1 = str(abc) a2 = a1[30:50] a3 = a1[55:79] print type(abc) print result if (result2 == "tamaki" and b01 >= 0.8): bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://imgur.com/Vsi1nLt.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction( uri='https://imgur.com/Vsi1nLt', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='926四葉環', weight='bold', size='xl'), # review # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='相近', color='#aaaaaa', size='sm', flex=1), TextComponent(text=result2, wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='數值', color='#aaaaaa', size='sm', flex=1), TextComponent( text=result, wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction( label='好微笑看看', uri= 'https://www.goodsmile.info/zh/product/7240/%E9%BB%8F%E5%9C%9F%E4%BA%BA+%E5%9B%9B%E8%91%89%E7%92%B0.html' ), ), # separator ]), ) message = FlexSendMessage(alt_text="感謝您的使用以下為你分析", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif (result2 == "sogo" and b01 >= 0.8): bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://imgur.com/oP3mQNC.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction( uri='https://imgur.com/oP3mQNC', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='905逢板壯五', weight='bold', size='xl'), # review # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='相近', color='#aaaaaa', size='sm', flex=1), TextComponent(text=result2, wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='數值', color='#aaaaaa', size='sm', flex=1), TextComponent( text=result, wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction( label='好微笑看看', uri= 'https://www.goodsmile.info/zh/product/7175/%E9%BB%8F%E5%9C%9F%E4%BA%BA+%E9%80%A2%E5%9D%82%E5%A3%AF%E4%BA%94.html' ), ), # separator ]), ) message = FlexSendMessage(alt_text="感謝您的使用以下為你分析", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif (result2 == "Kirby"): bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://imgur.com/ec1wSXs.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction( uri='https://imgur.com/ec1wSXs', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='544星之卡比', weight='bold', size='xl'), # review # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='相近', color='#aaaaaa', size='sm', flex=1), TextComponent(text=result2, wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='數值', color='#aaaaaa', size='sm', flex=1), TextComponent( text=result, wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction( label='好微笑看看', uri= 'https://www.goodsmile.info/zh/product/5207/%E9%BB%8F%E5%9C%9F%E4%BA%BA+%E5%8D%A1%E6%AF%94.html' ), ), # separator ]), ) message = FlexSendMessage(alt_text="感謝您的使用以下為你分析", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif (result2 == "kirbyPirate" and b01 >= 0.8): bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://imgur.com/Q032z3A.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction( uri='https://imgur.com/Q032z3A', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='此款有盜版嫌疑!!', weight='bold', size='xl'), # review # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='相近', color='#aaaaaa', size='sm', flex=1), TextComponent(text=result2, wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='數值', color='#aaaaaa', size='sm', flex=1), TextComponent( text=result, wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction( label='好微笑看看', uri= 'https://www.goodsmile.info/zh/product/7175/%E9%BB%8F%E5%9C%9F%E4%BA%BA+%E9%80%A2%E5%9D%82%E5%A3%AF%E4%BA%94.html' ), ), # separator ]), ) message = FlexSendMessage(alt_text="感謝您的使用以下為你分析", contents=bubble) line_bot_api.reply_message(event.reply_token, message) else: line_bot_api.reply_message(event.reply_token, [TextSendMessage(text='not find')])
def handle_text_message(event): text = event.message.text if text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + profile.status_message) ]) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text="Bot can't use profile API without user ID")) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn( thumbnail_image_url= 'https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/shop-icon.png', image_background_color='#ffffff', text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn( thumbnail_image_url= 'https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/shop-icon.png', image_background_color='#FFFFFF', text='hoge2', title='fuga2', actions=[ PostbackAction( label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage(alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction( label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url= 'https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_2_restaurant.png', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', spacing='md', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent( size='sm', url= 'https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png' ), IconComponent( size='sm', url= 'https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png' ), IconComponent( size='sm', url= 'https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png' ), IconComponent( size='sm', url= 'https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png' ), IconComponent( size='sm', url= 'https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png' ), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ]), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Place', color='#aaaaaa', size='sm', flex=1), TextComponent(text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Time', color='#aaaaaa', size='sm', flex=1), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='xxl'), # callAction ButtonComponent( style='primary', height='sm', # action=URIAction(label='CALL', uri='tel:000000'), color='#905c44', action=URIAction(label='Add to Cart', uri='https://linecorp.com')), # separator # SeparatorComponent(), # # websiteAction # ButtonComponent( # style='link', # height='sm', # action=URIAction(label='WEBSITE', uri="https://example.com") # ) ]), ) flex = [] flex.append(bubble) flex.append(bubble) a = CarouselContainer(type='carousel', contents=flex) message = FlexSendMessage(alt_text="hello", contents=a) line_bot_api.reply_message(event.reply_token, message) elif text == '/list_barang': req = requests.get( 'https://server-buychat.herokuapp.com/product_category') response = json.loads(req.text) items = [] for category in response['data']: postdata = { "action": "search_product", "product_category_id": category['Id'] } items.append( QuickReplyButton(action=PostbackAction( label=category['Product_category_name'], data=json.dumps(postdata)))) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Ada %s Jenis Barang Yang Tersedia' % (response['count']), quick_reply=QuickReply(items=items))) else: line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))
def handle_text_message(event): text = event.message.text if text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + profile.status_message) ] ) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't use profile API without user ID")) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage( alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage( alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage( alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label') ), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent(size='sm', url='https://example.com/gold_star.png'), IconComponent(size='sm', url='https://example.com/grey_star.png'), IconComponent(size='sm', url='https://example.com/gold_star.png'), IconComponent(size='sm', url='https://example.com/gold_star.png'), IconComponent(size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ] ), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Place', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5 ) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Time', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent( style='link', height='sm', action=URIAction(label='WEBSITE', uri="https://example.com") ) ] ), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message( event.reply_token, message ) elif text == 'quick_reply': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply( items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1") ), QuickReplyButton( action=MessageAction(label="label2", text="text2") ), QuickReplyButton( action=DatetimePickerAction(label="label3", data="data3", mode="date") ), QuickReplyButton( action=CameraAction(label="label4") ), QuickReplyButton( action=CameraRollAction(label="label5") ), QuickReplyButton( action=LocationAction(label="label6") ), ]))) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text=event.message.text))
def handle_text_message(event): text = (event.message.text).lower() if text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + profile.status_message), TextSendMessage(text='Picture: ' + profile.picture_url) ]) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text="Bot can't use profile API without user ID")) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'navigation': buttons_template = ButtonsTemplate( title='Navigation Bot vers.1', text='Hello, ada yang bisa saya bantu...?', actions=[ MessageAction(label='Info Grapari', text='cari info Grapari'), URIAction(label='About Dev.', uri='http://line.me/ti/p/~primaananda_') ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'cari info grapari' or text == 'cari grapari' or text == 'grapari': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='Daftar grapari wilayah Denpasar', title='Denpasar', actions=[ MessageAction(label='Grapari Teuku Umar', text='grapari teuku umar'), MessageAction(label='grapari renon', text='grapari renon') ]), CarouselColumn(text='Daftar grapari wilayah Bangli', title='Bangli', actions=[ PostbackAction( label='Status Grapari', data='tidak terdapat Grapari di Bangli', text=' '), MessageAction(label=' ', text=' ') ]), CarouselColumn(text='Daftar grapari wilayah Buleleng', title='Buleleng', actions=[ MessageAction(label='grapari Buleleng', text='grapari buleleng'), MessageAction(label=' ', text=' ') ]), CarouselColumn(text='Daftar grapari wilayah Gianyar', title='Gianyar', actions=[ MessageAction(label='grapari Gianyar', text='grapari gianyar'), MessageAction(label=' ', text=' ') ]), CarouselColumn(text='Daftar grapari wilayah Jembrana', title='Jembrana', actions=[ MessageAction(label='graPARI Negara', text='grapari negara'), MessageAction(label=' ', text=' ') ]), CarouselColumn(text='Daftar grapari wilayah Karangasem', title='Karangasem', actions=[ MessageAction(label='graPARI Karangasem', text='grapari karangasem'), MessageAction(label=' ', text=' ') ]), CarouselColumn(text='Daftar grapari wilayah Klungkung', title='Klungkung', actions=[ PostbackAction( label='Status Grapari', data='tidak terdapat Grapari di Klungkung', text=' '), MessageAction(label=' ', text=' ') ]), CarouselColumn(text='Daftar grapari wilayah Tabanan', title='Tabanan', actions=[ MessageAction(label='Grapari Tabanan', text='grapari tabanan'), MessageAction(label=' ', text=' ') ]), ]) template_message = TemplateSendMessage(alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'alamat grapari renon': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='My GraPARI Denpasar Renon', address= 'Jl. Raya Puputan No.33, Panjer, Denpasar Sel., Kota Denpasar, Bali 80234', latitude=-8.672262, longitude=115.228615) ]) elif text == 'alamat grapari teuku umar': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='My GraPARI Denpasar Teuku Umar', address= 'Jl. Teuku Umar No.6, Dauh Puri Klod, Denpasar Bar., Kota Denpasar, Bali 80114', latitude=-8.669041, longitude=115.214185) ]) elif text == 'alamat grapari buleleng': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='My GraPARI Buleleng', address= 'Jl. A. Yani No.72, Kaliuntu, Kec. Buleleng, Kabupaten Buleleng, Bali 81114', latitude=-8.111648, longitude=115.085734) ]) elif text == 'alamat grapari gianyar': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='My GraPARI Gianyar', address= 'Jl. A. Yani No.72, Kaliuntu, Kec. Buleleng, Kabupaten Buleleng, Bali 81114', latitude=-8.111648, longitude=115.085734) ]) elif text == 'alamat grapari mbg': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='My graPARI Mall Bali Galeria', address= 'JL. by Pass I Gusti Ngurah Rai , 80361 Denpasar , Bali, Kuta, Badung Regency, Bali 80361', latitude=-8.723316, longitude=115.184888) ]) elif text == 'alamat grapari karangasem': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='My graPARI Karangasem', address= 'Jl. PB. Sudirman 98 C, Amlapura, Kec. Karangasem, Kab. Karangasem, Bali 80811', latitude=-8.451645, longitude=115.603835) ]) elif text == 'alamat grapari negara': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='GraPARI Telkomsel Negara', address= 'Jl. Ngurah Rai, Pendem, Kec. Jembrana, Kabupaten Jembrana, Bali 82218', latitude=-8.360644, longitude=114.629995) ]) elif text == 'alamat grapari tabanan': line_bot_api.reply_message(event.reply_token, [ LocationSendMessage( title='GraPARI Telkomsel Tabanan', address= 'Jl. Dr. Ir. Soekarno, Banjar Anyar, Kediri, Kabupaten Tabanan, Bali 82121', latitude=-8.552806, longitude=115.135258) ]) elif text == 'grapari teuku umar': bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://i.imgur.com/G18bDUi.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction( uri='https://i.imgur.com/G18bDUi.jpg', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='My GraPARI Teuku Umar', weight='bold', size='xl'), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Alamat', color='#aaaaaa', size='sm', flex=3), TextComponent( text= 'Jl. Teuku Umar No.6, Dauh Puri Klod, Denpasar Bar., Kota Denpasar, Bali 80114', wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text="Waktu Buka", color='#aaaaaa', size='sm', flex=3), TextComponent( text= "Senin, 08.00–17.00\nSelasa, 08.00–17.00\nRabu, 08.00–17.00\nKamis, 08.00–17.00\nJumat, 08.00–17.00\nSabtu, 08.00–12.00\nMinggu, Tutup", wrap=True, color='#666666', size='sm', flex=5, ), ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='No. Telp', color='#aaaaaa', size='sm', flex=3), TextComponent( text="0811-3940-500", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='primary', color='#00b300', height='sm', action=URIAction(label='Call', uri='tel:08113940500'), ), # websiteAction ButtonComponent(style='secondary', height='sm', action=PostbackAction( label='Alamat GraPARI', data='alamat grapari teuku umar', text='alamat grapari teuku umar')) ]), ) message = FlexSendMessage(alt_text="grapari teuku umar", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), ]) template_message = TemplateSendMessage(alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction( label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ]), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Place', color='#aaaaaa', size='sm', flex=1), TextComponent(text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Time', color='#aaaaaa', size='sm', flex=1), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, alamatAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # alamatAction ButtonComponent(style='link', height='sm', action=URIAction( label='WEBSITE', uri="https://example.com")) ]), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif text == 'quick_reply': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply(items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1")), QuickReplyButton( action=MessageAction(label="label2", text="text2")), QuickReplyButton(action=DatetimePickerAction( label="label3", data="data3", mode="date")), QuickReplyButton(action=CameraAction(label="label4")), QuickReplyButton(action=CameraRollAction(label="label5")), QuickReplyButton(action=LocationAction(label="label6")), ]))) elif text == '.help': line_bot_api.reply_message( event.reply_token, TextSendMessage( text= 'Berikut merupakan command yang terdapat pada BOT ini: \n1. info_grapari\n2. profile\n3. bye\n4. confirm\n5. buttons\n6. carousel\n7 image\n8 image carousel\n9 imagemap\n10 flex\n11 quick_reply\n12 info\n13 about\n14 ip', quick_reply=QuickReply(items=[ QuickReplyButton( action=MessageAction(label="profile", text="profile")), QuickReplyButton( action=MessageAction(label="bye", text="bye")), QuickReplyButton( action=MessageAction(label="confirm", text="confirm")), QuickReplyButton( action=MessageAction(label="buttons", text="buttons")), QuickReplyButton(action=MessageAction(label="carousel", text="carousel")), QuickReplyButton( action=MessageAction(label="image", text="image")), QuickReplyButton(action=MessageAction( label="image carousel", text="image carousel")), QuickReplyButton(action=MessageAction(label="imagemap", text="imagemap")), QuickReplyButton( action=MessageAction(label="flex", text="flex")), QuickReplyButton(action=MessageAction(label="quick_reply", text="quick_reply")), QuickReplyButton( action=MessageAction(label="info", text="info")), QuickReplyButton( action=MessageAction(label="about", text="about")), QuickReplyButton( action=MessageAction(label="your ip", text="ip")) ]))) elif text == 'info': line_bot_api.reply_message(event.reply_token, TextSendMessage(text='')) elif text == 'about': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Ini merupakan bot yang masih dalam proses pengembangan.') ) elif text == 'ip': res = requests.get('https://ipinfo.io/') data = res.json() # your_ip = data['ip'] line_bot_api.reply_message( event.reply_token, TextSendMessage( text=('alamat IP : ' + data['ip'] + '\nhostname : ' + data['hostname'] + '\nKota : ' + data['city'] + '\nNegara : ' + data['country'] + '\nLokasi : ' + data['loc'] + '\nOrganisasi : ' + data['org']))) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Need help? click this button .help', quick_reply=QuickReply(items=[ QuickReplyButton( action=MessageAction(label="help", text=".help")) ])))
def get_products(event, category_id): req = requests.get( 'https://server-buychat.herokuapp.com/products?product_category_id=%s' % (category_id)) response = json.loads(req.text) products = response['data'] bubble_product = [] for product in products: print("merchant") print(product['Merchant']['Merchant_name']) bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url= 'https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_2_restaurant.png', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', spacing='md', contents=[ # title TextComponent(text=product['Product_name'], weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ # IconComponent(size='sm', url='https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png'), # IconComponent(size='sm', url='https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png'), # IconComponent(size='sm', url='https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png'), # IconComponent(size='sm', url='https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png'), # IconComponent(size='sm', url='https://cdn4.iconfinder.com/data/icons/small-n-flat/24/star-512.png'), TextComponent(text='%s' % (product['Product_sell_price']), size='sm', margin='sm', flex=0) ]), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ # TextComponent( # text='Place', # color='#aaaaaa', # size='sm', # flex=1 # ), TextComponent(text=product['Merchant'] ['Merchant_name'], wrap=True, color='#666666', weight='bold', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ # TextComponent( # text='Time', # color='#aaaaaa', # size='sm', # flex=1 # ), TextComponent( text="%s - %s" % (product['Merchant']['Merchant_open'], product['Merchant']['Merchant_close'] ), wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='xxl'), # callAction ButtonComponent( style='primary', height='sm', # action=URIAction(label='CALL', uri='tel:000000'), color='#26a65b', action=URIAction(label='Beli', uri='https://linecorp.com')), # separator # SeparatorComponent(), # # websiteAction # ButtonComponent( # style='link', # height='sm', # action=URIAction(label='WEBSITE', uri="https://example.com") # ) ]), ) bubble_product.append(bubble) contents_of_product = CarouselContainer(type='carousel', contents=bubble_product) message = FlexSendMessage(alt_text="hello", contents=contents_of_product) line_bot_api.reply_message(event.reply_token, message)
def shop_window(url, title, place, time, num, price, call, web, addr): postbackinfor = place + addr bubble = BubbleContainer( direction='ltr', hero=ImageComponent( # 图片的url url=url, size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text=title, weight='bold', size='xl'), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ # 商店地址 TextComponent(text='Place', color='#aaaaaa', size='sm', flex=1), TextComponent(text=place, wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ # 商店时间 TextComponent(text='Time', color='#aaaaaa', size='sm', flex=1), TextComponent( text=time, wrap=True, color='#666666', size='sm', flex=5, ), ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ # 口罩数量 TextComponent(text='Num of Masks(box):', color='#aaaaaa', size='sm', flex=8), TextComponent( text=num, wrap=True, color='#666666', size='sm', flex=5, ), ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ # 口罩价格 TextComponent(text='Price', color='#aaaaaa', size='sm', flex=1), TextComponent( text=price, wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri=call), ), SeparatorComponent(), ButtonComponent( height='sm', # 第一个是latitude, 第二个是longitude action=PostbackAction(label='Location', data=postbackinfor), ), # separator SeparatorComponent(), # websiteAction ButtonComponent(style='link', height='sm', action=URIAction(label='WEBSITE', uri=web)) ]), ) return bubble
def handle_message(event): text = event.message.text user_id = event.source.user_id print(user_id) print(text) if text == '自我介紹': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url='https://i.imgur.com/RXQnxHs.jpg', size='3xl', aspect_mode='cover', gravity='center', ), body=BoxComponent( layout='vertical', contents=[ SeparatorComponent(margin='xxl'), # title TextComponent( text='張書維 Vicharm Chang', weight='bold', size='xl', align='center', margin='lg'), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='程式語言', color='#aaaaaa', size='sm', flex=2 ), TextComponent( text='Python, C, C++', wrap=True, color='#666666', size='sm', flex=5 ) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='學籍', color='#aaaaaa', size='sm', flex=2 ), TextComponent( text="台大電機所計算機組,碩一", wrap=True, color='#666666', size='sm', flex=5, ), ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='畢業學校', color='#aaaaaa', size='sm', flex=2 ), TextComponent( text="成功大學資訊工程學系", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ), TextComponent( text='學習軟體程式8年 對軟體開發有極大興趣', color='#666666', size='sm', margin='lg' ), ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ SpacerComponent(size='sm'), ButtonComponent( style='primary', color='#6D5546', height='sm', action=MessageAction(label='查看專案', text='開發專案經驗'), ), ButtonComponent( style='primary', color='#6D5546', height='sm', action=MessageAction(label='自我推薦', text='自我推薦'), ) ] ), ) message = FlexSendMessage(alt_text="自我介紹", contents=bubble) line_bot_api.reply_message( event.reply_token, message ) elif text == '開發專案經驗': carousel = CarouselContainer( contents=[ BubbleContainer( direction='ltr', hero=ImageComponent( url='https://i.imgur.com/kj7UJcl.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', gravity='center' ), body=BoxComponent( margin='lg', layout='vertical', contents=[ # title TextComponent(text='Konma Bot', wrap=True, weight='bold', size='xxl', align='center'), # info BoxComponent( margin='lg', layout='vertical', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/YMxsgqo.png'), TextComponent( text='毛孩影片推播', color='#666666', size='sm', ), ] ), BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/gtkZuKm.png'), TextComponent( text='領養送養查詢', color='#666666', size='sm', ), ] ), BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/zALcrA5.png'), TextComponent( text='寵物知識問答', color='#666666', size='sm', ), ] ), BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/0lRYfH6.png'), TextComponent( text='飼主交友分享', color='#666666', size='sm', ), ] ), ] ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ SpacerComponent(size='sm'), ButtonComponent( style='link', height='sm', action=MessageAction( label='試玩影片推播功能', text='試玩Konma Bot'), ), ButtonComponent( style='link', height='sm', action=URIAction( label='查看github專案', uri='https://github.com/changsuwi/Konma-Chatbot'), ) ] ), ), BubbleContainer( direction='ltr', hero=ImageComponent( url='https://live.staticflickr.com/949/27294712367_63f3db8890_z_d.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', gravity='center' ), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Bon Voyage', wrap=True, weight='bold', size='xxl', align='center'), # info BoxComponent( margin='lg', layout='vertical', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/syxg5cD.png'), TextComponent( text='串接google map api 做資料視覺化', color='#666666', size='sm', flex=1 ), ] ), BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/Ye12a4V.png'), TextComponent( text='直觀查看熱門景點動態', color='#666666', size='sm', flex=1 ), ] ), BoxComponent( layout='baseline', spacing='sm', contents=[ IconComponent( url='https://i.imgur.com/spcJLhA.png'), TextComponent( text="上傳圖片,與其他使用者分享", wrap=True, color='#666666', size='sm', flex=1, ), ] ), ] ), ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ SpacerComponent(size='sm'), ButtonComponent( style='link', height='sm', action=URIAction( label='查看專案github', uri='https://github.com/changsuwi/Travel-Photography' ), ), ] ), ), BubbleContainer( direction='ltr', hero=ImageComponent( url='https://i.imgur.com/kj7UJcl.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', gravity='center' ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ ButtonComponent( style='link', height='sm', action=URIAction( label='查看其他github專案', uri='https://github.com/changsuwi'), ) ] ), ), ] ) message = FlexSendMessage(alt_text="開發專案經驗", contents=carousel) line_bot_api.reply_message( event.reply_token, message ) elif text == '自我推薦': bubble = BubbleContainer( body=BoxComponent( layout='vertical', contents=[ TextComponent( text='選擇我的原因', weight='bold', size='xxl', margin='md', align='center' ), SeparatorComponent(margin='xxl'), BoxComponent( layout='vertical', margin='xxl', spacing='sm', contents=[ TextComponent( text='1. Chatbot與其他軟體開發經驗豐富', margin='md', size='sm' ), TextComponent( text='2. 對於computer science學科相當熟悉', margin='md', size='sm' ), TextComponent( text='3. 學習速度快,可以快速進入狀況', margin='md', size='sm' ), TextComponent( text='4. 有團隊開發經驗,縮小溝通成本', margin='md', size='sm' ), ] ) ] ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ SpacerComponent(size='sm'), ButtonComponent( style='primary', color='#6D5546', height='sm', action=MessageAction( label='查看自我介紹', text='自我介紹' ), ), ButtonComponent( style='primary', color='#6D5546', height='sm', action=MessageAction( label='查看開發專案', text='開發專案經驗' ), ), ] ), ) message = FlexSendMessage(alt_text="自我推薦", contents=bubble) line_bot_api.reply_message( event.reply_token, message) elif text == '試玩Konma Bot': message = FlexSendMessage(alt_text="試玩Konma Bot", contents=get_video()) line_bot_api.reply_message( event.reply_token, message ) message = TextSendMessage( text='想看更多', quick_reply=QuickReply( items=[ QuickReplyButton( action=MessageAction( label='更多寵物影片', text='試玩Konma Bot' ) ), QuickReplyButton( action=MessageAction( label='自我推薦', text='自我推薦' ) ), QuickReplyButton( action=MessageAction( label='開發專案經驗', text='開發專案經驗' ) ), ] ) ) line_bot_api.push_message( user_id, message ) else: message = TextSendMessage(text='請點選主選單,點擊有興趣的項目,謝謝') line_bot_api.reply_message( event.reply_token, message )
def handle_text_message(event): text = event.message.text textlow = event.message.text.lower() #EDIT START HERE #YOU CAN EDIT WITH ELIF if ("!menu" in text) or ("menu" in textlow): carousel_template = CarouselTemplate(columns=[ CarouselColumn( text='Konsultasi dengan admin kami', title='Konsultasi', thumbnail_image_url= 'https://cdn2.tstatic.net/jateng/foto/bank/images/tugu-muda-semarang-ok.jpg', actions=[ URIAction(label='Line@', uri='https://line.me/R/ti/p/%40nut4837y'), ]), CarouselColumn( text='Informasi mengenai Alfalink', title='Info', thumbnail_image_url= 'https://cdn2.tstatic.net/jateng/foto/bank/images/tugu-muda-semarang-ok.jpg', actions=[ MessageAction(label='Detail', text='Info'), ]) ]) template_message = TemplateSendMessage(alt_text='Info', template=carousel_template) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='Info'), template_message]) elif ("!info" in text) or ("info" in textlow): carousel_template = CarouselTemplate(columns=[ CarouselColumn( text='Information About Alfalink', title='Informasi Alfalink', thumbnail_image_url= 'https://cdn2.tstatic.net/jateng/foto/bank/images/tugu-muda-semarang-ok.jpg', actions=[ URIAction(label='About Us', uri='http://alfalink.net/alfalink-profile/'), URIAction(label='Career', uri='http://alfalink.net/career/'), ]) ]) template_message = TemplateSendMessage(alt_text='Menu', template=carousel_template) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text="Info Alfalink"), template_message]) ##Apabila user memilih pilihan tidak pada bantuan elif ("tidak" in textlow): if isinstance(event.source, SourceUser): line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text="Terimakasih sampai jumpa kembali"), ]) #END OF EDIT ########################################################### #DEFAULT CODE elif text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + str(profile.status_message)) ]) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text="Bot can't use profile API without user ID")) elif text == 'quota': quota = line_bot_api.get_message_quota() line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='type: ' + quota.type), TextSendMessage(text='value: ' + str(quota.value)) ]) elif text == 'quota_consumption': quota_consumption = line_bot_api.get_message_quota_consumption() line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='total usage: ' + str(quota_consumption.total_usage)), ]) elif text == 'push': line_bot_api.push_message(event.source.user_id, [ TextSendMessage(text='PUSH!'), ]) elif text == 'multicast': line_bot_api.multicast([event.source.user_id], [ TextSendMessage(text='THIS IS A MULTICAST MESSAGE'), ]) elif text == 'broadcast': line_bot_api.broadcast([ TextSendMessage(text='THIS IS A BROADCAST MESSAGE'), ]) elif text.startswith('broadcast '): # broadcast 20190505 date = text.split(' ')[1] print("Getting broadcast result: " + date) result = line_bot_api.get_message_delivery_broadcast(date) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Number of sent broadcast messages: ' + date), TextSendMessage(text='status: ' + str(result.status)), TextSendMessage(text='success: ' + str(result.success)), ]) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'image': url = request.url_root + '/static/logo.png' app.logger.info("url=" + url) line_bot_api.reply_message(event.reply_token, ImageSendMessage(url, url)) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage(alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction( label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ]), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Place', color='#aaaaaa', size='sm', flex=1), TextComponent(text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Time', color='#aaaaaa', size='sm', flex=1), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent(style='link', height='sm', action=URIAction( label='WEBSITE', uri="https://example.com")) ]), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif text == 'quick_reply': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply(items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1")), QuickReplyButton( action=MessageAction(label="label2", text="text2")), QuickReplyButton(action=DatetimePickerAction( label="label3", data="data3", mode="date")), QuickReplyButton(action=CameraAction(label="label4")), QuickReplyButton(action=CameraRollAction(label="label5")), QuickReplyButton(action=LocationAction(label="label6")), ]))) elif text == 'link_token' and isinstance(event.source, SourceUser): link_token_response = line_bot_api.issue_link_token( event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='link_token: ' + link_token_response.link_token) ]) elif text == 'insight_message_delivery': today = datetime.date.today().strftime("%Y%m%d") response = line_bot_api.get_insight_message_delivery(today) if response.status == 'ready': messages = [ TextSendMessage(text='broadcast: ' + str(response.broadcast)), TextSendMessage(text='targeting: ' + str(response.targeting)), ] else: messages = [TextSendMessage(text='status: ' + response.status)] line_bot_api.reply_message(event.reply_token, messages) elif text == 'insight_followers': today = datetime.date.today().strftime("%Y%m%d") response = line_bot_api.get_insight_followers(today) if response.status == 'ready': messages = [ TextSendMessage(text='followers: ' + str(response.followers)), TextSendMessage(text='targetedReaches: ' + str(response.targeted_reaches)), TextSendMessage(text='blocks: ' + str(response.blocks)), ] else: messages = [TextSendMessage(text='status: ' + response.status)] line_bot_api.reply_message(event.reply_token, messages) elif text == 'insight_demographic': response = line_bot_api.get_insight_demographic() if response.available: messages = [ "{gender}: {percentage}".format(gender=it.gender, percentage=it.percentage) for it in response.genders ] else: messages = [TextSendMessage(text='available: false')] line_bot_api.reply_message(event.reply_token, messages) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Ketik "Menu" untuk melihat menu.', quick_reply=QuickReply(items=[ QuickReplyButton( action=MessageAction(label="Menu", text="menu")), ])))
def handle_text_message(event): text = event.message.text user_dict = get_user_identity(event.source.user_id) if user_dict['time'] > 10800: change_situation(event.source.user_id, 0) user_dict['situation'] = 0 if text == 'getid': if isinstance(event.source, SourceUser): user_dict = get_user_identity(event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='id: ' + str(user_dict['id'])), TextSendMessage(text='situation: ' + str(user_dict['situation'])), TextSendMessage(text='status: ' + str(user_dict['status'])), ]) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Cannot connect to the server")) elif user_dict['situation'] == 0: profile = line_bot_api.get_profile(event.source.user_id) change_situation(event.source.user_id, 1) temp_text = str(profile.display_name ) + 'さん初めまして、私はお料理お姉さんよ。もしかして、今晩のメニューに悩んでいるんじゃない?' confirm_template = ConfirmTemplate(text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_dict['situation'] == 1 and text == 'いいえ': change_situation(event.source.user_id, 0) line_bot_api.reply_message( event.reply_token, TextSendMessage(text="あら、そうなの。また料理に困ったら声をかけてね!")) elif user_dict['situation'] == 1 and text == 'はい': change_situation(event.source.user_id, 3) temp_text = 'じゃあ私があなたの気分からお料理を提案してあげるわ!今日はお肉の気分?' confirm_template = ConfirmTemplate(text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_dict['situation'] == 3 and text == 'いいえ': change_situation(event.source.user_id, 4) temp_text = 'じゃあ海鮮の気分?' confirm_template = ConfirmTemplate(text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_dict['situation'] == 3 and text == 'はい': change_situation(event.source.user_id, 5) temp_text = 'ちょっと凝った料理に挑戦してみる?' confirm_template = ConfirmTemplate(text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_dict['situation'] == 4 and text == 'いいえ': change_situation(event.source.user_id, 12) temp_text = 'あなたは今日はベジタリアンなのね!じゃあこのお肉を使わない豆腐ハンバーグはどうかな?' recipe_dict = get_recipe(6) img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Tofu_Hamburg_steak_20141107.jpg/200px-Tofu_Hamburg_steak_20141107.jpg' buttons_template = ButtonsTemplate(thumbnail_image_url=img_url, title='豆腐ハンバーグ', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 4 and text == 'はい': change_situation(event.source.user_id, 13) temp_text = 'じゃあこの「海鮮アボカド」を作ってみない?' recipe_dict = get_recipe(7) img_url = recipe_dict['img_url'] buttons_template = ButtonsTemplate(thumbnail_image_url=img_url, title='海鮮アボカド', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 5 and text == 'いいえ': change_situation(event.source.user_id, 6) temp_text = 'がっつりしたものが食べたいの?' confirm_template = ConfirmTemplate(text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_dict['situation'] == 5 and text == 'はい': change_situation(event.source.user_id, 8) temp_text = 'じゃあこの「ビーフウェリントン」を作ってみない? とっても豪華なイギリスの肉料理よ!' buttons_template = ButtonsTemplate( thumbnail_image_url= 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Beef_Wellington_-_Crosscut.jpg/800px-Beef_Wellington_-_Crosscut.jpg', title='ビーフウェリントン', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 6 and text == 'いいえ': change_situation(event.source.user_id, 9) temp_text = 'じゃあこの「タコライス」はいかが?' buttons_template = ButtonsTemplate( thumbnail_image_url= 'https://upload.wikimedia.org/wikipedia/commons/4/45/Taco_Rice1.JPG', title='タコライス', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 6 and text == 'はい': change_situation(event.source.user_id, 7) temp_text = 'じゃあこの「ビーフステーキ」がいいんじゃないかしら?' buttons_template = ButtonsTemplate( thumbnail_image_url= 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/150418_Awaji_beef_at_Sumoto_Hyogo_pref_Japan02s5.jpg/800px-150418_Awaji_beef_at_Sumoto_Hyogo_pref_Japan02s5.jpg', title='ビーフステーキ', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 7 and text == 'いいえ': change_situation(event.source.user_id, 0) line_bot_api.reply_message( event.reply_token, TextSendMessage(text="う〜ん、難しい子ねえ。また気が向いたら話しかけるのよ!")) elif user_dict['situation'] == 7 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(4) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage( text="やっぱりステーキだよね!じゃあこのレシピにしたがって作ってみるのよ!できたら写真を送ってね〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 8 and text == 'いいえ': change_situation(event.source.user_id, 0) line_bot_api.reply_message(event.reply_token, TextSendMessage(text="あら、残念。じゃあまた今度ね!")) elif user_dict['situation'] == 8 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(2) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text="やった〜!じゃあこのレシピにしたがって作ってみるのよ!できたら写真を送ってね〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 9 and text == 'いいえ': change_situation(event.source.user_id, 10) temp_text = 'それなら「青椒肉絲」ならどう?' buttons_template = ButtonsTemplate( thumbnail_image_url= 'https://upload.wikimedia.org/wikipedia/commons/9/9d/Pepper_steak.jpg', title='青椒肉絲', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 9 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(3) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage( text="そうこなくっちゃ!じゃあこのレシピにしたがって作ってみるのよ!できたら写真を送ってね〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 10 and text == 'いいえ': change_situation(event.source.user_id, 0) line_bot_api.reply_message( event.reply_token, TextSendMessage(text="う〜ん、難しい子ねえ。また気が向いたら話しかけるのよ!")) elif user_dict['situation'] == 10 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(5) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage( text="今日は中華に挑戦よ!じゃあこのレシピにしたがって作ってみるのよ!できたら写真を送ってね〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 12 and text == 'いいえ': change_situation(event.source.user_id, 14) temp_text = 'じゃあこのナスとチーズのベジタリアン料理でどうだ〜!!' recipe_dict = get_recipe(8) img_url = recipe_dict['img_url'] buttons_template = ButtonsTemplate(thumbnail_image_url=img_url, title='ナスとチーズのベジタリアン', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 12 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(6) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage( text="そうこなくっちゃ! じゃあこのレシピにしたがって作ってみるのよ。完成したら写真を送ってね〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 13 and text == 'いいえ': change_situation(event.source.user_id, 15) temp_text = 'じゃあこの「ガーリックシュリンプ」ならどうだ!!' recipe_dict = get_recipe(9) img_url = recipe_dict['img_url'] buttons_template = ButtonsTemplate(thumbnail_image_url=img_url, title='ガーリックシュリンプ', text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, [ template_message, ]) elif user_dict['situation'] == 13 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(7) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage( text="そうこなくっちゃ!じゃあこのレシピにしたがって作ってみるのよ!完成したら写真を送ってね〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 14 and text == 'いいえ': change_situation(event.source.user_id, 0) line_bot_api.reply_message( event.reply_token, TextSendMessage(text="もう! 難しいんだから! 勝手にしなさい(≧ヘ≦ )プイッ!!")) elif user_dict['situation'] == 14 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(8) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text="じゃあこのレシピにしたがって作ってね!完成したら写真を送るのよ〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 15 and text == 'いいえ': change_situation(event.source.user_id, 0) line_bot_api.reply_message( event.reply_token, TextSendMessage(text="もう! 難しいんだから! 勝手にしなさい(≧ヘ≦ )プイッ!!")) elif user_dict['situation'] == 15 and text == 'はい': change_situation(event.source.user_id, 11) recipe_dict = get_recipe(9) temp_text = recipe_dict['recipe_text'] line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text="じゃあこのレシピにしたがって作ってね!完成したら写真を送るのよ〜"), TextSendMessage(text=temp_text), ]) elif user_dict['situation'] == 11: temp_text = 'できたら写真を送ってね〜' line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text=temp_text), ]) elif user_dict['situation'] >= 1 and user_dict['situation'] <= 15: temp_text = '「はい」か「いいえ」で答えてね!' confirm_template = ConfirmTemplate(text=temp_text, actions=[ MessageAction(label='はい', text='はい'), MessageAction(label='いいえ', text='いいえ'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + str(profile.status_message)) ]) else: line_bot_api.reply_message( event.reply_token, TextSendMessage( text="Bot can't use profile API without user ID")) elif text == 'quota': quota = line_bot_api.get_message_quota() line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='type: ' + quota.type), TextSendMessage(text='value: ' + str(quota.value)) ]) elif text == 'quota_consumption': quota_consumption = line_bot_api.get_message_quota_consumption() line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='total usage: ' + str(quota_consumption.total_usage)), ]) elif text == 'push': line_bot_api.push_message(event.source.user_id, [ TextSendMessage(text='PUSH!'), ]) elif text == 'multicast': line_bot_api.multicast([event.source.user_id], [ TextSendMessage(text='THIS IS A MULTICAST MESSAGE'), ]) elif text == 'broadcast': line_bot_api.broadcast([ TextSendMessage(text='THIS IS A BROADCAST MESSAGE'), ]) elif text.startswith('broadcast '): # broadcast 20190505 date = text.split(' ')[1] print("Getting broadcast result: " + date) result = line_bot_api.get_message_delivery_broadcast(date) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='Number of sent broadcast messages: ' + date), TextSendMessage(text='status: ' + str(result.status)), TextSendMessage(text='success: ' + str(result.success)), ]) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'image': url = request.url_root + '/static/logo.png' app.logger.info("url=" + url) line_bot_api.reply_message(event.reply_token, ImageSendMessage(url, url)) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage(alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage(alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage(alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn( image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction( label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent(url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label')), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/gold_star.png'), IconComponent( size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ]), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Place', color='#aaaaaa', size='sm', flex=1), TextComponent(text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent(text='Time', color='#aaaaaa', size='sm', flex=1), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent(style='link', height='sm', action=URIAction( label='WEBSITE', uri="https://example.com")) ]), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message(event.reply_token, message) elif text == 'flex_update_1': bubble_string = """ { "type": "bubble", "body": { "type": "box", "layout": "vertical", "contents": [ { "type": "image", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip3.jpg", "position": "relative", "size": "full", "aspectMode": "cover", "aspectRatio": "1:1", "gravity": "center" }, { "type": "box", "layout": "horizontal", "contents": [ { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": "Brown Hotel", "weight": "bold", "size": "xl", "color": "#ffffff" }, { "type": "box", "layout": "baseline", "margin": "md", "contents": [ { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png" }, { "type": "icon", "size": "sm", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png" }, { "type": "text", "text": "4.0", "size": "sm", "color": "#d6d6d6", "margin": "md", "flex": 0 } ] } ] }, { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": "¥62,000", "color": "#a9a9a9", "decoration": "line-through", "align": "end" }, { "type": "text", "text": "¥42,000", "color": "#ebebeb", "size": "xl", "align": "end" } ] } ], "position": "absolute", "offsetBottom": "0px", "offsetStart": "0px", "offsetEnd": "0px", "backgroundColor": "#00000099", "paddingAll": "20px" }, { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": "SALE", "color": "#ffffff" } ], "position": "absolute", "backgroundColor": "#ff2600", "cornerRadius": "20px", "paddingAll": "5px", "offsetTop": "10px", "offsetEnd": "10px", "paddingStart": "10px", "paddingEnd": "10px" } ], "paddingAll": "0px" } } """ message = FlexSendMessage(alt_text="hello", contents=json.loads(bubble_string)) line_bot_api.reply_message(event.reply_token, message) elif text == 'quick_reply': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply(items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1")), QuickReplyButton( action=MessageAction(label="label2", text="text2")), QuickReplyButton(action=DatetimePickerAction( label="label3", data="data3", mode="date")), QuickReplyButton(action=CameraAction(label="label4")), QuickReplyButton(action=CameraRollAction(label="label5")), QuickReplyButton(action=LocationAction(label="label6")), ]))) elif text == 'link_token' and isinstance(event.source, SourceUser): link_token_response = line_bot_api.issue_link_token( event.source.user_id) line_bot_api.reply_message(event.reply_token, [ TextSendMessage(text='link_token: ' + link_token_response.link_token) ]) elif text == 'insight_message_delivery': today = datetime.date.today().strftime("%Y%m%d") response = line_bot_api.get_insight_message_delivery(today) if response.status == 'ready': messages = [ TextSendMessage(text='broadcast: ' + str(response.broadcast)), TextSendMessage(text='targeting: ' + str(response.targeting)), ] else: messages = [TextSendMessage(text='status: ' + response.status)] line_bot_api.reply_message(event.reply_token, messages) elif text == 'insight_followers': today = datetime.date.today().strftime("%Y%m%d") response = line_bot_api.get_insight_followers(today) if response.status == 'ready': messages = [ TextSendMessage(text='followers: ' + str(response.followers)), TextSendMessage(text='targetedReaches: ' + str(response.targeted_reaches)), TextSendMessage(text='blocks: ' + str(response.blocks)), ] else: messages = [TextSendMessage(text='status: ' + response.status)] line_bot_api.reply_message(event.reply_token, messages) elif text == 'insight_demographic': response = line_bot_api.get_insight_demographic() if response.available: messages = [ "{gender}: {percentage}".format(gender=it.gender, percentage=it.percentage) for it in response.genders ] else: messages = [TextSendMessage(text='available: false')] line_bot_api.reply_message(event.reply_token, messages) else: line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))
def history_flexMessage(userid,date): uri = host + "/nutritionweb/history/" + userid+"/look/0" results = userFood.objects.filter(userId=userid,date=date) length=len(results) def item_loop(length): item_list=list() iniitem = BoxComponent( background_color="#EA8244", layout='baseline', contents=[ TextComponent( type="text", text="您於今日還沒有紀錄喔!!!!", color="#FFFFFF", size="xl", wrap=True, align="center", ), ] ) if length == 0: item_list.append(iniitem) elif length>10: item = BoxComponent( background_color="#EA8244", layout='baseline', contents=[ TextComponent( type="text", text="您紀錄太多筆無法喔,可以點擊下方按鈕查看紀錄", color="#FFFFFF", size="xl", wrap=True, align="center", ), ] ) else: for result in results: item = BoxComponent( background_color="#EA8244", layout='baseline', contents=[ TextComponent( type="text", text=str(result.food_name), color="#FFFFFF", size="xs", wrap=True, align="center", ), TextComponent( type="text", text=str(result.food_quantity), color="#FFFFFF", size="xs", wrap=True, align="center", ), TextComponent( type="text", text=str(result.food_power), color="#FFFFFF", size="xs", wrap=True, align="center", ), TextComponent( type="text", text=str(result.food_protein), color="#FFFFFF", size="xs", wrap=True, align="center", ), TextComponent( type="text", text=str(result.food_carbohydrate), color="#FFFFFF", size="xs", wrap=True, align="center", ), TextComponent( type="text", text=str(result.food_fat), color="#FFFFFF", size="xs", wrap=True, align="center", ), ] ) item_list.append(item) return item_list temp1 = item_loop(length) i=0 temp=[] while i < 11: if i<len(temp1): temp.append(temp1[i]) else: temp.append(BoxComponent(background_color="#EA8244",layout='baseline',contents=[FillerComponent(flex=0)])) i+=1 container = BubbleContainer( size='giga', body=BoxComponent( layout='vertical', background_color="#2e2e2e", size='sm', contents=[ BoxComponent( layout='vertical', background_color="#2e2e2e", contents=[ TextComponent( text='今日飲食紀錄🍽', weight="bold", size="xl", style="normal", decoration="none", gravity="center", align="center", color="#ffffff", offset_bottom="1.25%", ), BoxComponent( layout='baseline', background_color="#EA8244", contents=[ TextComponent( type="text", text="食物", size="sm", align="center", weight="bold", ), TextComponent( type='text', text="數量", wrap=True, size="sm", align="center", weight="bold", ), TextComponent( type='text', text="熱量", wrap=True, size="sm", align="center", weight="bold", ), TextComponent( type='text', text="蛋白質", wrap=True, size="sm", align="center", weight="bold", ), TextComponent( type='text', text="碳水", wrap=True, size="sm", align="center", weight="bold", ), TextComponent( type='text', text="脂肪", wrap=True, size="sm", align="center", weight="bold", ) ] ), temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9], temp[10], ] ), ], ), footer=BoxComponent( background_color="#2e2e2e", layout='vertical', spacing='sm', contents=[ ButtonComponent( style="primary", height="sm", action=URIAction(label="點我查看更多紀錄👀", uri=uri), color="#EA8244", gravity="center", ), SpacerComponent(size='sm'), ], flex=0, ) ) return FlexSendMessage(alt_text="今日飲食紀錄", contents=container)
def test_spacer_component(self): arg = {'size': 'md'} self.assertEqual(self.serialize_as_dict(arg, type=self.SPACER), SpacerComponent(**arg).as_json_dict())
def skill(self): bubble = BubbleContainer( body = BoxComponent( layout = 'vertical', contents = [ # header TextComponent(text="SAM's SKIL", weight='bold', size='sm', color='#1DB446'), TextComponent(text='Programming Skill', weight='bold', size='xxl', margin='md'), SeparatorComponent(margin='xxl'), # Body BoxComponent( layout = 'vertical', margin ='md', spacing = 'sm', contents = [ # Backend BoxComponent( layout='baseline', spacing='md', contents=[ IconComponent(url='https://i.imgur.com/8Uxdkpn.png', size='md'), TextComponent(text='Backend', size='lg', color='#7b7b7b') ] ), # node go BoxComponent( layout = 'horizontal', spacing = 'md', contents = [ TextComponent(text='Node.js - express', size='sm', color='#111111'), TextComponent(text='Golang - gin', size='sm', color='#111111'), ] ), # Mobile BoxComponent( layout='baseline', spacing='md', margin='xl', contents=[ IconComponent(url='https://i.imgur.com/0hOfB2w.png', size='md'), TextComponent(text='Mobile', size='lg', color='#7b7b7b') ] ), # swift BoxComponent( layout = 'horizontal', spacing = 'md', contents = [ TextComponent(text='Swift', size='sm', color='#111111'), ] ), # Others BoxComponent( layout='baseline', spacing='md', margin='xl', contents=[ IconComponent(url='https://i.imgur.com/etmNxkf.png', size='md'), TextComponent(text='Others', size='lg', color='#7b7b7b') ] ), # py c# java html/css git docker BoxComponent( layout = 'horizontal', spacing = 'md', contents = [ TextComponent(text='Python', size='sm', color='#111111'), TextComponent(text='C#', size='sm', color='#111111'), TextComponent(text='Java', size='sm', color='#111111') ] ), BoxComponent( layout = 'horizontal', spacing = 'md', contents = [ TextComponent(text='HTML/CSS', size='sm', color='#111111'), TextComponent(text='Git', size='sm', color='#111111'), TextComponent(text='Docker', size='sm', color='#111111') ] ), # DB BoxComponent( layout='baseline', spacing='md', margin='xl', contents=[ IconComponent(url='https://i.imgur.com/DaVwYMh.png', size='md'), TextComponent(text='Database', size='lg', color='#7b7b7b') ] ), # mysql ms oracle pg redis mongo BoxComponent( layout = 'horizontal', spacing = 'md', contents = [ TextComponent(text='MySQL', size='sm', color='#111111'), TextComponent(text='MSSQL', size='sm', color='#111111'), TextComponent(text='Oracle', size='sm', color='#111111') ] ), BoxComponent( layout = 'horizontal', spacing = 'md', contents = [ TextComponent(text='PostgreSQL', size='sm', color='#111111'), TextComponent(text='Redis', size='sm', color='#111111'), TextComponent(text='Mongo', size='sm', color='#111111') ] ), SeparatorComponent(margin='xxl'), # footer BoxComponent( layout = 'vertical', spacing = 'md', contents = [ SpacerComponent(size='md'), ButtonComponent( style='primary', height='sm', color='#00BE00', action=MessageAction(label="SAM's Portfolio", text='你有什麼作品?'), ), ButtonComponent( style='secondary', height='sm', action=URIAction(label="SAM's GitHub", uri='line://app/1570355825-rLKWJZK6'), ) ] ) ] ) ] ) ) message = FlexSendMessage(alt_text="SAM's Skill", contents=bubble) return message
def handle_text_message(event): text = event.message.text if text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + str(profile.status_message)) ] ) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't use profile API without user ID")) elif text == 'quota': quota = line_bot_api.get_message_quota() line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='type: ' + quota.type), TextSendMessage(text='value: ' + str(quota.value)) ] ) elif text == 'quota_consumption': quota_consumption = line_bot_api.get_message_quota_consumption() line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='total usage: ' + str(quota_consumption.total_usage)), ] ) elif text == 'push': line_bot_api.push_message( event.source.user_id, [ TextSendMessage(text='PUSH!'), ] ) elif text == 'multicast': line_bot_api.multicast( [event.source.user_id], [ TextSendMessage(text='THIS IS A MULTICAST MESSAGE'), ] ) elif text == 'broadcast': line_bot_api.broadcast( [ TextSendMessage(text='THIS IS A BROADCAST MESSAGE'), ] ) elif text.startswith('broadcast '): # broadcast 20190505 date = text.split(' ')[1] print("Getting broadcast result: " + date) result = line_bot_api.get_message_delivery_broadcast(date) line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='Number of sent broadcast messages: ' + date), TextSendMessage(text='status: ' + str(result.status)), TextSendMessage(text='success: ' + str(result.success)), ] ) elif text == 'bye': if isinstance(event.source, SourceGroup): line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_group(event.source.group_id) elif isinstance(event.source, SourceRoom): line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Leaving group')) line_bot_api.leave_room(event.source.room_id) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text="Bot can't leave from 1:1 chat")) elif text == 'image': url = request.url_root + '/static/logo.png' app.logger.info("url=" + url) line_bot_api.reply_message( event.reply_token, ImageSendMessage(url, url) ) elif text == 'confirm': confirm_template = ConfirmTemplate(text='Do it?', actions=[ MessageAction(label='Yes', text='Yes!'), MessageAction(label='No', text='No!'), ]) template_message = TemplateSendMessage( alt_text='Confirm alt text', template=confirm_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) template_message = TemplateSendMessage( alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'carousel': carousel_template = CarouselTemplate(columns=[ CarouselColumn(text='hoge1', title='fuga1', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping') ]), CarouselColumn(text='hoge2', title='fuga2', actions=[ PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]), ]) template_message = TemplateSendMessage( alt_text='Carousel alt text', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'image_carousel': image_carousel_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='date', data='date_postback', mode='date')) ]) template_message = TemplateSendMessage( alt_text='ImageCarousel alt text', template=image_carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif text == 'imagemap': pass elif text == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url='https://example.com/cafe.jpg', size='full', aspect_ratio='20:13', aspect_mode='cover', action=URIAction(uri='http://example.com', label='label') ), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent(size='sm', url='https://example.com/gold_star.png'), IconComponent(size='sm', url='https://example.com/grey_star.png'), IconComponent(size='sm', url='https://example.com/gold_star.png'), IconComponent(size='sm', url='https://example.com/gold_star.png'), IconComponent(size='sm', url='https://example.com/grey_star.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ] ), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Place', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5 ) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Time', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent( style='link', height='sm', action=URIAction(label='WEBSITE', uri="https://example.com") ) ] ), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message( event.reply_token, message ) elif text == 'quick_reply': line_bot_api.reply_message( event.reply_token, TextSendMessage( text='Quick reply', quick_reply=QuickReply( items=[ QuickReplyButton( action=PostbackAction(label="label1", data="data1") ), QuickReplyButton( action=MessageAction(label="label2", text="text2") ), QuickReplyButton( action=DatetimePickerAction(label="label3", data="data3", mode="date") ), QuickReplyButton( action=CameraAction(label="label4") ), QuickReplyButton( action=CameraRollAction(label="label5") ), QuickReplyButton( action=LocationAction(label="label6") ), ]))) elif text == 'link_token' and isinstance(event.source, SourceUser): link_token_response = line_bot_api.issue_link_token(event.source.user_id) line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='link_token: ' + link_token_response.link_token) ] ) else: line_bot_api.reply_message( event.reply_token, TextSendMessage(text=event.message.text))
def message_text(event): # check bot prefix if event.message.text.startswith(BOT_PREFIX): # seperate message contents as command and arguments message_body = event.message.text.strip()[1:].split() command = message_body[0] if(len(message_body) >= 2): arguments_list = message_body[1:] arguments_string = ' '.join(arguments_list) else: arguments_list = [] arguments_string = '' # echo command: reply arguments to user if command == 'echo': if(arguments_string != ''): line_bot_api.reply_message( event.reply_token, TextSendMessage(text=arguments_string) ) elif command == 'flex': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url='https://img.wongnai.com/p/1920x0/2017/12/30/19f2934940cf47669b2d1336feea0b97.jpg', size='full', aspect_ratio='4:3', aspect_mode='cover', action=URIAction(uri='https://github.com/miner46er/python-line-bot-sparta', label='label') ), body=BoxComponent( layout='vertical', contents=[ # title TextComponent(text='Brown Cafe', weight='bold', size='xl'), # review BoxComponent( layout='baseline', margin='md', contents=[ IconComponent(size='sm', url='https://cdn2.iconfinder.com/data/icons/default-1/100/.svg-4-512.png'), IconComponent(size='sm', url='https://freeiconshop.com/wp-content/uploads/edd/star-curved-outline.png'), IconComponent(size='sm', url='https://cdn2.iconfinder.com/data/icons/default-1/100/.svg-4-512.png'), IconComponent(size='sm', url='https://cdn2.iconfinder.com/data/icons/default-1/100/.svg-4-512.png'), IconComponent(size='sm', url='https://freeiconshop.com/wp-content/uploads/edd/star-curved-outline.png'), TextComponent(text='4.0', size='sm', color='#999999', margin='md', flex=0) ] ), # info BoxComponent( layout='vertical', margin='lg', spacing='sm', contents=[ BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Place', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text='Shinjuku, Tokyo', wrap=True, color='#666666', size='sm', flex=5 ) ], ), BoxComponent( layout='baseline', spacing='sm', contents=[ TextComponent( text='Time', color='#aaaaaa', size='sm', flex=1 ), TextComponent( text="10:00 - 23:00", wrap=True, color='#666666', size='sm', flex=5, ), ], ), ], ) ], ), footer=BoxComponent( layout='vertical', spacing='sm', contents=[ # callAction, separator, websiteAction SpacerComponent(size='sm'), # callAction ButtonComponent( style='link', height='sm', action=URIAction(label='CALL', uri='tel:000000'), ), # separator SeparatorComponent(), # websiteAction ButtonComponent( style='link', height='sm', action=URIAction(label='WEBSITE', uri="https://github.com/miner46er/python-line-bot-sparta") ) ] ), ) message = FlexSendMessage(alt_text="hello", contents=bubble) line_bot_api.reply_message( event.reply_token, message ) elif command == 'buttons': buttons_template = ButtonsTemplate( title='My buttons sample', text='Hello, my buttons', actions=[ URIAction(label='Go to line.me', uri='https://line.me'), PostbackAction(label='ping', data='ping'), PostbackAction(label='ping with text', data='ping', text='ping'), MessageAction(label='Translate Rice', text='米') ]) menu_pesan = TemplateSendMessage( alt_text='Buttons alt text', template=buttons_template) line_bot_api.reply_message(event.reply_token, menu_pesan) elif command == 'pesan': order_memo = BOT_PREFIX + command + ' ' + arguments_string if len(arguments_list) == 0: pilihan_menu = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Nasi Putih', text=BOT_PREFIX + command + ' putih') ), ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Nasi Umami', text=BOT_PREFIX + command + ' umami') ) ]) menu_pesan = TemplateSendMessage( alt_text='Menu pesanan', template=pilihan_menu) line_bot_api.reply_message(event.reply_token, menu_pesan) elif len(arguments_list) == 1: if RICE_TYPE.count(arguments_list[0]) == 1: pilihan_menu = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Ayam', text=order_memo + ' ayam') ), ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Cumi', text=order_memo + ' cumi') ), ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Campur', text=order_memo + ' campur') ) ]) menu_pesan = TemplateSendMessage( alt_text='Menu pesanan', template=pilihan_menu) line_bot_api.reply_message(event.reply_token, menu_pesan) else: order_mistake(event) elif 2 <= len(arguments_list) <= 5 and arguments_list[-1] != 'selesai': if validate_order(arguments_list, -1): sauce_template = ImageCarouselTemplate(columns=[ ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='XO', text=order_memo + ' xo') ), ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Mayonnaise', text=order_memo + ' mayo') ), ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Bumbu Bali', text=order_memo + ' bali') ), ImageCarouselColumn( image_url='https://via.placeholder.com/512x512', action=MessageAction(label='Blackpepper', text=order_memo + ' blackpepper') ) ]) sauce_choice = TemplateSendMessage( alt_text='Menu saus', template=sauce_template) confirm_button = ButtonsTemplate( text=('Pesananmu sekarang:' + '\nNasi : ' + arguments_list[0] + '\nTopping : ' + arguments_list[1] + '\nSaus(max 4): ' + ', '.join(arguments_list[2:])), actions=[ MessageAction(label='Selesai memesan', text=order_memo + ' selesai') ]) order_confirm = TemplateSendMessage( alt_text='Pesanan saat ini', template=confirm_button) line_bot_api.reply_message(event.reply_token, [sauce_choice, order_confirm]) else: order_mistake(event) elif (len(arguments_list) == 6) and (arguments_list[-1] != 'selesai'): if validate_order(arguments_list, -1): summary_button = ButtonsTemplate( text=('Apakah pesanan sudah benar?' + '\nNasi : ' + arguments_list[0] + '\nTopping : ' + arguments_list[1] + '\nSaus(max 4): ' + ', '.join(arguments_list[2:])), actions=[ MessageAction(label='Selesai memesan', text=order_memo + ' selesai') ]) order_summary = TemplateSendMessage( alt_text='Konfirmasi pesanan', template=summary_button) line_bot_api.reply_message(event.reply_token, order_summary) elif len(arguments_list) >= 3 and arguments_list[-1] == 'selesai': if validate_order(arguments_list, -2): line_bot_api.reply_message( event.reply_token, TextSendMessage(text='Pesanan dikirim!') ) else: order_mistake(event) else: order_mistake(event)