def reply(update, context, from_callback=False, callback_msg=None): """Create reply to user.""" msg = update.message.text reply = response.create_reply(msg) reply_markup = button.create_button(msg) update.message.reply_text(reply, reply_markup=reply_markup, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def distance_result(window, button_event, distance, width, height): """ window: (window) pygame window button_event: (pygame event) pygame event that is either MOUSEBUTTONDOWN or MOUSEBUTTON UP distance: (float) the path's distance width: (int) width of window in pixels height: (int) height of window in pixels output: (int) updataed distance summary: creates a static button and updates the distance displayed on the button """ # starting coordinates of dropdown menu x = (width / 5) * 4 + 1 w = width /5 y = 0 h = height - width distance = round(distance, 0) # Static button create_button(window, button_event, lambda: "", lambda: "", width/54, f"Distance: {distance}", Colors.BUTTON_DEFAULT, Colors.BUTTON_DEFAULT, x, y, w, h) return distance
def time_result(window, button_event, time, width, height): """ window: (window) pygame window button_event: (pygame event) pygame event that is either MOUSEBUTTONDOWN or MOUSEBUTTON UP time: (float) the path's time, based off the speed of the nodes and distance of the path width: (int) width of window in pixels height: (int) height of window in pixels output: (int) updataed time summary: creates a static button and updates the time displayed on the button """ # starting coordinates of dropdown menu x = (width / 5) * 3 + 1 w = width / 5 - 1 y = 0 h = height - width time = round(time, 2) # Static button create_button(window, button_event, lambda: "", lambda: "", width/54, f"Time: {time}", Colors.BUTTON_DEFAULT, Colors.BUTTON_DEFAULT, x, y, w, h) return time
def reply_message(update, context, command): """Creating reply to user""" chat_id = update.message.chat.id # Get response text template try: text = get_template(f"command/{command}.html") except: text = None # Create button based on command try: keyboard = button.create_button(command) except: keyboard = None # Create and send reply for specific command if command == "kalendar": caption, image_path, _ = load_data(command).values() send_image(context, chat_id, image_path, keyboard, f"🗓 {caption}") elif command == "jam": image_path = load_data(command)["img_path"] send_image(context, chat_id, image_path) elif command == "loker": data = load_data(command) content = "" sorted_data = sort_data(data) for id in sorted_data: date, title, url = sorted_data[id].values() content += f"🗞 <b>({date}) <a href='{url}'>{title}</a></b>\n" text = text.format(content) elif command == "jadwal": keyboard = None message = update.message.text.lower() try: message_split = message.split(" ", 1) # Split command and query cmd, query = message_split assert cmd == "/jadwal" # Must be in '/jadwal [KELAS_ATAU_DOSEN]' format try: assert len(query) >= 5 # Query must be 5 characters long # Scraping data try: send_text( update, f"Mencari jadwal untuk input: <b>{query}</b> ...") data = scraping.scraping_jadwal_kuliah(query) if data: # Successful scraping img_path, caption = data keyboard = button.create_button(command) send_image(context, chat_id, img_path, keyboard, dedent(caption)) else: # Query not found text = get_template( "error/jadwal_not_found.html").format(query) except Exception as err: # Failed scraping data text = get_template( "error/jadwal_failed_scraping.html").format(err) except: # Query length < 5 text = get_template("error/jadwal_short_query.html") except: # Wrong format text = get_template("error/jadwal_wrong_format.html") elif command == "berita": data = load_data(command) message = update.message.text.lower() try: # Send news content by id message_split = message.split(" ", 1) # Split command and query cmd, query = message_split if cmd == "/berita": # Must be in '/berita [ID]' format try: content, _, title, _ = data[query].values() text = f"<b>{title.upper()}</b>\n\n{content}" except: # ID not found keyboard = None text = get_template("error/berita_not_found.html").format( query) else: # Wrong format keyboard = None text = get_template("error/berita_wrong_format.html") except: # Send list of news content = "" sorted_data = sort_data(data) for id in sorted_data: _, date, title, url = sorted_data[id].values() content += f"🗞 <b>({date} - {id}) <a href='{url}'>{title}</a></b>\n" text = text.format(content) if text: send_text(update, text, keyboard)
for ii, opts in enumerate(product(sizes, styles, clasps)): backend_value = "&".join([str(opt["value"]) for opt in opts]) value = "size:{} style:{} {}".format( *[str(opt["value"]) for opt in opts]) price = float(v["price"]) + sum( [float(opt["price"]) for opt in opts]) price = "{:.2f}".format(price) merged_options.append({"value": quote(value), "price": price}) merged_options_yml[backend_value] = { "value": value, "price": price } # hidding the actual mechanics button_html = create_button(merged_options, v["name"], sandbox=args.production == False) button_html = button_html.replace( "<table>", "<table style=\"display:none;\">") button_html = button_html.replace( "method=\"post\">", "method=\"post\" style=\"padding:0px\">") buttons[product_num + 1] = button_html all_options[product_num + 1] = json.dumps(merged_options_yml) #break if args.paypal: with open("_data/paypal_buttons.yml", "w") as f: yaml.dump(buttons, f) with open('_data/merged_options.yml', 'w') as f:
def __create_dropdown(window, button_event, entries, width, x, y, w, h): """ window: (window) pygame window button_event: (pygame event) pygame event that is either MOUSEBUTTONDOWN or MOUSEBUTTON UP entries: (list) list of entries for dropdown menu in order from top to bottom width: (int) x: (int) pixel x value of button y: (int) pixel y value of button w: (int) pixel width of button h: (int) pixel height of button output: results (array) array of same dimensions as entries, with updated values summary: handles the dropdown menu functionality. Updates the inputed entries according to user input (i.e. mouse being pressed) """ results = [] selected = False prev_true = None for i, entry in enumerate(entries): results.append(entry[1]) if entry[1] == True: prev_true = i # return true if button is not pressed def ret_true(): return True # return false so that start screen exits def ret_false(): return False dropdown = entries[0][1] # Static button if dropdown == False: name = entries[0][0] result = create_button(window, button_event, lambda: ret_false(), lambda: ret_true(), width/54, name, Colors.BUTTON_HOVER, Colors.BUTTON_DEFAULT, x, y, w, h) results[0] = result # Dropdown view elif dropdown == True: name = entries[0][0] result = create_button(window, button_event, lambda: ret_true(), lambda: ret_false(), width/54, name, Colors.BUTTON_HOVER, Colors.BUTTON_DEFAULT, x, y, w, h) results[0] = result for i, entry in enumerate(entries[1:]): y += h name = entry[0] result = create_button(window, button_event, lambda: ret_false(), lambda: ret_true(), width/54, name, Colors.BUTTON_HOVER, Colors.BUTTON_DEFAULT, x, y, w, h) results[i + 1] = result # if something is selected, mark it down if result == True: selected = True # if something has been selected, stop showing menu if selected == True: name = entries[0][0] result = create_button(window, button_event, lambda: ret_false(), lambda: ret_true(), width/54, name, Colors.BUTTON_HOVER, Colors.BUTTON_DEFAULT, x, 0, w, h) results[0] = result # if nothing has been selected, return to the previous state elif prev_true is not None and prev_true != 0: results[prev_true] = True return results
def start_screen(window, width, button_offset): pg.display.set_caption('Pathfinder') INTRO = True MOUSEUP = None while INTRO: MOUSEUP = None for event in pg.event.get(): # quit if prompted if event.type == pg.QUIT: INTRO = False pg.quit() quit() if event.type == MOUSEBUTTONUP and event.button == 1: MOUSEUP = MOUSEBUTTONUP if event.type == KEYDOWN: # exit fullscreen mode if event.key == K_ESCAPE: flags = DOUBLEBUF WINDOW = pg.display.set_mode((WIDTH, HEIGHT), flags) # enter fullscreen mode if event.key == pg.K_f: flags = DOUBLEBUF | FULLSCREEN WINDOW = pg.display.set_mode((WIDTH, HEIGHT), flags) # create the main screen and text window.fill(Colors.TEXT) large_text = pg.font.Font(Fonts.HOME, int(width / 7)) text_surf, text_rect = button.text_objects("Pathfinder", large_text, Colors.START_SCREEN_TEXT) text_rect.center = ((width / 2), (width / 2)) window.blit(text_surf, text_rect) # create the button rectangle x = 0 y = 0 w = width / 5 h = width / 14 x = (width / 2) - (w / 2) y = (width / 2) - (h / 2) + button_offset # return true if button is not pressed def ret_true(): return True # return false so that start screen exits def ret_false(): return False INTRO = button.create_button(window, MOUSEUP, lambda: ret_true(), lambda: ret_false(), width / 24, "START", Colors.BUTTON_HOVER, Colors.BUTTON_DEFAULT, x, y, w, h) pg.display.update()