Skip to content

Commit

Permalink
Add VK adapter - fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
sevazhidkov committed Mar 24, 2016
1 parent 6d0af9e commit 763f875
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
77 changes: 77 additions & 0 deletions adapters/vk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
# Adapter for receiving and sending messages by
# Telegram Bot API
name: telegram
description: "Adapter for connecting to Telegram"
config:
LEONARD_TELEGRAM_TOKEN: ''
"""

import time
import json
import requests
from leonard.adapter import IncomingMessage
from leonard.utils import logger

VK_API_URL = "https://api.vk.com/method/{}?v=5.41"


def get_messages(bot):
token = bot.config.get('LEONARD_VK_TOKEN')
# Get last message id
url = VK_API_URL.format('messages.get')
response = requests.get(url, params={'access_token': token,
'count': 1})
message_data = json.loads(response.text)['response']['items'][0]
last_message_id = message_data['id']
while True:
response = requests.get(url, params={'access_token': token,
'count': 50,
'last_message_id': last_message_id})
messages = json.loads(response.text)['response']['items']
for message in messages:
if 'body' in message:
message_text = message['body']
else:
message_text = ''
vk_id = message['user_id']
# Prepare variables to save it in DB
variables = {
'last_message': message,
'adapter': 'vk'
}
# If user send localition, save it to message object and
# DB variables to use location later
if 'geo' in message:
location = list(map(float, message['geo']['coordinates'].split()))
variables['location'] = location
else:
location = None
# Leonard iterating with message with 'for in', so our function
# is generator of IncomingMessage objects.
yield IncomingMessage(
adapter_id='vk' + str(message['user_id']),
text=message_text,
attachments=[],
location=location,
variables=variables
)
last_message_id = message['id']
time.sleep(5)


def send_message(message, bot):
token = bot.config.get('LEONARD_VK_TOKEN')
url = VK_API_URL.format('messages.send')
params = {
'access_token': token,
'message': message.text,
'peer_id': message.recipient.data['adapter_id'].lstrip('vk'),
'notification': 1
}

if message.location:
params['lat'] = message.location[0]
params['long'] = message.location[1]

response = requests.get(url, params=params)
2 changes: 1 addition & 1 deletion leonard/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def utc():
return (now - datetime.datetime(1970, 1, 1)).total_seconds()


def split_message(message_text, max_length=500):
def split_message(message_text, max_length=5000):
"""
Split message by paragraphs if it more than max_length symbols
Expand Down
8 changes: 6 additions & 2 deletions leonard/utils/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ def track_message(message, adapter, plugin, bot):
"""
Track message in Botan.io
:param message: dict, message from telegram
:param message: dict, raw message from adapter
:param adapter: str, adapter name
:param plugin: str, name of plugin that proceed that message
:param bot: Leonard object
"""
message['adapter'] = adapter
if adapter == 'telegram':
user_id = message['from']['id']
elif adapter == 'vk':
user_id = message['user_id']
url = BOTAN_URL.format(token=bot.config.get('LEONARD_BOTAN_TOKEN'),
uid=message['from']['id'], name=plugin)
uid=user_id, name=plugin)
try:
response = requests.post(
url, data=json.dumps(message),
Expand Down
2 changes: 2 additions & 0 deletions plugins/wikihow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
priority: 100
"""

import time
import json
import requests
import leonard
Expand Down Expand Up @@ -64,6 +65,7 @@ def article_message(message, bot):
variables={"telegram_hide_preview": True}
)
bot.send_message(answer)
time.sleep(0.3)
answer = leonard.OutgoingMessage(
recipient=message.sender,
text=message.locale.source.format(url.rstrip('&action=raw')),
Expand Down

0 comments on commit 763f875

Please sign in to comment.