def process(input, entities=None): output = {} try: url = "http://unbelievablefactsblog.com/rss" # request to dowmload website content r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') description_tag = soup.findAll('description')[1] textMe = description_tag.text start = textMe.find('<p>') + 3 end = textMe.find('<br/>', start) #facts = json.load(facts_file) #facts_list = facts['facts'] message = TextTemplate(textMe[start:end]).get_message() #message = add_quick_reply(message, 'Another fact!', modules.generate_postback('fact')) message = add_quick_reply(message, 'Tell me a joke.', modules.generate_postback('joke')) message = add_quick_reply(message, 'Show me a quote.', modules.generate_postback('quote')) output['input'] = input output['output'] = message output['success'] = True except: output['success'] = False return output
def process(input, entities=None): #Get all digits in input, use first digit found as number of sides sides = [int(s) for s in input.split(" ") if s.isdigit()] #Roll a 6 sided die if desired sides was 6, 0, or unspecified print(sides) if len(sides) == 0 or sides[0] == 6 or sides[0] == 0: message = AttachmentTemplate(dice_sides[random.randint(1, 6)], type='image').get_message() message = add_quick_reply(message, 'Roll again!', modules.generate_postback('dice')) message = add_quick_reply(message, 'Flip a coin.', modules.generate_postback('coin')) output = {'input': input, 'output': message, 'success': True} return output #Otherwise roll a die with the specified number of sides else: message = TextTemplate("You rolled a " + str(random.randint(1, sides[0])) + ".").get_message() message = add_quick_reply(message, 'Roll again!', modules.generate_postback('dice')) message = add_quick_reply(message, 'Flip a coin.', modules.generate_postback('coin')) output = {'input': input, 'output': message, 'success': True} return output
def process(input, entities=None): output = {} try: url = "https://www.brainyquote.com/link/quotebr.rss" # request to dowmload website content r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') # contentVar = soup.currentTag # titleVar = soup.findAll('title') # descVar = soup.findAll('description') title_tag = soup.findAll("title")[2] description_tag = soup.findAll("description")[2] # print(title_tag.text) # print(description_tag.text) quotes = (title_tag.text + ' ; ' + description_tag.text) #with open(config.QUOTES_SOURCE_FILE) as quotes_file: #quotes = json.load(quotes_file) #quotes_list = quotes['quotes'] message = TextTemplate(quotes).get_message() #message = add_quick_reply(message, 'Another one!', modules.generate_postback('quote')) message = add_quick_reply(message, 'Show me a fact.', modules.generate_postback('fact')) message = add_quick_reply(message, 'Tell me a joke.', modules.generate_postback('joke')) output['input'] = input output['output'] = message output['success'] = True except: output['success'] = False return output
def search(input, sender=None, postback=False): if postback: payload = json.loads(input) intent = payload['intent'] entities = payload['entities'] else: if "math" in input: intent = "math" entities = input.replace("math", "") else: intent, entities = process_query(input) # TODO: Needs to be refactored out try: keen.project_id = os.environ.get('KEEN_PROJECT_ID', config.KEEN_PROJECT_ID) keen.write_key = os.environ.get('KEEN_WRITE_KEY', config.KEEN_WRITE_KEY) keen.add_event( 'logs', { 'intent': intent, 'entities': entities, 'input': input, 'sender': sender, 'postback': postback }) except: pass # Could not stream data for analytics if intent is not None: if intent in src.__personalized__ and sender is not None: r = requests.get('https://graph.facebook.com/v2.6/' + str(sender), params={ 'fields': 'first_name', 'access_token': os.environ.get('ACCESS_TOKEN', config.ACCESS_TOKEN) }) if entities is None: entities = {} entities['sender'] = r.json() data = sys.modules['modules.src.' + intent].process(input, entities) if data['success']: return data['output'] else: if 'error_msg' in data: return data['error_msg'] else: return TextTemplate( 'Something didn\'t work as expected! I\'ll report this to my master.' ).get_message() else: message = TextTemplate( 'I\'m sorry; I\'m not sure I understand what you\'re trying to say.\nTry typing "help" or "request"' ).get_message() message = add_quick_reply(message, 'Help', modules.generate_postback('help')) message = add_quick_reply(message, 'Request', modules.generate_postback('request')) return message
def process(input, entities=None): message = AttachmentTemplate(dice_sides[random.randint(1, 6)], type='image').get_message() message = add_quick_reply(message, 'Roll again!', modules.generate_postback('dice')) message = add_quick_reply(message, 'Flip a coin.', modules.generate_postback('coin')) output = {'input': input, 'output': message, 'success': True} return output
def process(input, entities=None): message = AttachmentTemplate(coin_images[random.choice(['heads', 'tails'])], type='image').get_message() message = add_quick_reply(message, 'Flip again!', modules.generate_postback('coin')) message = add_quick_reply(message, 'Roll a die.', modules.generate_postback('dice')) output = {'input': input, 'output': message, 'success': True} return output
def process(input, entities=None): message = AttachmentTemplate(dice_sides[random.randint(1, 6)], type='image').get_message() message = add_quick_reply(message, 'Roll again!', modules.generate_postback('dice')) message = add_quick_reply(message, 'Flip a coin.', modules.generate_postback('coin')) output = { 'input': input, 'output': message, 'success': True } return output
def process(input, entities=None): message = AttachmentTemplate(coin_images[random.choice(['heads', 'tails'])], type='image').get_message() message = add_quick_reply(message, 'Flip again!', modules.generate_postback('coin')) message = add_quick_reply(message, 'Roll a die.', modules.generate_postback('dice')) output = { 'input': input, 'output': message, 'success': True } return output
def process(input, entities=None): output = {} try: with open(config.QUOTES_SOURCE_FILE) as quotes_file: quotes = json.load(quotes_file) quotes_list = quotes['quotes'] message = TextTemplate(choice(quotes_list)).get_message() message = add_quick_reply(message, 'Another one!', modules.generate_postback('quote')) message = add_quick_reply(message, 'Show me a fact.', modules.generate_postback('fact')) message = add_quick_reply(message, 'Tell me a joke.', modules.generate_postback('joke')) output['input'] = input output['output'] = message output['success'] = True except: output['success'] = False return output
def process(input, entities=None): help = 'Hi there! I\'m Jarvis, your personal assistant.' if entities is not None: if 'sender' in entities and 'first_name' in entities['sender']: sender_name = entities['sender']['first_name'] help = help.replace('there', sender_name) help += '\n\nYou can tell me things like:' help += '\n - define comfort' help += '\n - iron man 2 movie plot' help += '\n - tell me a joke/quote/fact' help += '\n - wiki html' help += '\n - mail id message' help += '\n - maths module type solve (your operation)' help += "\n - restaurants near me or i'm hungry" help += '\n - camera' help += '\n - anything you want book' help += '\n - usd to eur rate' help += '\n - death note anime' help += '\n - time in seattle' help += '\n - songs by linkin park' help += '\n - shorten google.com' help += '\n - weather in london' help += '\n - videos of sia' help += '\n - flip a coin' help += '\n - roll a die' help += '\n - show a random xkcd comic' help += '\n - latest news' help += '\n - paradise lyrics' help += '\n - ping google.com' help += '\n\nI\'m always learning, so do come back and say hi from time to time!' help += '\nHave a nice day. :)' message = TextTemplate(help).get_message() message = add_quick_reply(message, 'Tell me a joke', modules.generate_postback('joke')) message = add_quick_reply(message, 'Roll a die', modules.generate_postback('dice')) message = add_quick_reply(message, 'Latest News', modules.generate_postback('news')) message = add_quick_reply(message, 'Random xkcd comic', modules.generate_postback('xkcd')) output = { 'input': input, 'output': message, 'success': True } return output
def process(input, entities=None): output = {} try: with open(config.FACTS_SOURCE_FILE) as facts_file: facts = json.load(facts_file) facts_list = facts['facts'] message = TextTemplate(choice(facts_list)).get_message() message = add_quick_reply(message, 'Another fact!', modules.generate_postback('fact')) message = add_quick_reply(message, 'Tell me a joke.', modules.generate_postback('joke')) message = add_quick_reply(message, 'Show me a quote.', modules.generate_postback('quote')) output['input'] = input output['output'] = message output['success'] = True except: output['success'] = False return output
def process(input, entities=None): output = {} try: with open(config.JOKES_SOURCE_FILE) as jokes_file: jokes = json.load(jokes_file) jokes_list = jokes['jokes'] message = TextTemplate(choice(jokes_list)).get_message() message = add_quick_reply(message, 'One more!', modules.generate_postback('joke')) message = add_quick_reply(message, 'Show me a quote.', modules.generate_postback('quote')) message = add_quick_reply(message, 'Tell me a fact.', modules.generate_postback('fact')) output['input'] = input output['output'] = message output['success'] = True except: output['success'] = False return output
def search(input, sender=None, postback=False): if postback: payload = json.loads(input) intent = payload['intent'] entities = payload['entities'] else: intent, entities = process_query(input) # TODO: Needs to be refactored out try: keen.project_id = os.environ.get('KEEN_PROJECT_ID', config.KEEN_PROJECT_ID) keen.write_key = os.environ.get('KEEN_WRITE_KEY', config.KEEN_WRITE_KEY) keen.add_event('logs', { 'intent': intent, 'entities': entities, 'input': input, 'sender': sender, 'postback': postback }) except: pass # Could not stream data for analytics if intent is not None: if intent in src.__personalized__ and sender is not None: r = requests.get('https://graph.facebook.com/v2.6/' + str(sender), params={ 'fields': 'first_name', 'access_token': os.environ.get('ACCESS_TOKEN', config.ACCESS_TOKEN) }) if entities is None: entities = {} entities['sender'] = r.json() data = sys.modules['modules.src.' + intent].process(input, entities) if data['success']: return data['output'] else: if 'error_msg' in data: return data['error_msg'] else: return TextTemplate('Something didn\'t work as expected! I\'ll report this to my master.').get_message() else: message = TextTemplate( 'I\'m sorry; I\'m not sure I understand what you\'re trying to say.\nTry typing "help" or "request"').get_message() message = add_quick_reply(message, 'Help', modules.generate_postback('help')) message = add_quick_reply(message, 'Request', modules.generate_postback('request')) return message
def process(input, entities=None): help = '哈囉! 我是土思機! 請問有什麼需要幫助的嗎?' if entities is not None: if 'sender' in entities and 'first_name' in entities['sender']: sender_name = entities['sender']['first_name'] help = help.replace('there', sender_name) help += '\n\n我可以幫你' help += '\n - 檢查租賃契約' help += '\n - 我是房東/房客' help += '\n\n有時我會睡著一下~需要一些時間清醒\n請稍微等我一下' help += '\n歡迎隨時呼叫我! 也祝你有個美好的一天~' message = TextTemplate(help).get_message() message = add_quick_reply(message, '租賃契約', modules.generate_postback('lease_contract')) message = add_quick_reply(message, 'land', modules.generate_postback('land_toast')) output = {'input': input, 'output': message, 'success': True} return output
def process(input,entities): output = {} try: nse = Nse() symbol = " " # symbol is the 'NSE symbol of the company' s= input.split(' ') for i in s: if nse.is_valid_code(str(i)): symbol = i break q = nse.get_quote(str(symbol)) # Getting Company name using variable symbol company_name = q['companyName'] company_symbol = q['symbol'] # Getting open price of the day open_price = q['open'] # Getting closing price of the day close_price = q['closePrice'] # Getting average price of the day stock_price = q['faceValue'] # Top gainers for the last trading session # Top losers for the last trading session output['input'] = "Stock_price" msg = 'Company Name = '+company_name+" \n "+"Symbol = "+company_symbol+"\n "+"Open Price = "+str(open_price)+"\n"+"Close Price = "+str(close_price)+"\n"+"Stock Price = "+str(stock_price) msg_1={'text':msg} msg_1= add_quick_reply(msg_1, 'wipro stock', modules.generate_postback('stock_price')) msg_1 = add_quick_reply(msg_1, 'Tell me a joke.', modules.generate_postback('joke')) msg_1 = add_quick_reply(msg_1, 'roll a dice.', modules.generate_postback('dice')) output['output']=msg_1 output['success']=True except: error_message = 'I couldn\'t understand the symbol ' error_message += '\nPlease ask something like tcs stock_price etc.' output['error_msg'] = TextTemplate(error_message).get_message() output['success'] = False return output
def process(input, entities=None): help = 'Hi there! I\'m Jarvis, your personal assistant.' if entities is not None: if 'sender' in entities and 'first_name' in entities['sender']: sender_name = entities['sender']['first_name'] help = help.replace('there', sender_name) help += '\n\nYou can tell me things like:' help += '\n - define comfort' help += '\n - iron man 2 movie plot' help += '\n - tell me a joke/quote/fact' help += '\n - wiki html' help += '\n - anything you want book' help += '\n - usd to eur rate' help += '\n - death note anime' help += '\n - time in seattle' help += '\n - songs by linkin park' help += '\n - shorten google.com' help += '\n - weather in london' help += '\n - videos of sia' help += '\n - flip a coin' help += '\n - roll a die' help += '\n - show a random xkcd comic' help += '\n - latest news' help += '\n - paradise lyrics' help += '\n - ping google.com' help += '\n\nI\'m always learning, so do come back and say hi from time to time!' help += '\nHave a nice day. :)' message = TextTemplate(help).get_message() message = add_quick_reply(message, 'Tell me a joke', modules.generate_postback('joke')) message = add_quick_reply(message, 'Roll a die', modules.generate_postback('dice')) message = add_quick_reply(message, 'Latest News', modules.generate_postback('news')) message = add_quick_reply(message, 'Random xkcd comic', modules.generate_postback('xkcd')) output = { 'input': input, 'output': message, 'success': True } return output
def process(input, entities=None): output = {} try: with requests_cache.enabled('xkcd_cache', backend='sqlite', expire_after=3600): # Get the latest comic r = requests.get('http://xkcd.com/info.0.json') data = r.json() # Get a random comic between the first and the latest one r = requests.get('http://xkcd.com/%d/info.0.json' % randint(1, data['num'])) data = r.json() title = data['title'] item_url = 'http://xkcd.com/' + str(data['num']) + '/' explanation_url = 'http://explainxkcd.com/' + str(data['num']) + '/' image_url = data['img'].replace('\\', '') subtitle = data['alt'] buttons = ButtonTemplate() buttons.add_web_url('xkcd Link', item_url) buttons.add_web_url('Explanation Link', explanation_url) template = GenericTemplate() template.set_image_aspect_ratio_to_square() template.add_element(title=title, item_url=item_url, image_url=image_url, subtitle=subtitle, buttons=buttons.get_buttons()) message = template.get_message() message = add_quick_reply(message, 'Check out another!', modules.generate_postback('xkcd')) output['input'] = input output['output'] = message output['success'] = True except: error_message = 'There was some error while retrieving data from xkcd.' output['error_msg'] = TextTemplate(error_message).get_message() output['success'] = False return output
def process(input, entities=None): print('process',input,entities) output = {} try: if entities['type'] == None: message = TextTemplate('選擇你的角色').get_message() entities['type'] = 'choice' entities['role'] = {'landlord':1,'tenant':0} message = add_quick_reply(message, '我是房東', modules.generate_postback(input,entities)) entities['role'] = {'landlord':0,'tenant':1} message = add_quick_reply(message, '我是房客', modules.generate_postback(input,entities)) #message = add_quick_reply(message, '其他', modules.generate_postback('other')) output['input'] = input output['output'] = message output['success'] = True elif entities['type'] == 'choice' and entities['role']['landlord'] == 1: entities['type'] = 'landlord_model' message = TextTemplate('房東注意事項').get_message() message = add_quick_reply(message, '了解更多', modules.generate_postback(input,entities)) message = add_quick_reply(message, '再試一次!', modules.generate_postback(input,in_entities)) output['input'] = input output['output'] = message output['success'] = True elif entities['type'] == 'choice' and entities['role']['tenant'] == 1: entities['type'] = 'tenant_model' message = TextTemplate('房客注意事項').get_message() message = add_quick_reply(message, '了解更多', modules.generate_postback(input,entities)) message = add_quick_reply(message, '再試一次!', modules.generate_postback(input,in_entities)) output['input'] = input output['output'] = message output['success'] = True elif entities['type'] == 'landlord_model': message = TextTemplate('房東注意事項...').get_message() output['input'] = input output['output'] = message output['success'] = True elif entities['type'] == 'tenant_model': message = TextTemplate('房客注意事項...').get_message() output['input'] = input output['output'] = message output['success'] = True else: output['success'] = False except: output['success'] = False return output
def process(input, entities=None): print('process', input, entities) output = {} if entities['type'] == None: message = TextTemplate( '嗨,我是土思機器人啦!\n想要我幫你檢查看看,你簽的租賃契約合理嗎?').get_message() entities['type'] = 'step1' entities['choice'] = True message = add_quick_reply(message, '好啊,拿出契約來檢查一下好了!', modules.generate_postback(input, entities)) entities['choice'] = False message = add_quick_reply(message, '不想,我沒有租屋啦', modules.generate_postback(input, entities)) elif entities['type'] == 'step1': entities['type'] = 'step2' if entities['choice'] == True: message = TextTemplate('開始囉!上面寫的押金是幾個月租金呢?').get_message() entities['choice'] = True message = add_quick_reply( message, '2個月以下', modules.generate_postback(input, entities)) entities['choice'] = False message = add_quick_reply( message, '2個月以上', modules.generate_postback(input, entities)) elif entities['choice'] == False: message = TextTemplate('那我們無話可說…').get_message() entities['choice'] = None elif entities['type'] == 'step2': entities['type'] = 'step3' if entities['choice'] == True: message = TextTemplate( '太好了,押金最高不可以超過2個月房屋租金的總額。\n也建議要在合約上寫清楚退還時間與方式喔!\n\n下一題,契約裡的租金有寫清楚嗎?' ).get_message() entities['choice'] = True message = add_quick_reply( message, '有喔!', modules.generate_postback(input, entities)) entities['choice'] = False message = add_quick_reply( message, '好像….沒有欸?!', modules.generate_postback(input, entities)) elif entities['choice'] == False: message = TextTemplate('什麼?!你知道這樣其實已經超過法律規定的額度了嗎….').get_message() entities['choice'] = None elif entities['type'] == 'step3': entities['type'] = 'step4' if entities['choice'] == True: message = TextTemplate( '讚喔!除了租金的金額外,也應該包括何時給付及付款方式。還有管理費、清潔費或其他費用,也應該盡量寫在合約中。\n\n再來,修繕的責任有寫清楚嗎?' ).get_message() entities['choice'] = True message = add_quick_reply( message, '寫得清清楚楚', modules.generate_postback(input, entities)) entities['choice'] = False message = add_quick_reply( message, '疑?!怎麼沒看到…', modules.generate_postback(input, entities)) elif entities['choice'] == False: message = TextTemplate('什麼?!你知道這樣有可能被多收錢嗎…').get_message() entities['choice'] = None elif entities['type'] == 'step4': entities['type'] = 'step5' if entities['choice'] == True: message = TextTemplate( '喔喔喔喔!美賣喔~~~也建議在簽約時,依照實際狀況,逐一討論並載明於租約中,未來比較不會有爭執喔! \n\n再來,上面有寫到不能報稅嗎?' ).get_message() entities['choice'] = True message = add_quick_reply( message, '沒有!', modules.generate_postback(input, entities)) entities['choice'] = False message = add_quick_reply( message, '可…可惡!房東特別寫下來了啦…', modules.generate_postback(input, entities)) elif entities['choice'] == False: message = TextTemplate('什麼?!你知道這樣有可能被多收錢嗎…').get_message() entities['choice'] = None elif entities['type'] == 'step5': entities['type'] = 'step6' if entities['choice'] == True: message = TextTemplate( '太厲害了,恭喜你完成租約的考驗!你的租賃契約寫得很不錯,要記得確保契約內容,權利才會有保障喔!').get_message( ) entities['choice'] = None elif entities['choice'] == False: message = TextTemplate('什麼?!你知道房東這樣其實是違法的嗎….').get_message() entities['choice'] = None elif entities['type'] == 'end': template = TextTemplate() template.set_text('更多詳細內容請看我們整理的懶人包:今天要簽約?教你看到租約一眼就抓到重點') text = template.get_text() template = ButtonTemplate(text) #message = TextTemplate('更多詳細內容請看我們整理的懶人包:今天要簽約?教你看到租約一眼就抓到重點').get_message() link = 'https://www.facebook.com/LandToast' #template = ButtonTemplate(message) template.add_web_url('傳送門', link) output['input'] = input output['output'] = template.get_message() output['success'] = True return output else: output['success'] = False return output if entities['choice'] == None: entities['type'] = None message = add_quick_reply(message, '再試一次!', modules.generate_postback(input, entities)) entities['type'] = 'end' message = add_quick_reply(message, '結束對話', modules.generate_postback(input, entities)) output['input'] = input output['output'] = message output['success'] = True return output