S
S
ShamiLLL2020-12-22 18:11:23
Python
ShamiLLL, 2020-12-22 18:11:23

Why is the bot sometimes silent?

There is a piece of code printed in python. Everything works, but sometimes the bot simply ignores the message, and after introducing a new one, it starts working again. Tell me what's the matter? Maybe there is a problem with how the bot takes information from the data file?

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()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2020-12-22
@dimonchik2013

he thinks

S
soremix, 2020-12-22
@SoreMix

If you already use try ... except, then output the exception that has arisen.
At least you will find out what is wrong

except Exception as E:
  print(e)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question