U
U
uyrsr2021-02-07 01:36:14
Python
uyrsr, 2021-02-07 01:36:14

When the VK bot sends Russian characters, it replaces them with random Latin characters, how to fix it?

The bot should send annecdotes once an hour, and it takes annecdotes from a txt file. Here's what's in the txt file: 601f189646a79651501908.jpeg
Here's what the bot sends: ?4??4??4??4??4??4??1? ?4??4??4??4??1? one . Here is the code:

import vk_api
import time
from vk_api import VkUpload
from vk_api.longpoll import VkLongPoll, VkEventType

global an1
global an2
global an3
global score 


vk_session = vk_api.VkApi(token = '')
longpoll = VkLongPoll(vk_session)
upload = VkUpload(vk_session)

def sender(id, text):
    vk_session.method('messages.send', {'chat_id' : id, 'message' : text, 'random_id' : 0,})

for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW:
        if event.to_me:
            if event.from_chat:
                msg = event.text.lower()
                id = event.chat_id
                if msg == 'привет':
                    while True: 
                        score = 0
                        score += 1
                        an1 = open('C:/матвей/Вкбот/anekdot1.txt', 'rb')
                        an2 = open('C:/матвей/Вкбот/anekdot2.txt', 'rb')
                        an3 = open('C:/матвей/Вкбот/anekdot3.txt', 'rb')
                        sender(id, an1)
                        time.sleep(30)
                        if score == 1:
                            sender(id, an2)
                            score += 1
                            time.sleep(30)
                            if score == 2: 
                                sender(id, an3)
                                time.sleep(30)

No idea how to fix it please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_, 2021-02-07
@mrxor

Your sender expects in the text parameter - the string to be sent.
You feed it a file object instead of a string .
After opening the file, you need to read the data from it and then feed it to the sender.
How to open a file and read text from it, see here and here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question