Skip to content

Instantly share code, notes, and snippets.

@gc373
Last active June 15, 2017 01:01
Show Gist options
  • Select an option

  • Save gc373/d0a363239c1ad1200020e6af54d53ab1 to your computer and use it in GitHub Desktop.

Select an option

Save gc373/d0a363239c1ad1200020e6af54d53ab1 to your computer and use it in GitHub Desktop.
Mstdn2twi

使い方

first, pls get token with oauth.js
oauth.jsをつかってTokenを取得
streaming.jsで接続 Twitterのkey類と制御処理は自分で実装してね

動作未検証

WTFPL
Copyright © 2017 gc373
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

const readline = require('readline')
const Mastodon = require('./node_modules/mastodon-api/lib/mastodon.js')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let clientId
let clientSecret
const baseUrl = 'https://[~your URL~]/'
Mastodon.createOAuthApp(baseUrl + '/api/v1/apps', 'botToken', 'read write follow').catch(err => console.error(err)).then((res) => {
console.log('Please save \'id\', \'client_id\' and \'client_secret\' in your program and use it from now on!')
console.log(res)
clientId = res.client_id
clientSecret = res.client_secret
return Mastodon.getAuthorizationUrl(clientId, clientSecret, baseUrl)
}).then(url => {
console.log('This is the authorization URL. Open it in your browser and authorize with your account!')
console.log(url)
return new Promise((resolve) => {
rl.question('Please enter the code from the website: ', code => {
resolve(code)
rl.close()
})
})
}).then(code => Mastodon.getAccessToken(clientId, clientSecret, code, baseUrl)).catch(err => console.error(err)).then(accessToken => {
console.log(`This is the access token. Save it!\n${accessToken}`)
})
const Masto = require('mastodon-api')
const fs = require('fs');
const M = new Masto({
access_token: '',
timeout_ms: 60 * 1000,
api_url: 'https://[~your URL~]/api/v1/',
})
const streamPub = M.stream('streaming/user')
streamPub.on('message', (msg) => {
if (msg.event == 'update') { // ここで自分のIDだったらTwitterへ (要制御処理)
console.log(JSON.stringify(msg, null, "\t"))
post2twitter(msg);
}
})
streamPub.on('error', (err) => {
console.log(err)
})
var twitter = require('twitter');
var fs = require('fs');
var client = new twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
function post2twitter(text) {
client.post('statuses/update', {
status: text
}, function (error, tweet, response) {
if (error) throw error;
console.log(tweet); // Tweet body.
console.log(response); // Raw response object.
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment