Fixed attachments preview and .doc(x) duplicating

This commit is contained in:
Kylmakalle 2017-06-25 15:34:44 +03:00
parent 533213f969
commit 44bca45a38
2 changed files with 49 additions and 40 deletions

2
bot.py
View File

@ -81,7 +81,7 @@ def verifycode(code):
def info_extractor(info):
info = info[0].url[8:-1].split('.')
info = info[-1].url[8:-1].split('.')
return info

View File

@ -32,13 +32,13 @@ class VkPolling:
def handle_messages(m, vk_user, bot, chat_id, mainmessage=None):
user = vk.API(vk_user.session).users.get(user_ids=m["uid"], fields=[])[0]
if 'body' in m and not 'attachment' in m and not 'geo' in m and not 'fwd_messages' in m:
data = add_reply_info(m, user["first_name"], user["last_name"])
data = add_user_info(m, user["first_name"], user["last_name"])[:-1] + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
if 'attachment' in m:
attachment_handler(m, user, bot, chat_id, mainmessage)
if 'geo' in m:
data = add_reply_info(m, user["first_name"], user["last_name"])
data = add_user_info(m, user["first_name"], user["last_name"]) + add_reply_info(m)
geo = bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
bot.send_venue(chat_id, m['geo']['coordinates'].split(' ')[0], m['geo']['coordinates'].split(' ')[1],
@ -46,7 +46,8 @@ def handle_messages(m, vk_user, bot, chat_id, mainmessage=None):
disable_notification=check_notification(m),
reply_to_message_id=geo.message_id).wait()
if 'fwd_messages' in m:
data = add_reply_info(m, user["first_name"], user["last_name"]) + '<i>Пересланные сообщения</i>'
data = add_user_info(m, user["first_name"],
user["last_name"]) + '<i>Пересланные сообщения</i>' + add_reply_info(m)
reply = bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m),
reply_to_message_id=mainmessage).wait().message_id
@ -63,22 +64,23 @@ def handle_updates(vk_user, bot, chat_id, updates):
def attachment_handler(m, user, bot, chat_id, mainmessage=None):
if m['attachment']['type'] == 'photo':
for photo in m['attachments']:
data = add_reply_info(m, user['first_name'], user['last_name']) + '<a href="{}">Фото</a>'.format(
get_max_src(photo['photo']))
data = add_user_info(m, user['first_name'], user['last_name']) + '<a href="{}">Фото</a>'.format(
get_max_src(photo['photo'])) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
if m['attachment']['type'] == 'video':
for vid in m['attachments']:
link = 'https://vk.com/video{}_{}'.format(vid['video']['owner_id'],
vid['video']['vid'])
data = add_reply_info(m, user['first_name'], user['last_name']) + '<a href="{}">Видео</a>'.format(link)
data = add_user_info(m, user['first_name'], user['last_name']) + '<a href="{}">Видео</a>'.format(
link) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
if m['attachment']['type'] == 'audio':
for audio in m['attachments']:
data = add_reply_info(m, user['first_name'], user['last_name']) + '🎵 <code>{} - {}</code>'.format(
data = add_user_info(m, user['first_name'], user['last_name']) + '🎵 <code>{} - {}</code>'.format(
audio['audio']['artist'],
audio['audio']['title'])
audio['audio']['title']) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
if m['attachment']['type'] == 'doc':
@ -86,8 +88,8 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None):
if doc['doc']['ext'] == 'gif':
try:
link = doc['doc']['url']
data = add_reply_info(m, user["first_name"], user["last_name"]) + '<a href="{}">GIF</a>'.format(
link)
data = add_user_info(m, user["first_name"], user["last_name"]) + '<a href="{}">GIF</a>'.format(
link) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
except:
@ -96,9 +98,9 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None):
elif doc['doc']['ext'] == 'pdf' or doc['doc']['ext'] == 'zip':
try:
link = doc['doc']['url']
data = add_reply_info(m, user["first_name"],
data = add_user_info(m, user["first_name"],
user["last_name"], ) + '<a href="{}">Документ</a>'.format(
link)
link) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
except:
@ -107,7 +109,8 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None):
elif doc['doc']['ext'] == 'jpg' or doc['doc']['ext'] == 'png':
try:
link = doc['doc']['url']
data = add_reply_info(m, user["first_name"], user["last_name"], ) + '<i>Документ</i>'
data = add_user_info(m, user["first_name"],
user["last_name"], ) + '<i>Документ</i>' + add_reply_info(m)
notification = bot.send_message(chat_id, data, parse_mode='HTML',
disable_notification=check_notification(m),
reply_to_message_id=mainmessage).wait()
@ -121,8 +124,8 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None):
elif doc['doc']['ext'] == 'ogg':
try:
link = doc['doc']['url']
data = add_reply_info(m, user["first_name"], user["last_name"], ) + \
'<a href="{}">Аудио</a>'.format(link)
data = add_user_info(m, user["first_name"], user["last_name"], ) + \
'<a href="{}">Аудио</a>'.format(link) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
except:
@ -130,37 +133,46 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None):
elif doc['doc']['ext'] == 'doc' or doc['doc']['ext'] == 'docx':
try:
data = add_reply_info(m, user["first_name"], user["last_name"], ) + '<i>Документ</i>'
data = add_user_info(m, user["first_name"],
user["last_name"], ) + '<i>Документ</i>' + add_reply_info(m)
notification = bot.send_message(chat_id, data, parse_mode='HTML',
disable_notification=check_notification(m),
reply_to_message_id=mainmessage).wait()
uploading = bot.send_chat_action(chat_id, 'upload_document')
file = wget.download(requests.get(doc['doc']['url']).url)
bot.send_document(chat_id, open(file, 'rb'),
openedfile = open(file, 'rb')
bot.send_document(chat_id, openedfile,
reply_to_message_id=notification.message_id,
disable_notification=check_notification(m)).wait()
uploading.wait()
file.close()
openedfile.close()
os.remove(file)
except:
send_doc_link(doc, m, user, bot, chat_id, mainmessage)
else:
send_doc_link(doc, m, user, bot, chat_id, mainmessage)
if m['attachment']['type'] == 'sticker':
link = m['attachment']['sticker']['photo_512']
data = add_reply_info(m, user["first_name"], user["last_name"], ) + '<a href="{}">Стикер</a>'.format(link)
data = add_user_info(m, user["first_name"], user["last_name"]) + '<a href="{}">Стикер</a>'.format(
link) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
# TODO: Wall Posts and comments
def check_expansion(document):
print(document['doc']['title'].split('.'))
if len(document['doc']['title'].split('.')) - 1:
return document['doc']['title']
else:
return document['doc']['title'] + '.' + document['doc']['ext']
def send_doc_link(doc, m, user, bot, chat_id, mainmessage=None):
link = doc['doc']['url']
data = add_reply_info(m, user["first_name"], user["last_name"]) + \
'<i>Документ</i>\n<a href="{}">{}</a>'.format(link,
doc['doc']['title'] + '.' + doc['doc']['ext'])
data = add_user_info(m, user["first_name"], user["last_name"]) + \
'<i>Документ</i>\n<a href="{}">{}</a>'.format(link, check_expansion(doc)) + add_reply_info(m)
bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False,
disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait()
@ -172,27 +184,24 @@ def check_forward_id(msg):
return None
def add_reply_info(m, first_name, last_name):
def add_reply_info(m):
if 'chat_id' in m:
return '<a href="x{}.{}.{}">&#8203;</a>'.format(m['uid'], m['chat_id'], check_forward_id(m))
else:
return '<a href="x{}.{}.0">&#8203;</a>'.format(m['uid'], check_forward_id(m))
def add_user_info(m, first_name, last_name):
if m['body']:
if 'chat_id' in m:
return '<a href="x{}.{}.{}">&#8203;</a><b>{} {} @ {}:</b>\n{}\n'.format(m['uid'], m['chat_id'],
check_forward_id(m),
first_name,
last_name, m['title'],
m['body'].replace('<br>', '\n'))
return '<b>{} {} @ {}:</b>\n{}\n'.format(first_name, last_name, m['title'], m['body'].replace('<br>', '\n'))
else:
return '<a href="x{}.{}.0">&#8203;</a><b>{} {}:</b>\n{}\n'.format(m['uid'], check_forward_id(m), first_name,
last_name,
m['body'].replace('<br>', '\n'))
return '<b>{} {}:</b>\n{}\n'.format(first_name, last_name, m['body'].replace('<br>', '\n'))
else:
if 'chat_id' in m:
return '<a href="x{}.{}.{}">&#8203;</a><b>{} {} @ {}:</b>\n'.format(m['uid'], m['chat_id'],
check_forward_id(m),
first_name,
last_name, m['title'])
return '<b>{} {} @ {}:</b>\n'.format(first_name, last_name, m['title'])
else:
return '<a href="x{}.{}.0">&#8203;</a><b>{} {}:</b>\n'.format(m['uid'], check_forward_id(m), first_name,
last_name)
return '<b>{} {}:</b>\n'.format(first_name, last_name)
def check_notification(value):