def render_episodes(episodes, variables): """Render cards given a list of episodes""" if not len(episodes): return [Message(u'沒有更多的集數可以看囉 :(')] m = Message() for ep in episodes: b = Message.Bubble(ep.drama_name + u'第 %d 集' % ep.serial_number, image_url=CacheImage(ep.image_url), subtitle=ep.description) b.add_button( Message.Button(Message.ButtonType.WEB_URL, u'帶我去看', url=TrackedURL(ep.link, 'WatchButton'), variables=variables)) b.add_button( Message.Button(Message.ButtonType.POSTBACK, u'我想看前幾集', payload=EventPayload( 'GET_HISTORY', { 'drama_id': ep.drama_id, 'from_episode': ep.serial_number, 'backward': True, }))) m.add_bubble(b) m.add_quick_reply(Message.QuickReply(Message.QuickReplyType.TEXT, u'第1集')) m.add_quick_reply(Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門韓劇')) m.add_quick_reply(Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門日劇')) m.add_quick_reply(Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門台劇')) m.add_quick_reply(Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門陸劇')) m.add_quick_reply(Message.QuickReply(Message.QuickReplyType.TEXT, u'通知設定')) return [m]
def add_quick_reply_keywords(news_info, message, show_related=False): """Install QuickReply keywords into message.""" keywords = news_info.get_keywords(MAX_KEYWORDS) reply_kws = [] if show_related: query_term = CollectedData.GetLast('query_term', '') related_kws = news_info.get_related_keywords(query_term, 3) reply_kws = [kw.name for kw in related_kws] for kw in keywords: if len(reply_kws) < MAX_KEYWORDS and kw.name not in reply_kws: reply_kws.append(kw.name) else: reply_kws += [kw.name for kw in keywords] for kw in reply_kws: message.add_quick_reply(Message.QuickReply( Message.QuickReplyType.TEXT, kw)) message.add_quick_reply(Message.QuickReply( Message.QuickReplyType.TEXT, u'熱門新聞')) message.add_quick_reply(Message.QuickReply( Message.QuickReplyType.TEXT, u'搜尋'))
def append_categories_to_quick_reply(m): m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'荼蘼')) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'The K2 第十集')) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門韓劇')) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門日劇')) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門台劇')) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'熱門陸劇')) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'通知設定')) return [m]
def run(content_config, unused_env, variables): """ content_config schema: Platform dependent message: { 'send_payload_to_current_node': False, 'api_key': 'google geocoding api key', 'query_term': 'query term', 'language': 'zh_TW', 'region': 'tw', 'bounds': [ {'lat': 23.816576, 'long': 119.781068}, {'lat': 25.314444, 'long': 122.053702} ], 'center': {'lat': 23.816576, 'long': 122.053702} } """ query_term = Resolve(content_config['query_term'], variables) # Remove stop words query_term = re.sub(STOP_WORDS, '', query_term) cfg = content_config api = GoogleMapsPlaceAPI(content_config['api_key']) py_bounds = [] for b in cfg.get('bounds', []): py_bounds.append([(b[0]['lat'], b[0]['long']), (b[1]['lat'], b[1]['long'])]) results = api.query_top_n(3, query_term, cfg['language'], cfg['region'], py_bounds, cfg.get('center', None)) in_currrent = content_config['send_payload_to_current_node'] if not results: m = Message(u'對不起,我找不到這個地址, 請重新輸入 >_<') elif len(results) == 1: m = Message(u'你指的是「%s」嗎?' % results[0]['address']) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'是', payload=LocationPayload(results[0]['location'], in_currrent), acceptable_inputs=[u'^對', '(?i)y', '(?i)ok'])) m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'否', payload=EventPayload('WRONG_ADDRESS', None, in_currrent), acceptable_inputs=[u'不', '(?i)n'])) else: m = Message(buttons_text=u'你指的是以下哪一個地址呢?') for r in results: m.add_button( Message.Button(Message.ButtonType.POSTBACK, r['address'], payload=LocationPayload(r['location'], in_currrent))) return [m]
def run(content_config, unused_env, variables): """ content_config schema: { "bot_admins": { "platform_user_ident_1": ["bot_id_1", "bot_id_2" ..], ... } } """ user = g.user bots = content_config['bot_admins'].get(user.platform_user_ident) if not bots: return [] bot = Memory.Get('bot') if not bot: msgs = [ Message(u'嗨 {{user.first_name}},你想要操作哪隻機器人呢?', variables=variables) ] page = 1 m = Message() msgs.append(m) bubble = Message.Bubble('第 %d 頁' % page) for bot_id in bots: bot = Bot.get_by(id=bot_id, single=True) if not bot: continue bubble.add_button( Message.Button(Message.ButtonType.POSTBACK, title=bot.name, payload=EventPayload('SELECT_BOT', bot_id))) if len(bubble.buttons) == 3: m.add_bubble(bubble) page += 1 bubble = Message.Bubble(u'第 %d 頁' % page) if len(bubble.buttons): m.add_bubble(bubble) msgs[-1].add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'放棄', payload=EventPayload('CONTROL_FLOW', 'reset'), acceptable_inputs=['(?i)giveup', '(?i)reset'])) return msgs operation = Memory.Get('operation') if not operation: m = Message(buttons_text=u'你想要執行什麼動作呢?') m.add_button( Message.Button(Message.ButtonType.POSTBACK, title=u'廣播訊息', payload=EventPayload('SELECT_OP', 'broadcast'))) m.add_button( Message.Button(Message.ButtonType.POSTBACK, title=u'放棄', payload=EventPayload('CONTROL_FLOW', 'reset'))) return [m] if operation == 'broadcast': broadcast_message = Memory.Get('broadcast_message') status = Memory.Get('status') if status == 'input_broadcast_message': m = Message(u'你可以繼續輸入下一則訊息:') m.add_quick_reply( Message.QuickReply( Message.QuickReplyType.TEXT, u'完成', payload=EventPayload('MESSAGE_INPUT', 'done'), acceptable_inputs=[u'好了', u'(?i)done', '(?i)y'])) m.add_quick_reply( Message.QuickReply( Message.QuickReplyType.TEXT, u'重來', payload=EventPayload('MESSAGE_INPUT', 'restart'), acceptable_inputs=[u'好了', u'(?i)restart', '(?i)cancel'])) m.add_quick_reply( Message.QuickReply( Message.QuickReplyType.TEXT, u'放棄', payload=EventPayload('CONTROL_FLOW', 'reset'), acceptable_inputs=['(?i)giveup', '(?i)reset'])) return [m] elif not broadcast_message: Memory.Set('status', 'input_broadcast_message') return [Message(u'請輸入你要廣播的訊息:')] elif status == 'preview_message': msgs = [Message(u'請確認你要廣播的訊息:')] for raw_msg in broadcast_message: msgs.append(Message.FromDict(raw_msg)) m = msgs[-1] m.add_quick_reply( Message.QuickReply(Message.QuickReplyType.TEXT, u'確認', payload=EventPayload( 'CONFIRM_MESSAGE', True), acceptable_inputs=[u'是', '(?i)y', '(?i)ok'])) m.add_quick_reply( Message.QuickReply( Message.QuickReplyType.TEXT, u'取消', payload=EventPayload('CONFIRM_MESSAGE', False), acceptable_inputs=[u'是', '(?i)no', '(?i)cancel'])) return msgs return [Message(u'錯誤的操作')]