Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
D
domotikad
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
domotika
domotikad
Commits
5aec9707
Commit
5aec9707
authored
Dec 22, 2016
by
Franco (nextime) Lanza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Why pymessenger here?
parent
a02dc146
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
491 additions
and
0 deletions
+491
-0
__init__.py
domotika/clouds/pymessenger/__init__.py
+23
-0
bot.py
domotika/clouds/pymessenger/bot.py
+374
-0
graph_api.py
domotika/clouds/pymessenger/graph_api.py
+31
-0
receipt.py
domotika/clouds/pymessenger/receipt.py
+2
-0
user_profile.py
domotika/clouds/pymessenger/user_profile.py
+19
-0
utils.py
domotika/clouds/pymessenger/utils.py
+42
-0
No files found.
domotika/clouds/pymessenger/__init__.py
0 → 100644
View file @
5aec9707
import
json
import
six
from
.bot
import
Bot
class
Element
(
dict
):
__acceptable_keys
=
[
'title'
,
'item_url'
,
'image_url'
,
'subtitle'
,
'buttons'
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
if
six
.
PY2
:
kwargs
=
{
k
:
v
for
k
,
v
in
kwargs
.
iteritems
()
if
k
in
self
.
__acceptable_keys
}
else
:
kwargs
=
{
k
:
v
for
k
,
v
in
kwargs
.
items
()
if
k
in
self
.
__acceptable_keys
}
super
(
Element
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
to_json
(
self
):
return
json
.
dumps
({
k
:
v
for
k
,
v
in
self
.
iteritems
()
if
k
in
self
.
__acceptable_keys
})
class
Button
(
dict
):
# TODO: Decide if this should do more
pass
domotika/clouds/pymessenger/bot.py
0 → 100644
View file @
5aec9707
This diff is collapsed.
Click to expand it.
domotika/clouds/pymessenger/graph_api.py
0 → 100644
View file @
5aec9707
import
pymessenger.utils
as
utils
DEFAULT_API_VERSION
=
2.6
class
FacebookGraphApi
(
object
):
def
__init__
(
self
,
access_token
,
**
kwargs
):
'''
@required:
access_token
@optional:
api_version
app_secret
'''
self
.
api_version
=
kwargs
.
get
(
'api_version'
)
or
DEFAULT_API_VERSION
self
.
app_secret
=
kwargs
.
get
(
'app_secret'
)
self
.
graph_url
=
'https://graph.facebook.com/v{0}'
.
format
(
self
.
api_version
)
self
.
access_token
=
access_token
@
property
def
auth_args
(
self
):
if
not
hasattr
(
self
,
'_auth_args'
):
auth
=
{
'access_token'
:
self
.
access_token
}
if
self
.
app_secret
is
not
None
:
appsecret_proof
=
utils
.
generate_appsecret_proof
(
self
.
access_token
,
self
.
app_secret
)
auth
[
'appsecret_proof'
]
=
appsecret_proof
self
.
_auth_args
=
auth
return
self
.
_auth_args
domotika/clouds/pymessenger/receipt.py
0 → 100644
View file @
5aec9707
class
Receipt
:
pass
domotika/clouds/pymessenger/user_profile.py
0 → 100644
View file @
5aec9707
import
requests
from
pymessenger.graph_api
import
FacebookGraphApi
class
UserProfileApi
(
FacebookGraphApi
):
def
get
(
self
,
user_id
,
fields
=
None
):
params
=
{}
if
fields
is
not
None
and
isinstance
(
fields
,
(
list
,
tuple
)):
params
[
'fields'
]
=
","
.
join
(
fields
)
params
.
update
(
self
.
auth_args
)
request_endpoint
=
'{0}/{1}'
.
format
(
self
.
graph_url
,
user_id
)
response
=
requests
.
get
(
request_endpoint
,
params
=
params
)
if
response
.
status_code
==
200
:
user_profile
=
response
.
json
()
return
user_profile
return
None
domotika/clouds/pymessenger/utils.py
0 → 100644
View file @
5aec9707
import
hashlib
import
hmac
import
six
def
validate_hub_signature
(
app_secret
,
request_payload
,
hub_signature_header
):
"""
@inputs:
app_secret: Secret Key for application
request_payload: request body
hub_signature_header: X-Hub-Signature header sent with request
@outputs:
boolean indicated that hub signature is validated
"""
try
:
hash_method
,
hub_signature
=
hub_signature_header
.
split
(
'='
)
except
:
pass
else
:
digest_module
=
getattr
(
hashlib
,
hash_method
)
hmac_object
=
hmac
.
new
(
str
(
app_secret
),
unicode
(
request_payload
),
digest_module
)
generated_hash
=
hmac_object
.
hexdigest
()
if
hub_signature
==
generated_hash
:
return
True
return
False
def
generate_appsecret_proof
(
access_token
,
app_secret
):
"""
@inputs:
access_token: page access token
app_secret_token: app secret key
@outputs:
appsecret_proof: HMAC-SHA256 hash of page access token
using app_secret as the key
"""
if
six
.
PY2
:
hmac_object
=
hmac
.
new
(
str
(
app_secret
),
unicode
(
access_token
),
hashlib
.
sha256
)
else
:
hmac_object
=
hmac
.
new
(
bytearray
(
app_secret
,
'utf8'
),
str
(
access_token
)
.
encode
(
'utf8'
),
hashlib
.
sha256
)
generated_hash
=
hmac_object
.
hexdigest
()
return
generated_hash
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment