我有 python 的问题。我正在创建一个电报机器人,它使用一个名为 Telepot 的库(你可以在 google 上找到文档)返回一些城市的公交车站时间。
机器人的意图是询问出发城市、出发站点,然后询问城市和到达站点。当 bot 询问出发城市,出发站就可以了,但是当它询问到达城市时,bot 又重新开始。我已经确定了问题,但我不知道如何解决它。我试图更改缩进并复制字典但没有。
最后,我想将所有城市/站点的选择存储在列表中以供以后使用。
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)