python \.py
Long Polling is a technology that allows you to receive data about new events using the "long requests". The server receives the request, but sends a response to it not immediately, but only when a certain event occurs (for example, a new message arrives), or the specified timeout expires.Speaking Russian, we send a request to the server, and he, in turn, pokes at VKontakte if something happens there, for example, a message comes to us and he runs and tells us about it. From this and we will dance.
import vk_api import requests session = requests.Session() login, password = ' , email ', ' ' vk_session = vk_api.VkApi(login, password) try: vk_session.auth(token_only=True) except vk_api.AuthError as error_msg: print(error_msg) return
import requests import vk_api vk_session = vk_api.VkApi(token=' ')
from vk_api.longpoll import VkLongPoll, VkEventType longpoll = VkLongPoll(vk_session) vk = vk_session.get_api() for event in longpoll.listen(): if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text: # longpoll, : if event.text == ' ' or event.text == ' ': # if event.from_user: # vk.messages.send( # user_id=event.user_id, message=' ' ) elif event.from_chat: # vk.messages.send( # chat_id=event.chat_id, message=' ' )
import datetime vk.messages.send( user_id=event.user_id, message=' : ' + str(now.strftime("%H:%M")) )
attachments = [] from vk_api import VkUpload upload = VkUpload(vk_session) image_url = ' ' image = session.get(image_url, stream=True) photo = upload.photo_messages(photos=image.raw)[0] attachments.append( 'photo{}_{}'.format(photo['owner_id'], photo['id']) ) vk.messages.send( user_id=event.user_id, attachment=','.join(attachments), message=' ' )
image_url = 'http://.com/uploads/' + event.text + '.png'
import wikipedia # wikipedia.set_lang("RU") if event.text == '' or event.text == '' or event.text == '' or event.text == '' or event.text == 'Wikipedia' or event.text == 'wikipedia' or event.text == 'Wiki' or event.text == 'wiki': # ... wiki if event.from_user: # KC vk.messages.send( user_id=event.user_id, message=' ' # " " ) elif event.from_chat: # vk.messages.send( chat_id=event.chat_id, message=' ' # " " ) for event in longpoll.listen(): if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text: # longpoll if event.from_user: vk.messages.send( # user_id=event.user_id, message=' : \n' + str(wikipedia.summary(event.text)) # " " api Wikipedia ) break # elif event.from_chat: # vk.messages.send( chat_id=event.chat_id, message=' : \n' + str(wikipedia.summary(event.text)) # " " api Wikipedia ) break # continue
Source: https://habr.com/ru/post/428507/