def redirect_for_edit(cls, line_id, employee_id, patient, report_date, report_period, form_id, edit_url=None): token = get_random_string(32) LinebotDummyLog.append( line_id, 'begin_report', json.dumps({ 'patient_id': patient.id, 'date': report_date.strftime('%Y-%m-%d'), 'period': report_period, 'session_key': token })) cache.set( '_daily_report_cache:%s' % token, (employee_id, patient.id, report_date, report_period, form_id), 3600) form = settings.CARE_REPORTS.get(form_id) if not form: return HttpResponse('無效的報表識別碼: %s' % form_id, content_type='text/plain; charset=utf-8') params = form['params'] % { 'session': '%s;%s' % (quote(patient.name), quote(token)), 'date': quote(report_date.strftime('%Y-%m-%d')) } if edit_url: return redirect('%s&%s' % (edit_url, params)) else: return redirect(form['url'] % params)
def handle_postback(event): if event.source.type != 'user': return LinebotDummyLog.append(event.source.user_id, 'postback', event.postback.data) resp = json.loads(event.postback.data) target = resp['T'] if target == linebot_patients.T_PATIENT: linebot_patients.handle_postback(line_bot, event, resp) elif target == linebot_emergency.T_EMERGENCY: linebot_emergency.handle_postback(line_bot, event, resp) elif target == linebot_report.T_REPORT: linebot_report.handle_postback(line_bot, event, resp) elif target == linebot_simplequery.T_SIMPLE_QUERY: linebot_simplequery.handle_postback(line_bot, event, resp) elif target == linebot_nursing.T_NURSING: linebot_nursing.handle_postback(line_bot, event, resp) elif target == linebot_customer.T_CUSTOMER: linebot_customer.handle_postback(line_bot, event, resp) elif target == 'association': linebot_patients.handle_association(line_bot, event, resp)
def form_postback(request): if request.method == 'POST': try: doc = json.loads(request.body.decode()) session_key = doc['response']['payload'].pop( settings.CARE_REPORTS_SESSION_KEY, ';').split(';', 1)[-1] token = '_daily_report_cache:%s' % session_key employee_id, patient_id, report_date, report_period, form_id = cache.get( token) cache.delete(token) report_body = doc['response']['payload'] report_body['_meta'] = { 'edit_url': doc['response']['editUrl'], 'google_id': doc['response']['id'], 'timestamp': doc['response']['timestamp'] } except Exception as err: logger.exception('invaild form postback') return HttpResponse('', content_type='text/plain; charset=utf-8') employee = Employee.objects.get(id=employee_id) patient = Patient.objects.get(id=patient_id) LinebotDummyLog.append( 'google_session:%s' % session_key, 'postback_report', json.dumps({ 'patient_id': patient_id, 'date': report_date.strftime('%Y-%m-%d'), 'period': report_period })) report, created = CareDailyReport.objects.get_or_create( patient_id=patient_id, report_date=report_date, report_period=report_period, defaults={ 'filled_by_id': employee_id, 'form_id': form_id, 'report': report_body }) if created: employee.push_message( '%s 在 %s 的 %s 已經收到' % (patient.name, report_date, report.form_name)) return HttpResponse('', content_type='text/plain; charset=utf-8') else: if report_body['_meta']['timestamp'] == report.report.get( '_meta', {}).get('timestamp'): return HttpResponse('', content_type='text/plain; charset=utf-8') if report.reviewed_by: if patient.manager_set.filter(employee=employee, relation='照護經理'): report.reviewed_by = employee report.report = report_body report.save() employee.push_message( '%s 在 %s 的 %s 已經審核完成 (已更新)。' % (patient.name, report_date, report.form_name)) return HttpResponse( '', content_type='text/plain; charset=utf-8') else: employee.push_message( '%s 在 %s 的 %s 已經鎖定,請聯絡照護經理。' % (patient.name, report_date, report.form_name)) return HttpResponse( '', content_type='text/plain; charset=utf-8') else: if patient.manager_set.filter(employee=employee, relation='照護經理'): report.reviewed_by = employee report.report = report_body report.save() employee.push_message( '%s 在 %s 的 %s 已經審核完成。' % (patient.name, report_date, report.form_name)) return HttpResponse( '', content_type='text/plain; charset=utf-8') else: report.filled_by = employee report.report = report_body report.save() employee.push_message( '%s 在 %s 的 %s 已經更新。' % (patient.name, report_date, report.form_name)) return HttpResponse( '', content_type='text/plain; charset=utf-8') else: logger.error('invaild form postback action: %s', request.method) return HttpResponse('', content_type='text/plain; charset=utf-8')
def handle_sticker(event): if event.source.type != "user": return LinebotDummyLog.append(event.source.user_id, 'sticker', event.message.as_json_string())
def handle_location(event): if event.source.type != "user": return LinebotDummyLog.append(event.source.user_id, 'location', event.message.as_json_string())
def handle_message(event): if event.source.type != "user": return LinebotDummyLog.append(event.source.user_id, 'text', event.message.text) if event.message.text == '由康照護': linebot_customer.main_page(line_bot, event) elif event.message.text == '照護日誌': linebot_patients.request_daily_reports(line_bot, event) # elif event.message.text == '緊急通報': # linebot_emergency.ignition_emergency(line_bot, event) elif event.message.text == '照護秘書': linebot_report.ignition_report(line_bot, event) elif event.message.text == '照護小叮嚀': linebot_simplequery.ignition(line_bot, event, linebot_simplequery.CATALOG_COURSE) elif event.message.text == '聯絡照護團隊': linebot_simplequery.ignition(line_bot, event, linebot_simplequery.CATALOG_CONTECT) else: key = '_line:reply:%s' % event.source.user_id magic = cache.get(key) if magic: cache.delete(key) target = magic['T'] if target == linebot_patients.T_PATIENT: linebot_patients.handle_message(line_bot, event, event.message.text, magic) elif target == linebot_simplequery.T_SIMPLE_QUERY: linebot_simplequery.handle_message(line_bot, event, event.message.text, magic) else: if event.message.text == 'whoami': line_bot.reply_message(event.reply_token, TextSendMessage(text=event.source.user_id)) elif is_system_admin(event): try: if event.message.text == '1': flush_messages_queue() line_bot.reply_message(event.reply_token, TextSendMessage(text='訊息柱列已經清空。')) elif event.message.text == '2': count = linebot_nursing.schedule_fixed_schedule_message() line_bot.reply_message(event.reply_token, TextSendMessage(text='照護排程已處理:%s筆' % count)) elif event.message.text == '3': a, b, c = linebot_patients.prepare_dairly_cards() line_bot.reply_message(event.reply_token, TextSendMessage(text='%i 個個案已送出卡片\n%i 個個案等待審核\n%i 個個案已略過 (本日已送出)' % (b, c, a))) elif event.message.text == '7': try: sync_schedule_from_csv(event) except RuntimeError: pass elif event.message.text == '8': sync_employees_from_csv(event) elif event.message.text == '9': line_bot.push_message(event.source.user_id, TextSendMessage('準備與 CRM 同步資料,這會使用一段時間...')) from ukumpcore.crm.agile import sync_patients, sync_customers sync_patients() sync_customers() line_bot.reply_message(event.reply_token, TextSendMessage(text='CRM 資料已更新完成')) elif event.message.text == '0': line_bot.reply_message(event.reply_token, TextSendMessage( text='命令清單:\n' '1 - 立刻清空訊息柱列\n' '2 - 立刻檢查照護排程清單\n' '3 - 立刻送出客戶卡片\n' '7 - 從 csv 同步班表\n' '8 - 從 csv 同步員工資料\n' '9 - 從 CRM 更新個案資料')) except Exception as err: line_bot.reply_message(event.reply_token, TextSendMessage(text='處理命令時發生錯誤:%s' % err)) raise else: raise LineMessageError(event.source.user_id)