Есть часть кода, напечатанного на питоне. Все работает, но иногда бот просто игнорирует сообщение, а после введения нового, начинает снова работать. Подскажите в чем дело? Может быть проблема с тем, как бот берет информацию с файла data?
import vk_api, random
import json
import time
from vk_api.longpoll import VkLongPoll, VkEventType
import threading
vk_session = vk_api.VkApi(token = "токен")
vk = vk_session.get_api()
longpol = VkLongPoll(vk_session)
def get_but(text, color):
return {
"action": {
"type": "text",
"payload": "{\"button\": \"" + "1" + "\"}",
"label": f"{text}"
},
"color": f"{color}"
}
keyboard = {
"one_time" : False,
"buttons" : [
[get_but('1', 'positive'), get_but('2', 'positive')],
]
}
keyboard = json.dumps(keyboard, ensure_ascii = False).encode('utf-8')
keyboard = str(keyboard.decode('utf-8'))
def get_but2(text, color):
return {
"action": {
"type": "text",
"payload": "{\"button\": \"" + "1" + "\"}",
"label": f"{text}"
},
"color": f"{color}"
}
keyboard2 = {
"one_time" : False,
"buttons" : [
[get_but2('назад', 'negative')],
]
}
keyboard2 = json.dumps(keyboard2, ensure_ascii = False).encode('utf-8')
keyboard2 = str(keyboard2.decode('utf-8'))
def sender(id, text):
vk_session.method('messages.send', {'user_id' : id, 'message' : text, 'random_id' : 0, 'keyboard' : keyboard})
def sender2(id, text):
vk_session.method('messages.send', {'user_id' : id, 'message' : text, 'random_id' : 0, 'keyboard' : keyboard2})
def send_stick(id, number):
vk.messages.send(user_id = id, sticker_id = number, random_id = 0)
def construct(id,msg):
p = {}
p["name"] = msg
data[str(id)] = p
return "normal"
def savebd():
with open("data.txt", "w") as file:
for i in data:
p = str(i) + " " +str(data[i]["name"])
file.write(p + '\n')
def loadbd():
file = open("data.txt","r")
datas= file.read()
datas = datas.splitlines()
file.close()
data = {}
for i in datas:
i = i.split()
if len(i)>1:
data[str(i[0])] = {}
data[str(i[0])]["name"] = i[1]
return(data)
data =loadbd()
def start():
while True:
try:
for event in longpol.listen():
if event.type == VkEventType.MESSAGE_NEW:
if event.to_me:
messages = vk_session.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
id = event.user_id
msg = event.text.lower()
n = 0
for i in data:
print(i)
if str(id) == i :
n = 1
if n == 0:
construct(id , msg)
data[str(id)]["name"]="4"
if msg == "1":
if "payload" in messages["items"][0]["last_message"]:
data[str(id)]["name"]="1"
sender2(id, "текст")
send_stick(id, 16975)
savebd()
except Exception as E:
time.sleep(1)
threading.Thread(target=start, args = []).start()