Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hookup #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions shiva/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .modules import Hawk

from .messengers import Telegram
from .messengers import VKBot

class Core:

Expand Down Expand Up @@ -37,3 +38,5 @@ def __init__(self, params):
self.scheduler = Scheduler(self)

self.telegram = Telegram(self.PARAMS['telegram_token'])

self.vk = VKBot()
22 changes: 22 additions & 0 deletions shiva/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .Core import Core
from .config import config
from aiohttp import web
import datetime
import json

Expand Down Expand Up @@ -44,8 +45,29 @@ async def tg_callback(request):

return {'text': 'OK'}

async def vk_callback(request):
"""
Web App function for processing callback
"""
try:
data = await request.json()
print(data)

if data['type'] == 'confirmation':
return {'text': '2bc41420'}

if data['type'] == 'message_new':
vk.send_photo(data['object']['user_id'], "E:\GitHubRep\shiva\shiva\messengers\\vk\ii5hpvRVF24.jpg")
except Exception as e:
print("Message process error: [%s]" % e)

return {'text': 'ok'}

telegram = core.telegram
telegram.createWebhook(config['host'])
core.server.hooks.add(telegram.URI, tg_callback)

vk = core.vk
core.server.hooks.add(vk.get_web_hook(), vk_callback)

core.server.run()
1 change: 1 addition & 0 deletions shiva/messengers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .telegram import Telegram
from .vk import VKBot
# from .vk import vk
22 changes: 0 additions & 22 deletions shiva/messengers/vk/SendMessage.py

This file was deleted.

23 changes: 13 additions & 10 deletions shiva/messengers/vk/VKAPIRequest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import requests
from messenger.vk.config import access_token


class VKRequest:

@staticmethod
def get_request_url(method, **kwargs):
def get_request_url(method, token, **kwargs):
attacments = ''

for arg in kwargs:
attacments += arg+'='+str(kwargs[arg])+"&"

attacments = attacments[0:-1]
url = """https://api.vk.com/method/{method}?{attacments}&access_token={access_token}&v=5.53\
""".replace(" ", "").format(method=method, attacments=attacments, access_token=access_token)
""".replace(" ", "").format(method=method, attacments=attacments, access_token=token)

return url

Expand All @@ -33,8 +32,8 @@ def post_request(url, files=None):
return q.json()

@staticmethod
def _api_upload_server_response(user_id):
url = VKRequest.get_request_url('photos.getMessagesUploadServer', peer_id=user_id)
def _api_upload_server_response(user_id, token):
url = VKRequest.get_request_url('photos.getMessagesUploadServer', token=token, peer_id=user_id)

q = VKRequest.get_request(url)

Expand All @@ -49,18 +48,22 @@ def _api_upload_photo(url, file_name):
return q

@staticmethod
def _api_save_photo(server, photo, hash):
url = VKRequest.get_request_url('photos.saveMessagesPhoto', server=server, photo=photo, hash=hash)
def _api_save_photo(server, photo, hash, token):
url = VKRequest.get_request_url('photos.saveMessagesPhoto', token=token, server=server, photo=photo, hash=hash)

q = VKRequest.get_request(url)

return q

@staticmethod
def get_upload_photos(user_id, file_name):
server_response = VKRequest._api_upload_server_response(user_id)
def get_upload_photos(user_id, file_name, token):
server_response = VKRequest._api_upload_server_response(user_id, token)
upload_response = VKRequest._api_upload_photo(server_response, file_name)

photo = upload_response['photo']
server = upload_response['server']
hash = upload_response['hash']

photo = VKRequest._api_save_photo(server, photo, hash)['response'][-1]
photo = VKRequest._api_save_photo(server, photo, hash, token)['response'][-1]

return photo
24 changes: 24 additions & 0 deletions shiva/messengers/vk/VKBot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from .VKAPIRequest import VKRequest
from .config import config


class VKBot:
def __init__(self):
self._token = config['vk_token']

@staticmethod
def get_web_hook():
return config['vk_webhook']

def send_text(self, user_id, text):
url = VKRequest.get_request_url(method='messages.send', token=self._token, message=text, user_id=user_id)
VKRequest.get_request(url)

def send_photo(self, user_id, file_name, message=''):
photo = VKRequest.get_upload_photos(user_id=user_id, file_name=file_name, token=self._token)
attachment = 'photo{owner_id}_{picture_id}'.format(owner_id=photo['owner_id'], picture_id=photo['id'])

url = VKRequest.get_request_url('messages.send', user_id=user_id, token=self._token,
message=message, attachment=attachment)

VKRequest.get_request(url)
33 changes: 1 addition & 32 deletions shiva/messengers/vk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1 @@
from aiohttp import web
import asyncio
import requests
from messenger.vk.config import *
from messenger.vk.SendMessage import *

PORT = 3000

async def vk_callback(request):
"""
Web App function for processing callback
"""
try:
data = await request.json()
print(data)
if data['object']['out'] == 0:
vk_send_photo(data['object']['user_id'], "E:\GitHubRep\shiva\messenger\\vk\ii5hpvRVF24.jpg" )
vk_send_text(data['object']['user_id'], "Hello")

except Exception as e:
print("Message process error: [%s]" % e)

return web.Response(text='ok')

async def main_page(request):
return web.Response(text='Shive is alive')

loop = asyncio.get_event_loop()
app = web.Application(loop=loop)
app.router.add_get('/', main_page)
app.router.add_post('/vk', vk_callback)
web.run_app(app, port=PORT)
from .VKBot import VKBot
4 changes: 4 additions & 0 deletions shiva/messengers/vk/config.sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config = {
'vk_token': '',
'vk_webhook': ''
}