[PYTHON] Link Hatena Bookmark and Yammer

There is an information sharing tool called Yammer.

If I wanted to share the article I was reading in Pocket at work, I used the IFTTT recipe to tag it on Pocket and post it to Yammer.

ifttt

This was convenient, but IFTTT will stop supporting Yammer, and I've always wanted to link it with Hatena Bookmark, so I took this opportunity to make my own.

Advance preparation

Preparing the server

The Yammer API has a Python SDK, so I'll use that. This time, I decided to run Python with Sakura Internet's 500 yen plan. The point is to add --user in setup.py.

 $  wget https://github.com/yammer/yam-python/archive/master.zip
 $  cd yam-python-master/
 $  python setup.py install --user

Register the app with Yammer

After confirming the operation of the library, register the application name and redirect URL referring to this site, and obtain the client_id and client_secret. This time, we will use Sakura Internet, so set redirect_uri to http://xxxx.sakura.ne.jp/yammer/redirect.cgi.

https://developer.yammer.com/docs/app-registration

WebHook registration with Hatena Bookmark

Next, register the webhook with Hatena Bookmark. Set http://xxxx.sakura.ne.jp/yammer/post.cgi in" URL to receive event notification "referring to the procedure below.

http://developer.hatena.ne.jp/ja/documents/bookmark/apis/webhook

coding

First, make a separate file and restrict access with htaccess so that you do not accidentally leak the client_id and client_secret.

yammer.conf


[Yammer]
client_id = xxxx
client_secret = xxxx
redirect_uri = http://example.com/redirect.cgi
access_token = xxxx

.htaccess


AddHandler cgi-script cgi
<Files ~ ".conf">
deny from all
</Files>

Get access_token

Since it is OAuth, you will write a process like this on the Application side. (The image is from OAuth Bible of mashape)

The redirect from Yammer is returned in the format redirect.cgi? Code = xxxx, so if the URL does not have code =, the processing of Get Request Token and Direct User To Service Provider in the figure will be performed. If there is code = , it is written to execute the process of ʻExchange Request Token for Access Token.

redirect.cgi


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import yampy
import cgi
import ConfigParser

config = ConfigParser.RawConfigParser()
config.read('yammer.conf')

client_id = config.get('Yammer','client_id')
client_secret = config.get('Yammer','client_secret')
redirect_uri = config.get('Yammer','redirect_uri')

authenticator = yampy.Authenticator(client_id=client_id, client_secret=client_secret)

form = cgi.FieldStorage()
if form.has_key("code"):
    #Code obtained from Yammer(Request Token)From access_Generate token
    code = form["code"].value
    access_token = authenticator.fetch_access_token(code)
    print "Content-Type: text/html"
    print ""
    print access_token
else:
    #Generate an authentication URL and redirect to the Yammer authentication screen
    auth_url = authenticator.authorization_url(redirect_uri=redirect_uri)
    print "Location: "+auth_url
    print ""

If you place this file on the server and access http://xxxx.sakura.ne.jp/yammer/redirect.cgi, you will be redirected to the Yammer login screen. If you log in to Yammer on this screen and press the approve button, access_token will be displayed.

If you copy this access_token and write it in yammer.conf, you will be able to post to yammer via post.cgi below. (I didn't automate this part ...)

post.cgi


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cgi
import yampy
import ConfigParser

config = ConfigParser.RawConfigParser()
config.read('yammer.conf')
access_token = config.get('Yammer','access_token')

form = cgi.FieldStorage()
if form.has_key("comment"):
    comment = form["comment"].value
    #If the comment contains a yammer string, post it to Yammer
    if comment.find('yammer') > -1:
        url = form["url"].value
        title = form["title"].value
        body = title + ' ' + url
        yammer = yampy.Yammer(access_token=access_token)
        yammer.messages.create(body)

print "Content-Type: text/html"
print ""
print body

Now, if you tag it with yammer in Hatena Bookmark, it will post to Yammer.

Recommended Posts

Link Hatena Bookmark and Yammer
Migrate Qiita stock and LGTM to "Hatena Bookmark"
GCP: Link Functions and Pub / Sub