├── runtime.txt ├── Procfile ├── Procfile.windows ├── botinfo.py ├── requirements.txt ├── README.md └── SUTDtoSimei.py /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.6.6 2 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: python SUTDtoSimei.py -------------------------------------------------------------------------------- /Procfile.windows: -------------------------------------------------------------------------------- 1 | web:python SUTDtoSimei.py 2 | -------------------------------------------------------------------------------- /botinfo.py: -------------------------------------------------------------------------------- 1 | TOKEN = "593199969:AAF8zuSnfCig5C3QNwy_ItERMVQZEZxEjoo" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python_telegram_bot==10.0.1 2 | requests>=2.20.0 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a simple Telegram bot that checks bus timings to go to Simei from SUTD. 2 | 3 | It's purpose is to help people in SUTD decide which bus stop to go to in order to go to Simei, without having to use other bus timing applications to check two bus stops. 4 | 5 | The bot can be found at t.me/sutdtosimei_bot 6 | -------------------------------------------------------------------------------- /SUTDtoSimei.py: -------------------------------------------------------------------------------- 1 | ############################################################################# 2 | # This bot determines the optimal bus to take when going to Simei from SUTD # 3 | ############################################################################# 4 | 5 | import requests 6 | import json 7 | import math 8 | import telegram 9 | import botinfo 10 | import os 11 | from telegram.ext import Updater, CommandHandler, CallbackQueryHandler 12 | 13 | #initialization of variables stored in seperate file for security reasons 14 | TOKEN = botinfo.TOKEN 15 | 16 | #initialization of stuff for the bot to run 17 | PORT = int(os.environ.get('PORT', '5000')) 18 | bot = telegram.Bot(token = TOKEN) 19 | bot.setWebhook(url = "https://sutdtosimei-bot.herokuapp.com/" + TOKEN) 20 | updater = Updater(token = TOKEN) 21 | dispatcher = updater.dispatcher 22 | 23 | #This function queries an API to get the arrival time of the next bus 5 at the SUTD bus stop 24 | #Returns the arrival time in minutes 25 | def get_bus5_time(): 26 | r = requests.get("https://arrivelah.herokuapp.com/?id=96049") 27 | data = r.text 28 | data = json.loads(data) 29 | services = data['services'] 30 | for service in services: 31 | if service["no"] == "5": 32 | service5 = service 33 | 34 | next_bus5_time_ms = service5['next']['duration_ms'] 35 | subsq_bus5_time_ms = service5['subsequent']['duration_ms'] 36 | next_bus5_time_min = math.floor(next_bus5_time_ms/60/1000) 37 | subsq_bus5_time_min = math.floor(subsq_bus5_time_ms/60/1000) 38 | 39 | if next_bus5_time_min < 0: 40 | next_bus5_time_min = 0 41 | if subsq_bus5_time_min < 0: 42 | subsq_bus5_time_min = 0 43 | 44 | return (next_bus5_time_min, subsq_bus5_time_min) 45 | 46 | #This function queries an API to get the arrival time of the next bus 20 at the SUTD bus stop 47 | #Returns the arrival time in minutes 48 | def get_bus20_time(): 49 | r = requests.get("https://arrivelah.herokuapp.com/?id=96441") 50 | data = r.text 51 | data = json.loads(data) 52 | service20 = data['services'][0] 53 | 54 | next_bus20_time_ms = service20['next']['duration_ms'] 55 | subsq_bus20_time_ms = service20['subsequent']['duration_ms'] 56 | next_bus20_time_min = math.floor(next_bus20_time_ms/60/1000) 57 | subsq_bus20_time_min = math.floor(subsq_bus20_time_ms/60/1000) 58 | 59 | if next_bus20_time_min < 0: 60 | next_bus20_time_min = 0 61 | if subsq_bus20_time_min < 0: 62 | subsq_bus20_time_min = 0 63 | 64 | return (next_bus20_time_min, subsq_bus20_time_min) 65 | 66 | #Function executed when user calls start command, gives the user some info about what the bot does 67 | def start(bot,update): 68 | bot.sendMessage(chat_id = update.message.chat_id, 69 | text = "Hello %s! I will help you check the arrival times of bus 20 and bus 5 going towards Simei from SUTD.\n" 70 | "To get the bus timings, just type /gosimei."%update.message.from_user.first_name) 71 | 72 | dispatcher.add_handler(CommandHandler('start', start)) 73 | 74 | #This function compares the time needed for bus 5 and bus 20 to come, and returns appropriate advice for the user 75 | def getAdvice(bus5_time, bus20_time): 76 | if bus5_time < 3 and bus20_time < 3: 77 | return "Seems like both buses are coming soon, better hurry to whichever bus stop is nearer!" 78 | 79 | if bus5_time < 3: 80 | return "Seems like bus 5 is coming soon, better hurry!" 81 | 82 | if bus20_time < 3: 83 | return "Seems like bus 20 is coming soon, better hurry!" 84 | 85 | if bus20_time < bus5_time: 86 | faster_bus = "bus 20" 87 | 88 | else: 89 | faster_bus = "bus 5" 90 | 91 | return "Seems like %s is coming sooner, guess you should go for that one."%faster_bus 92 | 93 | 94 | #Function that checks both the bus 5 and 20 timings and sends them to the user 95 | def goSimei(bot,update): 96 | sent = bot.sendMessage(chat_id = update.message.chat_id, 97 | text = "Hold on, checking the timings now") 98 | bus5_time, subsq_bus5_time = get_bus5_time(); 99 | bus20_time, subsq_bus20_time = get_bus20_time(); 100 | advice = getAdvice(bus5_time, bus20_time) 101 | bot.editMessageText(chat_id = update.message.chat_id, 102 | message_id = sent['message_id'], 103 | text = "Bus 5 is arriving in %d minutes.\n" 104 | "Bus 20 is arriving in %d minutes.\n" 105 | "%s\n\n" 106 | "The subsequent bus 5 is arriving in %d minutes.\n" 107 | "The subsequent bus 20 is arriving in %d minutes."%(bus5_time,bus20_time,advice,subsq_bus5_time, subsq_bus20_time)) 108 | 109 | dispatcher.add_handler(CommandHandler('gosimei', goSimei)) 110 | 111 | updater.start_webhook(listen="0.0.0.0", 112 | port=PORT, 113 | url_path=TOKEN) 114 | updater.bot.setWebhook("https://sutdtosimei-bot.herokuapp.com/" + TOKEN) 115 | updater.idle() 116 | 117 | 118 | --------------------------------------------------------------------------------