def readDate(self, token, userId, client): """觀看約會""" colLis = [] for dating in Date.objects(status=10): girl = Member.getUser(dating.femaleId) action = actions.PostbackAction(data=dating.femaleId, label="邀請她", display_text="邀請她") column = template.CarouselColumn( actions=[action], title=f'{girl.calculate_age()},{girl.nickName}', text= f'我在{dating.workDist}上班,喜歡吃{dating.eatype},禮拜{str(dating.dateDate.isoweekday())}有空嗎?', thumbnail_image_url=girl.pictUri, ) colLis.append(column) if colLis == []: if token != userId: client.reply_message(token, TextSendMessage(text='目前無約會')) return carouse = template.CarouselTemplate(columns=colLis) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return
def createNews(newsType): resp = requests.get( 'https://www.google.com/search', params={ 'q': newsType, 'tbm': 'nws', 'start': '0' }, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36' }) resp.encoding = 'UTF-8' html_doc = resp.text soup = BeautifulSoup(html_doc, 'html.parser') news = soup.select('.ts') ImgSoruce = { '自來水': 'https://i.imgur.com/KxbkZz6.png', '電力': 'https://i.imgur.com/YTuT4j6.jpg', '瓦斯': 'https://i.imgur.com/46bvScx.png' }[newsType] newsColums = [] for n in news: col = template.CarouselColumn( thumbnail_image_url=ImgSoruce, title=''.join(list(n.select_one('.r').stripped_strings)), text=(''.join(list(n.select_one('.st').stripped_strings)))[:55] + '...', actions=[ actions.URIAction(label='前往新聞頁面', uri=n.select_one('.l')["href"]) ]) newsColums.append(col) carousel_template_message = template.TemplateSendMessage( alt_text='Carousel template', template=template.CarouselTemplate(columns=newsColums, image_size='contain')) return carousel_template_message
def createDIYList(DIYType): DIYList = { '自來水': WaterDIYList, '電力': ElectricityDIYList, '瓦斯': GasDIYList }[DIYType] DIYColums = [] for n in DIYList: col = template.CarouselColumn( thumbnail_image_url='https://i.imgur.com/2IzuDCM.jpg', title=n['title'], text=DIYType, actions=[actions.URIAction(label='前往DIY頁面', uri=n['url'])]) DIYColums.append(col) carousel_template_message = template.TemplateSendMessage( alt_text='Carousel template', template=template.CarouselTemplate(image_size='contain', columns=DIYColums)) return carousel_template_message
def readInvList(self, token, userId, client): """觀看邀請名單""" colLis = [] date = Member.getDate(self.userId) for invId in date.invList: user = Member.getUser(invId) text = f'個性{user.personality}喜歡{user.hobit}的{user.getConstellation()}男孩' action = actions.PostbackAction(data=invId, label="選他", display_text="選他") column = template.CarouselColumn(title=user.nickName, text=text, thumbnail_image_url=user.pictUri, actions=[action]) colLis.append(column) carouse = template.CarouselTemplate(columns=colLis) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return
def CarouselSendMessage(forecast_day): columns = [] for i, lapse in enumerate(forecast_day.values()): # 多すぎたので偶数個だけ表示 if i % 2 == 0: image_url = f"https://rawgit.com/Wild-Family/Forecaster-bot/add-feature-forecast/bot/icons/{lapse['weather'][0]['icon']}.png" columns.append( template.CarouselColumn( thumbnail_image_url=image_url, title=datetime.datetime.strptime( lapse['dt_txt'], '%Y-%m-%d %H:%M:%S').strftime('%Y/%m/%d %H:%M'), text=build_forecast_text(lapse), actions=[ template.MessageTemplateAction( label='More Info', text='i dont know sry :)') ])) message = template.TemplateSendMessage( alt_text='天気教えたるで', template=template.CarouselTemplate(columns=columns)) return message
def webhook(): client = LineBotApi(os.environ['lineKey']) rJson = request.json["events"] if not rJson: return "" eventType: Final = rJson[0]["type"] if eventType not in ["postback", "message"]: return userId: Final = rJson[0]["source"]["userId"] user = cluster.Member.getUser(userId) token: Final = rJson[0]["replyToken"] replyT = "" if eventType == "postback": status = user.status if status == 100: """event 男生發出邀約""" reqsT: Final = rJson[0]["postback"]["data"] user.status += 10 user.save() date = cluster.Member.getDate(reqsT) date.assignValue(reqsT=userId, userId=userId, token=token, client=client) replyT = date.replyText(reqsT=userId, userId=userId, token=token, client=client) date.statusChange(reqsT=userId) # push message to girl if token != userId: client.push_message(reqsT, TextSendMessage(text='收到邀約囉')) elif status == 110: """女生選擇約會對象""" reqsT: Final = rJson[0]["postback"]["data"] date = cluster.Member.getDate(userId) date.assignValue(reqsT=reqsT, userId=userId, token=token, client=client) replyT = date.replyText(reqsT=reqsT, userId=userId, token=token, client=client) date.statusChange(reqsT=reqsT) # push message to man if token != userId: client.push_message(reqsT, TextSendMessage(text='配對成功囉,開始聊天')) return replyT # status 1 ~ 2 reqsT: Final = rJson[0]["message"]["text"] if not user: if reqsT in ["開始使用", "去註冊約她"]: replyT = "請輸入性別" action1 = actions.MessageAction(text="男生", label="男生") action2 = actions.MessageAction(text="女生", label="女生") column = template.CarouselColumn(text=replyT, actions=[action1, action2]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return replyT elif reqsT == "預覽約會": replyT = "本週約會有" action = actions.MessageAction(text="去註冊約她", label="去註冊約她") columnL = [] for girl in cluster.Female.objects(status__gte=15): column = template.CarouselColumn( title=girl.nickName, text=girl.introT(), thumbnail_image_url=girl.pictUri, actions=[action]) if len(columnL) == 10: break else: columnL.append(column) carouse = template.CarouselTemplate(columns=columnL) if token != userId: client.reply_message(token, [ TextSendMessage(text=replyT), template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return replyT elif reqsT in ["男生", "男"]: cluster.Male(userId=userId, status=3).save() replyT = "注意:需用<>標記訊息" elif reqsT in ["女生", "女"]: cluster.Female(status=3, userId=userId).save() replyT = "注意:需用<>標記訊息" elif reqsT in ["測試帳號男"]: ming = cluster.Male.objects(nickName="小明", phone="test123")[0] ming.userId = userId ming.save() replyT = "設置完成" elif reqsT in ["測試帳號女"]: mei = cluster.Female.objects(nickName="小美", phone="test123")[0] mei.userId = userId mei.save() replyT = "設置完成" elif reqsT == "快速使用(測試用)": replyT = "選擇性別" action1 = actions.MessageAction(text="測試帳號男", label="測試帳號男") action2 = actions.MessageAction(text="測試帳號女", label="測試帳號女") column = template.CarouselColumn(text=replyT, actions=[action1, action2]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return replyT else: replyT = "歡迎" action1 = actions.MessageAction(text="開始使用", label="開始使用") action2 = actions.MessageAction(text="預覽約會", label="預覽約會") action3 = actions.MessageAction(text="快速使用(測試用)", label="快速使用(測試用)") column = template.CarouselColumn( text=replyT, actions=[action1, action2, action3]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return replyT if token != userId: client.reply_message(token, TextSendMessage(text=replyT)) return replyT status = user.status # status 3 ~ 14 if status in range(3, 15): replyT = user.signup(reqsT=reqsT, client=client, token=token) elif status == 100: if reqsT == "發起約會" and not user.isMale(): replyT = user.createDate() elif reqsT == "觀看約會" and user.isMale(): user.readDate(token=token, userId=userId, client=client) return elif reqsT == "我的交友名片": replyT = "我的交友名片" action = actions.MessageAction(text='好', label='好') column = template.CarouselColumn(title=user.nickName, text=user.introT(), thumbnail_image_url=user.pictUri, actions=[action]) carouse = template.CarouselTemplate(columns=[column]) sendMsg = [ TextSendMessage(text=replyT), template.TemplateSendMessage(template=carouse, alt_text="break") ] if token != userId: client.reply_message(token, sendMsg) return else: replyT = "歡迎" action = "" if user.isMale(): action = actions.MessageAction(text="觀看約會", label="觀看約會") else: action = actions.MessageAction(text="發起約會", label="發起約會") action2 = actions.MessageAction(text="我的交友名片", label="我的交友名片") column = template.CarouselColumn(text=replyT, actions=[action, action2]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return replyT elif status == 110: date = cluster.Member.getDate(userId) if reqsT == "觀看邀請名單" and not user.isMale(): user.readInvList(token=token, userId=userId, client=client) return "" date.assignValue(reqsT=reqsT, userId=userId, token=token, client=client) replyT = date.replyText(reqsT=reqsT, userId=userId, token=token, client=client) date.statusChange(reqsT=reqsT) return replyT if token != userId: client.reply_message(token, TextSendMessage(text=replyT)) return replyT
def replyText(self, token, userId, reqsT, client): tup = ( (), ("workDist", "幾點開始午休呢", ("十二點", "十二點半", "一點")), ("lunchBreakT", "午休時間很長嗎?", ("普通,一小", "還行,一小半", "很長,兩小")), ("lunchBreakL", "喜歡吃韓式還是日式", ("日式", "韓式", "港式")), ("eatype", "那約個明天、後天", ("明天", "後天", "大後天")), ("dateDate", "成功發起約會"), (), (), (), (), # index:10 ("invList", "成功邀約,對象會在24小時內回覆"), ("maleId", "開放 12hr 聊天", ("討論好餐廳和時間了", )), (), (), (), (), (), (), (), (), # index:20 ( None, "請輸入<inLIne定位資訊>", ), ("inlineRes", "關閉聊天,約會前12hr會開啟"), (), (), (), (), (), (), (), (), (None, None), (), (), (), (), (), (), (), (), (), # index:40 (None, "祝您約會順利")) STAT = self.status replytext = tup[STAT][1] if STAT in (1, 2, 3, 4): action1 = actions.MessageAction(text=tup[STAT][2][0], label=tup[STAT][2][0]) action2 = actions.MessageAction(text=tup[STAT][2][1], label=tup[STAT][2][1]) action3 = actions.MessageAction(text=tup[STAT][2][2], label=tup[STAT][2][2]) column = template.CarouselColumn( text=replytext, actions=[action1, action2, action3]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) elif STAT in (10, ) and Member.getUser(reqsT) is None: replytext = "無人邀請" elif STAT in (11, ) and Member.getUser(reqsT) is None: replytext = "有人邀約了" action = actions.MessageAction(text="觀看邀請名單", label="觀看邀請名單") column = template.CarouselColumn(text=replytext, actions=[action]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) return replytext elif STAT in (11, ): action = actions.MessageAction(text=tup[STAT][2][0], label=tup[STAT][2][0]) column = template.CarouselColumn(text=replytext, actions=[action]) carouse = template.CarouselTemplate(columns=[column]) if token != userId: client.reply_message(token, TextSendMessage(text=replytext)) if token != userId: client.push_message(self.maleId, [ template.TemplateSendMessage(template=carouse, alt_text="broke") ]) elif STAT in (20, 40) and reqsT not in ("討論好餐廳和時間了", "我出發了"): to, prefix = ("", "") if userId == self.maleId: to = self.femaleId prefix = "👦:" else: to = self.maleId prefix = "👩:" if token != userId: client.push_message(to, TextSendMessage(text=prefix + reqsT)) return reqsT elif STAT == 20: to, prefix = ("", "") if userId == self.maleId: to = self.femaleId prefix = "👦:" else: to = self.maleId prefix = "👩:" if token != userId: client.push_message( to, TextSendMessage(text=f"{prefix}定好約會囉,約會那天再聊吧")) if token != userId: client.reply_message(token, TextSendMessage(text=replytext)) elif STAT == 30: replytext = "時間還沒到,不能跟對象聊天唷" if token != userId: client.reply_message(token, TextSendMessage(text=replytext)) else: if token != userId: client.reply_message(token, TextSendMessage(text=replytext)) return replytext
def signup(self, reqsT, client, token): """ 只處理 status += 1 的流程 status not change 和 status -=1 情況當作例外處理 (regex , 6, 13等 共3處) """ tup = ( (), (), (), # status3 (None, "請輸入<名字>"), ("nickName", "請輸入<生日> (yyyy-mm-dd)"), ("birthDate", None, ("沒錯", "剛剛手滑了")), (None, "請輸入<個人特質>"), ("personality", "請輸入<興趣>"), ("hobit", "請輸入<職業>"), ("job", "請輸入<照片url>"), # status10 ("pictUri", "請輸入<信箱>"), ("email", "請輸入<電話>"), ("phone", "請輸入<驗證碼>,查看手機簡訊"), (None, "最後確認,這樣資料正確嗎?", ("沒錯", )), (None, "待審核後就可以開始使用了"), ) STAT = self.status if tup[STAT][0]: m = re.search("<(\\S+)>", reqsT) if not m: """status not change""" if token != self.userId: client.reply_message(token, TextSendMessage(text="偵測不到<>,請再試一次")) return "偵測不到<>,請再試一次" else: reqsT = m.groups()[0] if STAT in (6, ) and reqsT in ["不是", "剛剛手滑了", "n"]: """status -1 """ self.status -= 1 self.birthDate = None self.save() if token != self.userId: client.reply_message( token, TextSendMessage(text="請輸入<生日> (yyyy-mm-dd)")) return "請輸入<生日> (yyyy-mm-dd)" if STAT in (13, ) and reqsT not in [ "iampassword", ]: """status -1 """ self.status -= 1 self.phone = None self.save() if token != self.userId: client.reply_message(token, TextSendMessage(text="錯誤,請再輸入一次手機")) return "錯誤,請再輸入一次手機" # 處理assign Value if STAT == 5: reqsT = dt.fromisoformat(reqsT) attr = tup[STAT][0] if attr: setattr(self, attr, reqsT) # 處理 status 變換 self.status += 1 # 處理 replyMessage sendmesg replyT, sendMsg = tup[STAT][1], TextSendMessage(text=tup[STAT][1]) if STAT == 5: 生日 = self.birthDate.isoformat() 生肖 = self.getConstellation() listS = ['您是', 生日, '的', 生肖, '男孩嗎?' ] if self.isMale() else ['您是', 生日, '的', 生肖, '女孩嗎?'] replyT = ''.join(listS) action1 = actions.MessageAction(text=tup[STAT][2][0], label=tup[STAT][2][0]) action2 = actions.MessageAction(text=tup[STAT][2][1], label=tup[STAT][2][1]) column = template.CarouselColumn(text=replyT, actions=[action1, action2]) carouse = template.CarouselTemplate(columns=[column]) sendMsg = [ template.TemplateSendMessage(template=carouse, alt_text="broke") ] elif STAT == 13: action = actions.MessageAction(text=tup[STAT][2][0], label=tup[STAT][2][0]) column = template.CarouselColumn(title=self.nickName, text=self.introT(), thumbnail_image_url=self.pictUri, actions=[action]) carouse = template.CarouselTemplate(columns=[column]) sendMsg = [ TextSendMessage(text=replyT), template.TemplateSendMessage(template=carouse, alt_text="break") ] self.save() if token != self.userId: client.reply_message(token, sendMsg) return replyT