-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ __pycache__/ | |
/build/ | ||
cmake-build-debug/ | ||
/Testing/ | ||
|
||
# Telegram bot token | ||
/scripts/token.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import subprocess | ||
import telebot | ||
import os | ||
|
||
from src.utils import mkpath_root | ||
|
||
|
||
EXEC = mkpath_root('build/sdt-navigator') | ||
|
||
|
||
with open(mkpath_root('scripts/token.txt')) as file: | ||
tg_token = file.read().strip() | ||
|
||
bot = telebot.TeleBot(tg_token) | ||
|
||
SELECTED_DATASET = {} # {user_id: dataset_name} | ||
|
||
|
||
def run_command(dataset: str, command: str) -> str: | ||
print(f'Running command: {command}') | ||
result = "" | ||
|
||
try: | ||
executable = [EXEC, dataset] | ||
# print(f'Running executable: {executable}') | ||
process = subprocess.Popen( | ||
executable, | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True | ||
) | ||
|
||
# print('Waiting for response') | ||
stdout, stderr = process.communicate(command, timeout=10) | ||
# print('Response received') | ||
|
||
if process.returncode == 0: | ||
result = stdout.split('[sdt-navigator]$')[1].strip() | ||
else: | ||
result = f'Error occurred: {stderr}' | ||
|
||
process.kill() | ||
|
||
except Exception as e: | ||
result = f'Error occurred: {e}' | ||
|
||
return result | ||
|
||
|
||
@bot.message_handler(commands=['start']) | ||
def start_message(message): | ||
bot.send_message( | ||
message.chat.id, | ||
'Please select a dataset using /select command (e.g., `/select gtfs_hamburg`)', | ||
parse_mode='Markdown' | ||
) | ||
|
||
|
||
@bot.message_handler(commands=['select']) | ||
def select_dataset(message): | ||
user_input = message.text.strip().removeprefix('/select').strip() | ||
|
||
if not user_input: | ||
return start_message(message) | ||
|
||
if not os.path.isdir(mkpath_root(f'data/{user_input}')): | ||
bot.send_message( | ||
message.chat.id, | ||
'Dataset was not found. Please try again', | ||
) | ||
return start_message(message) | ||
|
||
SELECTED_DATASET[message.chat.id] = user_input | ||
bot.send_message( | ||
message.chat.id, | ||
f'Dataset \"{user_input}\" selected! You can now use /route or /search like this:' | ||
) | ||
bot.send_message( | ||
message.chat.id, | ||
f'```\n' | ||
f'/route --start \"Bremen Hbf\" --end \"Hamburg Airport (Flughafen)\" --date 2020-03-20 --time 06:00\n' | ||
f'/search Bremen\n' | ||
f'```', | ||
parse_mode='Markdown' | ||
) | ||
|
||
|
||
@bot.message_handler(commands=['route']) | ||
def route_command(message): | ||
if message.chat.id not in SELECTED_DATASET: | ||
return start_message(message) | ||
|
||
user_input = message.text.strip().removeprefix('/') | ||
|
||
bot.send_message( | ||
message.chat.id, | ||
run_command(SELECTED_DATASET[message.chat.id], user_input) | ||
) | ||
|
||
|
||
@bot.message_handler(commands=['search']) | ||
def search_command(message): | ||
if message.chat.id not in SELECTED_DATASET: | ||
return start_message(message) | ||
|
||
user_input = message.text.strip().removeprefix('/') | ||
|
||
bot.send_message( | ||
message.chat.id, | ||
run_command(SELECTED_DATASET[message.chat.id], user_input) | ||
) | ||
|
||
|
||
@bot.message_handler(func=lambda message: True) | ||
def handle_input(message): | ||
bot.send_message( | ||
message.chat.id, | ||
'Please use commands: /select, /route, /search' | ||
) | ||
|
||
|
||
print('Starting Telegram Bot...') | ||
bot.polling() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
orjson~=3.10.12 | ||
pandas~=2.2.3 | ||
pysimdjson~=6.0.2 | ||
pyTelegramBotAPI~=4.25.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/select - Select a dataset | ||
/route - Input a route query to find the best route between two stations | ||
/search - Search a station by it's approximate name | ||
|
||
|
||
route --start "Bremen Hbf" --end "Hamburg Airport (Flughafen)" --date 2020-03-20 --time 06:00 |