-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
62 lines (52 loc) · 1.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os, sys
from flask import Flask
from flask_hookserver import Hooks
import tweepy
# encoding=utf8
reload(sys)
sys.setdefaultencoding('utf8')
twitter_consumer_key = os.environ['OSII_BOT_CONSUMER_KEY']
twitter_consumer_secret = os.environ['OSII_BOT_CONSUMER_SECRET']
twitter_access_token = os.environ['OSII_BOT_ACCESS_TOKEN']
twitter_access_token_secret = os.environ['OSII_BOT_ACCESS_TOKEN_SECRET']
github_webhooks_secret = os.environ['OSII_BOT_WEBHOOKS_SECRET']
# flask app
app = Flask(__name__)
# twitter auth by tweepy
twitter_auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
twitter_auth.set_access_token(twitter_access_token, twitter_access_token_secret)
twitter_api = tweepy.API(twitter_auth)
# github webhooks & request checking options
app.config['GITHUB_WEBHOOKS_KEY'] = github_webhooks_secret
app.config['VALIDATE_IP'] = False
app.config['VALIDATE_SIGNATURE'] = True
hooks = Hooks(app, url='/webhooks')
def shorten(text, length):
"""Truncate the text if it is too long.
"""
if len(text) > length:
pos = text.rfind(' ', 0, length - 2)
if pos == -1:
pos = length - 3
return text[:pos] + "..."
else:
return text
# flask routes
@app.route('/')
def hello():
return '<a href="{0}">{0}</a>'.format('https://github.com/open-source-ideas')
@hooks.hook('issues')
def issues(data, delivery):
twitter_update_status = 'This hook is not implemented'
# Tweet only newly created issues
if (data['action'] == 'opened'):
tweet_max_len = 278
title = 'New open source idea!'
url = data['issue']['html_url']
# URL in tweet always counts for 23 characters
desc = shorten(data['issue']['title'], tweet_max_len - len(title) - 23)
tweet = '{} {} {}'.format(title, desc, url)
twitter_update_status = twitter_api.update_status(tweet)
return str(twitter_update_status)
# app running on c9.io
# app.run(host=os.getenv('IP', '0.0.0.0'),port=int(os.getenv('PORT', 8080)))