def cekserver(): conn = create_connection() cursor = conn.cursor() content = request.get_json() text = content['text'] user_id = '99' name_user = '******' time = dt.datetime.now() result_message = {} if dt.datetime.now() < dt.datetime.now().replace( hour=12, minute=0, second=0) and dt.datetime.now() > dt.datetime.now().replace( hour=0, minute=0, second=0): salam = "Selamat Pagi " elif dt.datetime.now() > dt.datetime.now().replace( hour=12, minute=0, second=0) and dt.datetime.now() < dt.datetime.now().replace( hour=18, minute=0, second=0): salam = "Selamat Siang " elif dt.datetime.now() > dt.datetime.now().replace( hour=18, minute=0, second=0): salam = "Selamat Malam " else: salam = "Assalamualaikum " decision = decide_process(text) print("DEBUG> pilihan = ", decision) if decision == "informasi": disease_id = 0 sinonim, penyakit, messages_info = get_info(text) if len(penyakit) == 0 and len(sinonim) <= 2: # gabung_sinonim = ' '.join(sinonim) messages = check_greeting(sinonim) save_history(user_id, name_user, text, messages, "", disease_id, time, conn) else: messages = salam + name_user for msg in messages_info: messages = messages + "\n\n" + msg[0][0] save_history(user_id, name_user, text, messages, "", disease_id, time, conn) result_message['message'] = messages return jsonify(result_message) else: messages = message_bot(user_id, name_user, salam, text, time, conn) result_message['message'] = messages return jsonify(result_message) delete_menukonsultasi(user_id, conn)
def get_info(text): stopwords = get_stopword('file/konjungsi_info.csv') contents = tokenizing(text) filters = filtering(contents, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) conn = create_connection() cursor = conn.cursor() penyakit = [] sinonim_untuk_gejala = copy.deepcopy(sinonim) # remove stopword_info_list = ["apa", "mengapa", "bagaimana", "obat", "sebab", "solusi", "gejala", "komplikasi", "cegah"] stop_list = [word for word in stopword_info_list if word in sinonim_untuk_gejala] for stop in stop_list: sinonim_untuk_gejala.remove(stop) if "sakit" in sinonim_untuk_gejala: sinonim_untuk_gejala.remove("sakit") for i in sinonim_untuk_gejala: cursor.execute("SELECT id_penyakit, nama_penyakit FROM penyakit WHERE nama_penyakit LIKE '%" + i + "%'") penyakit.append(cursor.fetchall()) arr_penyakit = [e for e in penyakit if e] # list of tuple to list and not empty # if len(arr_penyakit) == 0: # messages = check_greeting(sinonim) # return sinonim, arr_penyakit, messages print("DEBUG> arr_penyakit = ", arr_penyakit) if len(arr_penyakit) != 0: penyakit_max = penyakit_count(arr_penyakit, sinonim) result = get_keywoard(sinonim, penyakit_max, conn) else: result = [[("Nama penyakit tidak dicantumkan. Silahkan menyertakan nama penyakit dan informasi yang ingin diketahui",)]] return sinonim, arr_penyakit, result
from processing.sinonim import get_sinonim from processing.symptoms import get_symptoms """ untuk kegunaan tes line bot, contoh python app_local_bot.py <argumen input user>""" if __name__ == "__main__": # dapatkan argumen cmd, contoh: python app_local_bot.py "saya merasa mual muntah" args = sys.argv if len(args) == 1: # text = "saya mual, muntah, bintik merah pada kulit, nyeri untuk melirik" # text = "demam tinggi,mata tidak merah, batuk darah, mata berair, tidak bisa tidur, kepala tidak sakit, sensitif terhadap cahaya" # text = "Saya merasa mual dan kepala serasa berputar, saya sakit apa?" text = "mag" else: text = args[1] conn = create_connection() cursor = conn.cursor() stopwords = get_stopword('file/konjungsi_info.csv') contents = tokenizing(text) filters = filtering(contents, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) # dapatkan gejala # gejalas = [] # for sin in sinonim: # cursor.execute("SELECT * FROM gejala WHERE nama_gejala LIKE '%" + sin + "%'") # gejalas = [[r[0], r[1]] for r in cursor.fetchall()] # print(gejalas)
def decide_process(text): print("\nDEBUG> ------------ DECIDE PROCESS --------------") conn = create_connection() cursor = conn.cursor() stopwords = get_stopword('file/konjungsi_info.csv') contents = tokenizing(text) filters = filtering(contents, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) stopword_info_list = [ "apa", "kenapa", "mengapa", "bagaimana", "obat", "sebab", "solusi", "gejala", "komplikasi", "cegah" ] stop_list = [word for word in stopword_info_list if word in sinonim] for stop in stop_list: sinonim.remove(stop) if "sakit" in sinonim: sinonim.remove("sakit") if "demam" in sinonim: sinonim.remove("demam") # print("DEBUG> sinonim baru = ", sinonim) # print("DEBUG> stop_list = ", stop_list) daftar_gejala, id_gejala, nama_gejala = get_symptoms(conn, sinonim) # print("DEBUG> daftar gejala", daftar_gejala) daftar_penyakit = [] for i in sinonim: cursor.execute( "SELECT id_penyakit, nama_penyakit FROM penyakit WHERE nama_penyakit LIKE '%" + i + "%'") daftar_penyakit.append(cursor.fetchall()) daftar_penyakit = [e for e in daftar_penyakit if e] # list of tuple to list and not empty # print("DEBUG> daftar penyakit", daftar_penyakit) if len(stop_list) != 0: print("DEBUG> ------------ END DECIDE PROCESS --------------\n") if len(stop_list) >= 1: if len(daftar_penyakit) == 0 and len(daftar_gejala) < 2: return "informasi" elif len(daftar_penyakit) > 0 and len(daftar_gejala) < 2: return "informasi" else: return "konsultasi" else: return "informasi" else: if len(sinonim) == 1 and "tidak" in sinonim: return "konsultasi" if len(daftar_penyakit) == 0 and len(daftar_gejala) < 2: return "informasi" elif len(daftar_penyakit) > 0 and len(daftar_gejala) < 2: return "informasi" else: return "konsultasi"
def handle_text_message(event): text = event.message.text profile = line_bot_api.get_profile(event.source.user_id) 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( text='hoge1', image_url='https://via.placeholder.com/1024x1024', action=DatetimePickerAction(label='datetime', data='datetime_postback', mode='datetime')), ImageCarouselColumn( text='hoge1', 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 == 'image': bubble = BubbleContainer( direction='ltr', hero=ImageComponent( url='http://gosehat.heroku.com/static/image/logo_new.png', 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='Tentang Aplikasi', 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= 'GoSehat merupakan aplikasi konsultasi kesehatan yang dibangun oleh :' '1. Arvianti Yulia Maulfa, 2. Entin Martiana Kusumaningtyas, 3. Fadilah Fahrul Hardiansyah ' '' # BoxComponent( # layout='baseline', # spacing='sm', # contents=[ # TextComponent( # text='GoSehat merupakan aplikasi konsultasi kesehatan yang dibangun oleh :' # '1. Arvianti Yulia Maulfa, 2. Entin Martiana Kusumaningtyas, 3. Fadilah Fahrul Hardiansyah ' # '', # 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.lower() == 'tentang aplikasi': url = request.url_root + '/static/image/tentang.png' app.logger.info("url=" + url) text = 'GoSehat merupakan aplikasi konsultasi kesehatan yang dibangun oleh:\n1. Arvianti Yulia Maulfa \n2. Entin ' \ 'Martiana Kusumaningtyas \n3. Fadilah Fahrul Hardiansyah\n' line_bot_api.reply_message(event.reply_token, [ ImageSendMessage(url, url), TextSendMessage(text=text), ]) elif text.lower() == 'informasi aplikasi': text = 'GoSehat adalah chatbot (aplikasi pintar) yang dapat digunakan untuk ' \ 'konsultasi kesehatan secara gratis! Anda dapat bertanya seputar kesehatan seperti pengobatan,' \ 'pencegahan, atau penyebab suatu penyakit. \n\nCukup kirim pesan apa yang ingin Anda tanyakan atau ' \ 'memilih menu yang tersedia ya.\ncontoh: "Saya sering mengalami pusing, mual, batuk. Saya ' \ 'sakit apa ?" atau menanyakan informasi seperti "Haloo, untuk obat maag apa ya?" \n\nTahapan pemakaian ' \ 'aplikasi GoSehat :\n1. Ketikkan pesan seperti pada contoh diatas atau pilih menu \n2. Anda bisa ' \ 'mengetikkan gejala untuk mendeteksi penyakit pada tubuh Anda\n3. Anda bisa mengetikkan nama penyakit ' \ 'untuk mengetahui jenis penyakit\n4. Tunggu hingga aplikasi memberikan Anda jawaban\n\nTetap jaga ' \ 'kesehatan ya!' line_bot_api.reply_message( event.reply_token, TextSendMessage(text=text), ) 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: conn = create_connection() cursor = conn.cursor() # set user_id dan profile (untuk nama) user_id = event.source.user_id name_user = line_bot_api.get_profile(event.source.user_id).display_name time = dt.datetime.now() if dt.datetime.now() < dt.datetime.now().replace( hour=12, minute=0, second=0) and dt.datetime.now() > dt.datetime.now().replace( hour=0, minute=0, second=0): salam = "Selamat Pagi " elif dt.datetime.now() > dt.datetime.now().replace( hour=12, minute=0, second=0) and dt.datetime.now() < dt.datetime.now().replace( hour=18, minute=0, second=0): salam = "Selamat Siang " elif dt.datetime.now() > dt.datetime.now().replace( hour=18, minute=0, second=0): salam = "Selamat Malam " else: salam = "Assalamualaikum " # MENU if text == '\informasi': messages = "Masukkan informasi yang ingin dicari.\nContoh : 'Apa penyakit maag ?'" save_menuinformasi(user_id, name_user, text, conn) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=(messages))) elif text == '\konsultasi': save_menukonsultasi(user_id, name_user, text, conn) messages = "Masukkan keluhan Anda.\nContoh : 'Saya merasa demam, mual pusing muntih. Saya sakit apa ?'" line_bot_api.reply_message(event.reply_token, TextSendMessage(text=(messages))) else: cursor.execute("SELECT status FROM menu WHERE id_user = '******'") count_menu = cursor.fetchall() # print("DEBUG> count menu = ", count_menu) if len(count_menu) != 0: if count_menu[0][0] == '\informasi': disease_id = 0 sinonim, penyakit, messages_info = get_info(text) if len(penyakit) == 0: messages = check_greeting(sinonim) save_history(user_id, name_user, text, messages, "", disease_id, time, conn) else: messages = salam + name_user + "\n" + messages_info[0][0] save_history(user_id, name_user, text, messages_info[0][0], "", disease_id, time, conn) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=(messages))) delete_menukonsultasi(user_id, conn) elif count_menu[0][0] == '\konsultasi': messages = message_bot(user_id, name_user, salam, text, time, conn) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=(messages))) delete_menukonsultasi(user_id, conn) else: decision = decide_process(text) print("DEBUG> pilihan = ", decision) if decision == "informasi": disease_id = 0 sinonim, penyakit, messages_info = get_info(text) if len(penyakit) == 0 and len(sinonim) <= 2: # gabung_sinonim = ' '.join(sinonim) messages = check_greeting(sinonim) save_history(user_id, name_user, text, messages, "", disease_id, time, conn) else: messages = salam + name_user for msg in messages_info: messages = messages + "\n\n" + msg[0][0] save_history(user_id, name_user, text, messages, "", disease_id, time, conn) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=(messages))) else: messages = message_bot(user_id, name_user, salam, text, time, conn) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=(messages))) delete_menukonsultasi(user_id, conn)