def handle_message(event): ''' 當收到使用者訊息的時候 ''' profile = line_bot_api.get_profile(event.source.user_id) name = profile.display_name uid = profile.user_id message = event.message.text print(name) print(uid) print(message) if mongodb.get_ready(uid, 'users') == 1: mongodb.update_byid(uid, {'ready': 0}, 'users') casttext = name + ' 對大家說: ' + message remessage = TextSendMessage(text=casttext) userids = mongodb.get_all_userid('users') line_bot_api.multicast(userids, remessage) return 0 if message == '群體廣播': # 設定使用者下一句話要群廣播 mongodb.update_byid(uid, {'ready': 1}, 'users') remessage = TextSendMessage(text='請問要廣播什麼呢?') line_bot_api.reply_message(event.reply_token, remessage) return 0 if re.search('Hi|hello|你好|ha', message, re.IGNORECASE): line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text)) return 0 if re.search('新聞|news', event.message.text, re.IGNORECASE): dic = corwler.udn_news() columns = [] for i in range(0, 3): carousel = CarouselColumn( thumbnail_image_url=dic[i]['img'], title=dic[i]['title'], text=dic[i]['summary'], actions=[URITemplateAction(label='點我看新聞', uri=dic[i]['link'])]) columns.append(carousel) remessage = TemplateSendMessage( alt_text='Carousel template', template=CarouselTemplate(columns=columns)) line_bot_api.reply_message(event.reply_token, remessage) return 0 if re.search('Dcard|dcard', event.message.text, re.IGNORECASE): text = corwler.Dcard() line_bot_api.reply_message(event.reply_token, TextSendMessage(text=text)) return 0 if message == 'googlemap': # 取得最新評價 text = corwler.google() # 包裝訊息 remessage = TextSendMessage(text=text) # 回應使用者 line_bot_api.reply_message(event.reply_token, remessage) return 0 line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text)) return 0
def handle_message(event): ''' 當收到使用者訊息的時候 ''' profile = line_bot_api.get_profile(event.source.user_id) name = profile.display_name uid = profile.user_id message = event.message.text print(name) print(uid) print(message) dic = { 'userid': uid, 'username': name, 'creattime': datetime.now(), 'mess': message } mongodb.insert_one(dic, 'message') if mongodb.get_ready(uid, 'users') == 1: mongodb.update_byid(uid, {'ready': 0}, 'users') casttext = name + ' 對大家說: ' + message remessage = TextSendMessage(text=casttext) userids = mongodb.get_all_userid('users') line_bot_api.multicast(userids, remessage) return 0 #關鍵評論網 if re.search('theNewLens', event.message.text, re.IGNORECASE): columns = [] img = 'https://image3.thenewslens.com/assets/web/cover-photo-medium.png' carousel = CarouselColumn(thumbnail_image_url=img, title='關鍵評論網新聞', text='點擊觀看類型', actions=[ MessageTemplateAction(label='科學', text='關鍵評論科學'), MessageTemplateAction(label='商業', text='關鍵評論商業'), MessageTemplateAction(label='職場', text='關鍵評論職場') ]) columns.append(carousel) remessage = TemplateSendMessage( alt_text='Carousel template', template=CarouselTemplate(columns=columns)) line_bot_api.reply_message(event.reply_token, remessage) return 0 if re.search('關鍵評論科學', event.message.text, re.IGNORECASE): dic = corwler.theNewLens('science') makeCard(dic, event) return 0 if re.search('關鍵評論商業', event.message.text, re.IGNORECASE): dic = corwler.theNewLens('business') makeCard(dic, event) return 0 if re.search('關鍵評論職場', event.message.text, re.IGNORECASE): dic = corwler.theNewLens('career') makeCard(dic, event) return 0 #科技報橘 if re.search('科技報橘ai', event.message.text, re.IGNORECASE): #人工智慧 dic = corwler.techorangeAi() makeCard(dic, event) if re.search('科技報橘全部', event.message.text, re.IGNORECASE): #全部 dic = corwler.techorange() makeCard(dic, event) #泛科學 if re.search('泛科學', event.message.text, re.IGNORECASE): dic = corwler.Pansci() makeCard(dic, event)
def handle_message(event): ''' 當收到使用者訊息的時候 ''' profile = line_bot_api.get_profile(event.source.user_id) name = profile.display_name uid = profile.user_id message = event.message.text def choose_id(): # 設定使用者下一句話要群廣播 mongodb.update_byid(uid, {'ready': 1}, 'users') remessage = TextSendMessage(text='請輸入客戶編號') line_bot_api.reply_message(event.reply_token, remessage) def get_id1(message): mongodb.update_byid(uid, {'ready': 0}, 'users') global msg1, fid1 fid1 = 0 msg1 = str(message) if msg1 in id_list: remessage = TextSendMessage(text='推薦客戶編號%s的品牌:\n%s, %s, %s' % \ (msg1, df_id_brand_res.loc[msg1][0], df_id_brand_res.loc[msg1][1], \ df_id_brand_res.loc[msg1][2]) ) line_bot_api.reply_message(event.reply_token, remessage) else: remessage = TextSendMessage(text='無此客戶') line_bot_api.reply_message(event.reply_token, remessage) def get_id2(message): mongodb.update_byid(uid, {'ready': 0}, 'users') global msg1, fid2 fid2 = 0 msg1 = str(message) if msg1 in id_list: remessage = TextSendMessage(text='推薦客戶編號%s的類別:\n%s、%s、%s' % \ (msg1, df_id_cate_res.loc[msg1][0], df_id_cate_res.loc[msg1][1], \ df_id_cate_res.loc[msg1][2]) ) line_bot_api.reply_message(event.reply_token, remessage) else: remessage = TextSendMessage(text='無此客戶') line_bot_api.reply_message(event.reply_token, remessage) def get_id3(message): mongodb.update_byid(uid, {'ready': 0}, 'users') global msg1, fid3 fid3 = 0 msg1 = str(message) if msg1 in id_list: remessage = TextSendMessage(text='推薦客戶編號%s的商品:\n%s-%s\n%s-%s\n%s-%s' % \ (msg1, eval(df_id_cb_res.loc[msg1][0])[1], eval(df_id_cb_res.loc[msg1][0])[0], \ eval(df_id_cb_res.loc[msg1][1])[1], eval(df_id_cb_res.loc[msg1][1])[0], \ eval(df_id_cb_res.loc[msg1][2])[1], eval(df_id_cb_res.loc[msg1][2])[0]) ) line_bot_api.reply_message(event.reply_token, remessage) else: remessage = TextSendMessage(text='無此客戶') line_bot_api.reply_message(event.reply_token, remessage) def choose_group(): # 設定使用者下一句話要群廣播 mongodb.update_byid(uid, {'ready': 1}, 'users') remessage = TextSendMessage( text="請輸入客戶年齡及性別(age, gender)\n女性請填'0',男性請填'1'\n如女性25歲=25,0") line_bot_api.reply_message(event.reply_token, remessage) def get_group1(message): mongodb.update_byid(uid, {'ready': 0}, 'users') global msg2, fgroup1 bins = [0, 19, 21, 23, 33, 41, 101] fgroup1 = 0 transex(message) if msg2 == 'error': remessage = TextSendMessage(text='資料輸入錯誤') line_bot_api.reply_message(event.reply_token, remessage) else: try: group = MTF.insert_trans(msg2, bins) if msg2[1] == 0: sug = MTF.tar_recommand(df_group_brand_res, 'group', group, ['brand']) elif msg2[1] == 1: sug = pd.read_csv(path[1] + res_file2[0] + '.csv', encoding='utf8', header=None).head(3) sug.columns = ['brand', 'score'] remessage = TextSendMessage(text='推薦%s歲%s客戶的品牌:\n%s, %s, %s' % \ (msg2[0], sex,sug.brand[sug.index[0]], \ sug.brand[sug.index[1]], sug.brand[sug.index[2]] )) line_bot_api.reply_message(event.reply_token, remessage) except: errorinput() def get_group2(message): mongodb.update_byid(uid, {'ready': 0}, 'users') global msg2, fgroup2 bins = [0, 19, 21, 23, 33, 41, 101] fgroup2 = 0 transex(message) if msg2 == 'error': remessage = TextSendMessage(text='資料輸入錯誤') line_bot_api.reply_message(event.reply_token, remessage) else: try: group = MTF.insert_trans(msg2, bins) if msg2[1] == 0: sug = MTF.tar_recommand(df_group_cat_res, 'group', group, ['category']) elif msg2[1] == 1: sug = pd.read_csv(path[1] + res_file2[1] + '.csv', encoding='utf8').head(3) remessage = TextSendMessage(text='推薦%s歲%s客戶的類別:\n%s、%s、%s' % \ (msg2[0], sex, sug.category[sug.index[0]], \ sug.category[sug.index[1]], sug.category[sug.index[2]] )) line_bot_api.reply_message(event.reply_token, remessage) except: errorinput() def get_group3(message): mongodb.update_byid(uid, {'ready': 0}, 'users') global msg2, fgroup3 bins = [0, 19, 21, 23, 33, 41, 101] fgroup3 = 0 transex(message) if msg2 == 'error': remessage = TextSendMessage(text='資料輸入錯誤') line_bot_api.reply_message(event.reply_token, remessage) else: try: group = MTF.insert_trans(msg2, bins) if msg2[1] == 0: sug = MTF.tar_recommand(df_group_cb_res, 'group', group, ['category', 'brand']) elif msg2[1] == 1: sug = pd.read_csv(path[1] + res_file2[2] + '.csv', encoding='utf8').head(3) remessage = TextSendMessage(text='推薦%s歲%s客戶的商品:\n%s-%s\n%s-%s\n%s-%s' % \ (msg2[0], sex, sug.brand[sug.index[0]], sug.category[sug.index[0]], \ sug.brand[sug.index[1]], sug.category[sug.index[1]], \ sug.brand[sug.index[2]], sug.category[sug.index[2]] )) line_bot_api.reply_message(event.reply_token, remessage) except: errorinput() def transex(message): global msg2, sex msg2 = message msg2 = msg2.replace(',', ',') msg2 = msg2.split(',') if msg2[1] == '0': sex = '女性' elif msg2[1] == '1': sex = '男性' else: sex = '' try: msg2[0] = int(msg2[0]) msg2[1] = int(msg2[1]) msg2 = tuple(msg2) except ValueError: msg2 = 'error' return 0 def errorinput(): if msg2[0] < 0 or msg2[0] > 100: remessage = TextSendMessage(text='無此年齡區間') line_bot_api.reply_message(event.reply_token, remessage) elif msg2[1] != 0 or msg2[1] != 1: remessage = TextSendMessage(text='無此性別') line_bot_api.reply_message(event.reply_token, remessage) else: remessage = TextSendMessage(text='資料輸入錯誤') line_bot_api.reply_message(event.reply_token, remessage) def report1(cid): string = '誠心推薦!!%s' % str(cid) return string def report2(tup): string = '誠心推薦!!%s, %s' % (tup[0], tup[1]) return string if message == 'ID-品牌': global fid1 fid1 = 1 choose_id() return 0 if message == 'ID-類別': global fid2 fid2 = 1 choose_id() return 0 if message == 'ID-品牌類別': global fid3 fid3 = 1 choose_id() return 0 if message == '族群-品牌': global fgroup1 fgroup1 = 1 choose_group() return 0 if message == '族群-類別': global fgroup2 fgroup2 = 1 choose_group() return 0 if message == '族群-品牌類別': global fgroup3 fgroup3 = 1 choose_group() return 0 if mongodb.get_ready(uid, 'users') == 1 and fid1 == 1: get_id1(message) return 0 if mongodb.get_ready(uid, 'users') == 1 and fid2 == 1: get_id2(message) return 0 if mongodb.get_ready(uid, 'users') == 1 and fid3 == 1: get_id3(message) return 0 if mongodb.get_ready(uid, 'users') == 1 and fgroup1 == 1: get_group1(message) return 0 if mongodb.get_ready(uid, 'users') == 1 and fgroup2 == 1: get_group2(message) return 0 if mongodb.get_ready(uid, 'users') == 1 and fgroup3 == 1: get_group3(message) return 0 line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text)) return 0
def handle_message(event): ''' 當收到使用者訊息的時候 ''' profile = line_bot_api.get_profile(event.source.user_id) name = profile.display_name uid = profile.user_id message = event.message.text def hello(): now = datetime.now() greet = '' twh = int(now.hour + 8) if twh > 24: twh = twh - 24 if twh < 12: greet = '早安!' elif twh < 14: greet = '午安!' elif twh < 18: greet = '下午好!' else: greet = '晚安!' casttext = name + '對大家說:大家' + greet remessage = TextSendMessage(text=casttext) userids = mongodb.get_all_userid('users') msgs = StickerSendMessage( package_id='1', #sticker_id=random.randint(1,15) sticker_id='2') line_bot_api.multicast(userids, remessage) line_bot_api.multicast(userids, msgs) def news(): dic = corwler.udn_news() columns = [] for i in range(0, 3): carousel = CarouselColumn( thumbnail_image_url=dic[i]['img'], title=dic[i]['title'], text=dic[i]['summary'], actions=[URITemplateAction(label='點我看新聞', uri=dic[i]['link'])]) columns.append(carousel) remessage = TemplateSendMessage( alt_text='Carousel template', template=CarouselTemplate(columns=columns)) line_bot_api.reply_message(event.reply_token, remessage) def dcard(): text = corwler.Dcard() line_bot_api.reply_message(event.reply_token, TextSendMessage(text=text)) def choosebank(): # 設定使用者下一句話要群廣播 mongodb.update_byid(uid, {'ready': 1}, 'users') remessage = TextSendMessage(text='請選擇銀行編號(120,142)?') line_bot_api.reply_message(event.reply_token, remessage) def postscore(message): mongodb.update_byid(uid, {'ready': 0}, 'users') message = int(message) text = corwler.google(message) # 包裝訊息 remessage = TextSendMessage(text=text) # 回應使用者 line_bot_api.reply_message(event.reply_token, remessage) if re.search('新聞|news', event.message.text, re.IGNORECASE): news() return 0 if re.search('Dcard|dcard', event.message.text, re.IGNORECASE): dcard() return 0 if message == '打招呼': hello() return 0 if message == '評價': choosebank() return 0 if mongodb.get_ready(uid, 'users') == 1: postscore(message) return 0 if re.search('Hi|hello|你好|ha', message, re.IGNORECASE): line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text)) return 0 line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text)) return 0