Skip to content

Instantly share code, notes, and snippets.

@Zulcom
Last active January 10, 2018 13:47
Show Gist options
  • Save Zulcom/b5ffd44135aff07dccd2e0e1feeff922 to your computer and use it in GitHub Desktop.
Save Zulcom/b5ffd44135aff07dccd2e0e1feeff922 to your computer and use it in GitHub Desktop.
Export VK Group post to facebook page. Экспорт из группы вк в фейсбук. Webhook. Вебхук. Python. Heroku

Setup Heroku

create account on heroku
install heroku cli
after fill script with your token and page id do in folder:

git add .  
git commit -m "init"  
heroku login  
heroku create  
git push heroku master  

get link for your app from heroku

Setup Vk

Go to group in vk
select API usage
select Callback API
Fill address with link from heroku (add in end of link '/vk/callback')
get confirm code from vk
at 15 line add

return "<your confirm code>" 

After that update code on heroku:

git add.  
git commit -m "confirm"  
git push heroku master  

click confirm on vk
remove 15 line

git add.  
git commit -m "work mode"  
git push heroku master  

Enjoy.

import os
import gunicorn
from flask import Flask
app = Flask(__name__)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=false)
from flask import request
from flask import Flask
import traceback
from facepy import GraphAPI
app = Flask(__name__)
graph = GraphAPI(
'YOUR API TOKEN')
#https://stackoverflow.com/questions/17197970/facebook-permanent-page-access-token
@app.route('/vk/callback', methods=['POST'])
@app.route('/vk/callback')
def hander():
data = request.json['object']
url = ""
text = ""
if data['post_type'] != 'post':
return "ok"
if data['text'] != '':
text = data['text']
if 'attachments' in data:
for attachment in data['attachments']:
if attachment['type'] == 'photo':
photo = attachment['photo']
if 'photo_75' in photo:
url = photo['photo_75']
if 'photo_130' in photo:
url = photo['photo_130']
if 'photo_604' in photo:
url = photo['photo_604']
if 'photo_1280' in photo:
url = photo['photo_1280']
if 'photo_2560' in photo:
url = photo['photo_2560']
try:
if not url:
graph.post(path=str('your page id') + '/feed', message=text)
else:
graph.post(path=str('your page id') + '/feed', message=text, link=url)
except Exception as e:
# exception handler
return "ok"
web: gunicorn -b 0.0.0.0:$PORT main:app
certifi==2017.11.5
chardet==3.0.4
click==6.7
facepy==1.0.9
Flask==0.12.2
gunicorn==19.7.1
idna==2.6
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pushbullet.py==0.11.0
python-magic==0.4.15
requests==2.18.4
six==1.11.0
urllib3==1.22
websocket-client==0.46.0
Werkzeug==0.14.1
python-3.6.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment