D
D
dr0zd02020-03-17 20:50:13
JavaScript
dr0zd0, 2020-03-17 20:50:13

How to output data in one line from a for loop?

There is this code:

for i in event.raw['object']['attachments']:
    attachments1 = i['type']
    try:
        attachments = attachments1 + str(i[attachments1]['owner_id']) + '_' + str(i[attachments1]['id']) + '_' + str(i[attachments1]['access_key']) + ','
    except:
        attachments = attachments1 + str(i[attachments1]['owner_id']) + '_' + str(i[attachments1]['id']) + ','
    vk.method('messages.send', {'peer_id': event.object.peer_id, 'message': event.object.text[5:], 'attachment': attachments, 'random_id': 0})

It should send multiple photos in one message, but it sends them one by one in different messages.

Question: how to make it send photos in one message?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
fuckingawesomenigga, 2019-08-15
@fuckingawesomenigga

var widthCol = 0;
var i = 0;
var flag = true;

while (flag) {
    i++;

    if ($("div").is('.col-' + i)) {
        $('.col-' + i).each(function(index, el) {
            var temp = $(this).innerWidth();
            if (temp > widthCol) {
                widthCol = temp;
            }
        });

        $('.col-' + i).css({
            width: widthCol
        });

        widthCol = 0;
    }else{
    	flag = false;
    }
}

T
truenotnamed, 2020-03-17
@dr0zd0

Sending a message inside a loop, which is why each attachment is sent separately in each message. As I understand it, it is necessary to send all attachments from the event, then we do this:

attachments = ""
for i in event.raw['object']['attachments']:
    attachments1 = i['type']
    try:
        attach = attachments1 + str(i[attachments1]['owner_id']) + '_' + str(i[attachments1]['id']) + '_' + str(i[attachments1]['access_key']) + ','
        attachments += attach
    except:
        attach = attachments1 + str(i[attachments1]['owner_id']) + '_' + str(i[attachments1]['id']) + ','
        attachments += attach
vk.method('messages.send', {'peer_id': event.object.peer_id, 'message': event.object.text[5:], 'attachment': attachments, 'random_id': 0})

Or like this:
attachments = []
eventAttachments = event.raw['object']['attachments']
for attachment in eventAttachments:
    attach = "%s%s_%s" % (attachment['type'], attachment['owner_id'], attachment['id'])
    if 'access_key' in attachment:
        attach += "_%s" % attachment['access_key']
    attachments.append(attach)
vk.method('messages.send', {'peer_id': event.object.peer_id, 'message': event.object.text[5:], 'attachment': ','.join(attachments), 'random_id': 0})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question