def route(bot, update, args, user_data): try: # creació imatge del mapa i enviament al bot image_file = '_route.png' gbOp.checkUserData(user_data) image = gbOp.route(user_data['graph'], args[0] + ' ' + args[1], args[2] + ' ' + args[3]) image.save(image_file) bot.send_photo(chat_id=update.message.chat_id, photo=open(image_file, 'rb')) os.remove(image_file) except ValueError: print('ValueError') bot.send_message(chat_id=update.message.chat_id, text='Value error') except IndexError: print('IndexError') bot.send_message( chat_id=update.message.chat_id, text= 'Usage: /route <src> <dst>\n<src>/<dst> format: "<city>, <country code>"' ) except (MyExceptions.MapRenderException, MyExceptions.NoGraphLoadedException, MyExceptions.NoPathFoundException) as e: print(e.text) bot.send_message(chat_id=update.message.chat_id, text=e.text) except Exception as e: print(e) bot.send_message(chat_id=update.message.chat_id, text='No response')
def graph(bot, update, args, user_data): try: gbOp.checkData(user_data) if len(args) == 2: bot.send_message( chat_id=update.message.chat_id, text='Please, wait until the graph is made. Thanks!') user_data['graph'] = gbOp.graph(args[0], args[1], user_data['data']) bot.send_message(chat_id=update.message.chat_id, text='The graph has been updated') else: raise IndexError() except ValueError: print('ValueError') bot.send_message(chat_id=update.message.chat_id, text='Value error') except IndexError: print('IndexError') bot.send_message(chat_id=update.message.chat_id, text='Usage: /graph <distance> <population>') except MyExceptions.NoGraphLoadedException as e: print(e.text) bot.send_message(chat_id=update.message.chat_id, text=e.text) except Exception as e: print(e) bot.send_message(chat_id=update.message.chat_id, text='No response')
def plotgraph(bot, update, args, user_data): try: # creació imatge del mapa i enviament al bot image_file = '_plotgraph.png' if len(args) == 1: if 'latitude' in user_data and 'longitude' in user_data: lat = user_data['latitude'] lon = user_data['longitude'] else: raise MyExceptions.LocationException elif len(args) == 3: lat, lon = args[1], args[2] else: raise IndexError() gbOp.checkUserData(user_data) image = gbOp.plotgraph(user_data['graph'], args[0], lat, lon) image.save(image_file) bot.send_photo(chat_id=update.message.chat_id, photo=open(image_file, 'rb')) os.remove(image_file) except ValueError: print('ValueError') bot.send_message(chat_id=update.message.chat_id, text='Value error') except IndexError: print('IndexError') bot.send_message(chat_id=update.message.chat_id, text='Usage: /plotgraph <dist> [<lat> <lon>]') except (MyExceptions.LocationException, MyExceptions.MapRenderException, MyExceptions.NoGraphLoadedException) as e: print(e.text) bot.send_message(chat_id=update.message.chat_id, text=e.text) except Exception as e: print(e) bot.send_message(chat_id=update.message.chat_id, text='No response')
def edges(bot, update, user_data): try: gbOp.checkUserData(user_data) n_edges = gbOp.edges(user_data['graph']) bot.send_message(chat_id=update.message.chat_id, text=n_edges) except MyExceptions.NoGraphLoadedException as e: print(e.text) bot.send_message(chat_id=update.message.chat_id, text=e.text) except Exception as e: print(e) bot.send_message(chat_id=update.message.chat_id, text='No response')
def start(bot, update, user_data): bot.send_message(chat_id=update.message.chat_id, text='Hello! Welcome to Graphbot v1.0') bot.send_message(chat_id=update.message.chat_id, text='Starting to download the data...') try: user_data['data'] = gbOp.getData() except Exception as e: print(e) bot.send_message(chat_id=update.message.chat_id, text='No response') bot.send_message(chat_id=update.message.chat_id, text='Data downloaded successfully') graph(bot, update, [300, 100000], user_data)