def push_my_recent_courses(self, line_id): member = member_repo.get_by_line_id(line_id) applied_courses = course_apply_repo.get_my_recent_courses(member)[:10] columns = [] for applied_course in applied_courses: schedule = applied_course.schedule course = schedule.course actions = [] actions.append(MessageAction(label='課程說明', text=course.description)) title = '{name} {start}'.format(name=course.name, start=schedule.start_date) col = CarouselColumn(title=title, text=course.desc, actions=actions, thumbnail_image_url=course.image_url) columns.append(col) template = TemplateSendMessage( alt_text='近期課程來囉', template=CarouselTemplate(columns=columns, image_aspect_ratio='square'), ) push_templates(line_id, [template])
def push_booking_slot(line_id, date_str): booking_info = time_slot_service.get_booking_slots(date_str) cols = [] groups = [booking_info[i:i + 3] for i in range(0, len(booking_info), 3)] for group in groups: while len(group) < 3: group.append(None) carousel_text = '預約的日期是{date},請選擇時段' actions = [] for info in group: if info: carousel_text = carousel_text.format(date=info['slot'].date) begin = info['slot'].hour end = begin + 1 label = '{begin}點-{end}點 ({remain})'.format( begin=begin, end=end, remain=info['remain']) data = 'CONFIRM#{}'.format(info['slot'].id) actions.append( PostbackAction(label=label, data=build_postback_data(data))) else: actions.append(PostbackAction(label=' ', data=' ')) cols.append(CarouselColumn(text=carousel_text, actions=actions)) templates = TemplateSendMessage(alt_text='請選擇預約的時段', template=CarouselTemplate(columns=cols)) push_templates(line_id, templates)
def push_booking_days(line_id): slots = time_slot_service.get_booking_days() cols = [] groups = [slots[i:i + 3] for i in range(0, len(slots), 3)] for group in groups: while len(group) < 3: group.append(None) actions = [] for slot in group: if slot: year, month, day = slot.date.split('-') dt = datetime.strptime( slot.date, '%Y-%m-%d').replace(tzinfo=timezone('Asia/Taipei')) label = '{month}-{day} {weekday}'.format( month=month, day=day, weekday=WEEKDAY[dt.weekday()]) data = 'DATE#{}'.format(slot.date) actions.append( PostbackAction(label=label, data=build_postback_data(data))) else: actions.append(PostbackAction(label=' ', data=' ')) cols.append(CarouselColumn(text='請選擇想預約的日期', actions=actions)) templates = TemplateSendMessage(alt_text='開放預約日期', template=CarouselTemplate(columns=cols)) push_templates(line_id, templates)
def push_my_reservation(line_id): tz = timezone('Asia/Taipei') today = datetime.now(tz=tz).strftime('%Y-%m-%d') reservations = reservation_service.get_by_line_id(line_id).filter( time_slot__date__gte=today).order_by('time_slot__date')[:10] if len(reservations) <= 0: push_templates(line_id, TextSendMessage(text='你目前沒有預約任何時段喔!')) return cols = [] for reservation in reservations: info = {} info['customer'] = reservation.customer_name info['date'] = reservation.time_slot.date info['begin'] = reservation.time_slot.hour info['end'] = reservation.time_slot.hour + 1 text = '{customer}\n{date}\n{begin}:00 ~ {end}:00'.format(**info) data = 'CANCEL#{id}'.format(id=reservation.id) actions = [ PostbackAction(label='取消預約', data=build_postback_data(data)) ] cols.append(CarouselColumn(text=text, actions=actions)) templates = TemplateSendMessage(alt_text='預約時段查詢', template=CarouselTemplate(columns=cols)) push_templates(line_id, templates)
def push_booking_confirm(line_id, slot_id): time_slot = time_slot_service.get_slot_by_id(slot_id) if not reservation_service.check_slot_available(time_slot): return push_templates( line_id, TextSendMessage(text='阿阿!\n你想預約的時段已經滿了,可以選擇其他的時段嗎?')) # TODO check time table reservation_service.create(line_id, time_slot) push_templates(line_id, TextSendMessage(text='預約成功囉!請問我們要怎麼稱呼你的個案呢?'))
def registered(self, request): if request.method == 'GET': return render(request, 'registered.html') elif request.method == 'POST': data = dict(request.data) # print(data) line_id = data.get('line_id') name = data.get('name') member_service.modify(line_id, name, registered=True) text = '恭喜你 {name} 註冊成功囉'.format(name=name) push_templates(line_id, TextSendMessage(text=text)) return HttpResponse()
def recieve_text(line_id, text, **kwargs): if text == '#可預約時段查詢': push_booking_days(line_id) elif text == '#我預約的時段': push_my_reservation(line_id) # TODO 問答事件table reservation = reservation_service.get_latest(line_id) if reservation and reservation.customer_name.endswith('的個案'): reservation.customer_name = text reservation.save() push_templates(line_id, TextSendMessage(text='完成囉!我們到時見^_^~')) from bot.services import member_service admins = member_service.get_admins() admin_line_ids = [member.line_id for member in admins] text = '新增了一筆預約\n\n{reservation}'.format(reservation=reservation) multicast_templates(admin_line_ids, TextSendMessage(text=text)) return
def push_recent_course(self, line_id): # member = member_repo.get_by_line_id(line_id) schedules, courses = course_schedule_repo.get_recent_courses() columns = [] for course_id, schedule in schedules.items(): course = courses[course_id] actions = [] actions.append(MessageAction(label='課程說明', text=course.description)) for course_schedule in schedule: label = course_schedule.start_date data = 'COURSE_APPLY#{}'.format(course_schedule.id) actions.append( PostbackAction(label=label, data=data, display_text='報名資料已送出')) empty_count = 3 - len(actions) for i in range(empty_count): actions.append(PostbackAction(label='-', data=' ')) col = CarouselColumn(title=course.name, text=course.desc, actions=actions, thumbnail_image_url=course.image_url) columns.append(col) loop_count = int(ceil(len(columns) / 10)) templates = [] for i in range(loop_count): template = TemplateSendMessage( alt_text='近期課程來囉', template=CarouselTemplate(columns=columns[i * 10:(i + 1) * 10], image_aspect_ratio='square'), ) templates.append(template) push_templates(line_id, templates)