I have a problem with python. I'm creating a telegram bot that returns the bus stop times of some cities, using a library called Telepot (you can find the documentation on google).
The intention of the bot is to ask the city of departure, the departure stop and then the city and the arrival stop. When the bot asks for the departure city and the departure stop is all right, but when it asks for the city of arrival, the bot starts all over again. I have identified the problem but I don't know how to solve it. I tried to change the indentations and duplicate the dictionary but nothing.
And finally I would like to store all the choices of cities / stops in lists to be used later.
import time
import botogram
import json
import sqlite3
result = {}
conntta = sqlite3.connect("Database.db")
cursortta = conntta.cursor()
sqltta = cursortta.execute("select np,id from orari_andata")
result = {}
resultr = {}
for np, id in sqltta:
if np in result:
result[np].append(id)
else:
result[np] = [id]
for np, id in sqltta:
if np in resultr:
resultr[np].append(id)
else:
resultr[np] = [id]
result2 = result.copy()
for i in result:
keyboard_locpo = {"keyboard": [[{"text": i} for i in pair] for pair in zip(result)]}
keyboard_selection = {"keyboard": [[{"text": "Cerca fermata ora"}],
[{"text": "Pianifica Viaggio"}]]}
bot = telepot.create("token")
keyboard_selection = {"keyboard": [[{"text": "Cerca fermata ora"}],
[{"text": "Pianifica Viaggio"}]]}
city_dep = []
stop_dep = []
city_arr = []
stop_arr = []
#result is a dictionary with all the cities and their corresponding stops es. {New York: bronx, china town, manhattan}
def handle(msg):
step = 0
content_type, chat_type, chat_id = telepot.glance(msg)
text = msg['text']
if text == "/start":
bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)
if text == "Cerca fermata ora":
bot.sendMessage(chat_id, text = "Seleziona la località in cui ti trovi", reply_markup=keyboard_locpo)
content_type, chat_type, chat_id = telepot.glance(msg)
for i in result:
if text == i:
city_dep.append(i)
print(city_dep)
for t in result[i]:
keyboard_stops = {"keyboard": [[{"text": t} for t in pair] for pair in zip(result[i])]}
bot.sendMessage(chat_id, text = "Seleziona la fermata in cui ti trovi", reply_markup=keyboard_stops)
for t in result[i]:
if text == t:
stop_dep.append(t)
print(stop_dep)
bot.sendMessage(chat_id, text = "Seleziona la località che vuoi raggiungere", reply_markup=keyboard_locpo)
for y in result:
if text == y:
city_arr.append(y)
print(city_arr)
for x in result[y]:
keyboard_stops_1 = {"keyboard": [[{"text": x} for x in pair] for pair in zip(result[y])]}
bot.sendMessage(chat_id, text = "Seleziona la fermata che vuoi raggiungere", reply_markup=keyboard_stops_1)
for x in result[y]:
if text == x:
stop_arr.append(x)
print(stop_arr)
MessageLoop(bot, handle).run_as_thread()
while 1:
time.sleep(10)