Created
September 20, 2017 03:16
-
-
Save potato4d/561773e246dd47c4524bc2522a2bd704 to your computer and use it in GitHub Desktop.
AWS LambdaでMisskeyの通知を受けるやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = function(_, __, callback){ | |
const axios = require('axios'); | |
const firebase = require('firebase'); | |
const misskey = Object.assign({}, axios); | |
// 悪 | |
const CSRF_TOKEN = 'YOUR-MISSKEY-CSRF-TOKEN' | |
const APPNO = 'YOUR-PUSH7-APPNO' | |
const APIKEY = 'YOUR-PUSH7-APIKEY' | |
const COOKIE = 'YOUR MISSKEY.LINK COOKIE'; | |
const API_ROOT = 'https://himasaku.misskey.link' | |
const PUSH7_ROOT = 'https://api.push7.jp/api/v1'; | |
const YOUR_USERNAME = 'YOUR MISSKEY ID(WITHOUT "@")'; | |
const config = {}; // FirebaseのConfigをコピペしてくる | |
function getBody(type, data){ | |
switch (type) { | |
case 'like': | |
return { | |
title: `${data.content.user.name}にライクされました。`, | |
body: data.content.post.text | |
} | |
case 'repost': | |
return { | |
title: `${data.content.user.name}にリポストされました。`, | |
body: data.content.post.text | |
} | |
case 'mention': | |
return { | |
title: `${data.content.post.user.name}があなたをメンションしました。`, | |
body: data.content.post.text | |
} | |
default: | |
return { | |
title: type + ": それ以外", | |
body: "それ以外です。" | |
} | |
} | |
} | |
firebase.initializeApp(config); | |
firebase.database().ref('/users/' + YOUR_USERNAME + '/latest').once('value').then((snapshot) =>{ | |
const cursor = +(snapshot.val()); | |
misskey.defaults.baseURL = API_ROOT; | |
misskey.defaults.headers.common['cookie'] = COOKIE; | |
misskey.defaults.headers.common['csrf-token'] = CSRF_TOKEN; | |
misskey.post('/notifications/unread/count') | |
.then((res)=>{ | |
console.log(res.data); | |
if(res.data){ | |
return Promise.resolve('OK Unread'); | |
} | |
}) | |
.then((res)=>{ | |
console.log(res); | |
return misskey.post('/notifications/timeline') | |
}) | |
.then((res)=>{ | |
return new Promise((resolve, reject)=>{ | |
const notifications = res.data.filter((n)=>(n.cursor > cursor)); | |
if(!notifications.length){ | |
resolve(res);return; | |
} | |
if(notifications.length){ | |
const insertId = notifications.reduce((a, b)=>{return a.cursor > b.cursor ? a : b}).cursor; | |
firebase.database().ref('/users/potato4d').update({latest: ""+insertId}) | |
.then(()=>{resolve(res);return;}) | |
.catch(()=>{resolve(res);return;}) | |
} | |
}); | |
}) | |
.then((res)=>{ | |
axios.defaults.headers.common['Authorization'] = `Bearer ${APIKEY}`; | |
const notifications = res.data.filter((n)=>(n.cursor > cursor)); | |
if(!notifications.length){ | |
return Promise.resolve([]); | |
} | |
return Promise.all( | |
notifications.map((n)=> | |
axios.post( | |
`${PUSH7_ROOT}/${APPNO}/send`, | |
Object.assign( | |
{ | |
icon: 'https://dashboard.push7.jp/uploads/4f56abbc9e3c4c3c8fc942e85d5151c3.png', | |
url: 'https://misskey.link' | |
}, | |
getBody(n.type, n) | |
) | |
) | |
) | |
); | |
}) | |
.then((res)=>{ | |
firebase.database().goOffline() | |
console.log('さいご'); | |
callback(null, "success"); | |
}) | |
.catch((err)=>{ | |
console.log(err); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment