mirror of
https://github.com/RGBCube/TwitterDiscordWebhook
synced 2025-07-27 04:57:44 +00:00
chore & readme: better config
This commit is contained in:
parent
5047b38f4e
commit
fd73c2ad1f
3 changed files with 42 additions and 45 deletions
18
README.md
18
README.md
|
@ -14,15 +14,15 @@ cd discord-twitter-webhook
|
||||||
### Configure
|
### Configure
|
||||||
Edit the `config.example.json` and rename it to `config.json` with the given information below:
|
Edit the `config.example.json` and rename it to `config.json` with the given information below:
|
||||||
|
|
||||||
* `webhook_url`: The Discord webhook url, for more information check out [this](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) page.
|
* `discord.webhookUrl`: The Discord webhook url, for more information check out [this](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) page.
|
||||||
* `webhook_name`: The username of your Discord webhook.
|
* `discord.webhookName`: The username of your Discord webhook.
|
||||||
* `webhook_avatar`: The avatar of your Discord webhook.
|
* `discord.webhookAvatar`: The avatar of your Discord webhook.
|
||||||
* `webhook_message`: The message before your Twitter link. Formatted as `{message} {tweet_link}`, no newline added.
|
* `discord.webhookMessage`: The message before your Twitter link. Formatted as `{message} {tweet_link}`, no newline added.
|
||||||
* `api_key`: The Twitter API key, if you do not have a Twitter developer account then check out [this](https://developer.twitter.com/en) page.
|
* `twitter.apiKey`: The Twitter API key, if you do not have a Twitter developer account then check out [this](https://developer.twitter.com/en) page.
|
||||||
* `api_secret`: The Twitter API secret, requires a Twitter developer account.
|
* `twitter.apiSecret`: The Twitter API secret, requires a Twitter developer account.
|
||||||
* `access_token`: The Twitter account access token, requires a Twitter developer account.
|
* `twitter.accessToken`: The Twitter account access token, requires a Twitter developer account.
|
||||||
* `access_secret`: The Twitter account acesss secret, requires a Twitter developer account.
|
* `twitter.accessSecret`: The Twitter account acesss secret, requires a Twitter developer account.
|
||||||
* `user_id`: The user ID for the Twitter account you want notifications from, to find a Twitter id use [this](https://tweeterid.com/) tool.
|
* `followingUserId`: The user ID for the Twitter account you want notifications from, to find a Twitter id use [this](https://tweeterid.com/) tool.
|
||||||
|
|
||||||
### Install Dependencies
|
### Install Dependencies
|
||||||
> You will need Node.js installed to do this and the next section! If you don't have it installed, you can install it from [here](https://nodejs.org/en/).
|
> You will need Node.js installed to do this and the next section! If you don't have it installed, you can install it from [here](https://nodejs.org/en/).
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
{
|
{
|
||||||
"webhook_url": "",
|
"discord": {
|
||||||
"webhook_name": "",
|
"webhookUrl": "",
|
||||||
"webhook_avatar": "",
|
"webhookName": "",
|
||||||
"webhook_message": "",
|
"webhookAvatar": "",
|
||||||
"api_key": "",
|
"webhookMessage": ""
|
||||||
"api_secret": "",
|
},
|
||||||
"access_token": "",
|
"twitter": {
|
||||||
"acesss_secret": "",
|
"apiKey": "",
|
||||||
"user_id": ""
|
"apiSecret": "",
|
||||||
|
"accessToken": "",
|
||||||
|
"acessSecret": ""
|
||||||
|
},
|
||||||
|
"followingUserId": ""
|
||||||
}
|
}
|
||||||
|
|
47
main.js
47
main.js
|
@ -3,48 +3,41 @@ import { Webhook } from "discord-webhook-node";
|
||||||
|
|
||||||
const config = require("config.json");
|
const config = require("config.json");
|
||||||
|
|
||||||
const wClient = new Webhook(config.webhook_url);
|
const dClient = new Webhook(config.discord.webhookUrl);
|
||||||
wClient.setAvatar(config.webhook_avatar);
|
dClient.setUsername(config.discord.webhookName);
|
||||||
wClient.setUsername(config.webhook_name);
|
dClient.setAvatar(config.discord.webhookAvatar);
|
||||||
|
|
||||||
const tClient = new Twitter({
|
const tClient = new Twitter({
|
||||||
consumer_key: config.api_key,
|
consumer_key: config.twitter.apiKey,
|
||||||
consumer_secret: config.api_secret,
|
consumer_secret: config.twitter.apiSecret,
|
||||||
access_token: config.access_token,
|
access_token: config.twitter.accessToken,
|
||||||
access_token_secret: config.acesss_secret,
|
access_token_secret: config.twitter.acessSecret,
|
||||||
});
|
});
|
||||||
|
|
||||||
tClient.get("account/verify_credentials", { skip_status: true })
|
tClient.get("account/verify_credentials", { skip_status: true })
|
||||||
.catch(err =>
|
.catch(err =>
|
||||||
console.log(`Unable to login due to the following error:\n${err}`)
|
console.error(`Unable to login due to the following error:\n${err}`)
|
||||||
)
|
)
|
||||||
.then(res =>
|
.then(res =>
|
||||||
console.log(`Logged in as '${res.data.name}'!`)
|
console.info(`Logged in as '${res.data.name}'!`)
|
||||||
);
|
);
|
||||||
|
|
||||||
let stream = tClient.stream("statuses/filter", { follow: config.user_id });
|
console.info(`Fetching tweets from ID '${config.followingUserId}'.`);
|
||||||
|
|
||||||
console.log(`Fetching tweets from id '${config.user_id}'.`);
|
tClient.stream("statuses/filter", { follow: config.followingUserId })
|
||||||
|
.on("tweet", twt => {
|
||||||
|
if (twt.retweeted_status || twt.in_reply_to_status_id || twt.is_quote_status) return;
|
||||||
|
|
||||||
stream.on("connected", () =>
|
let twitterLink = `https://twitter.com/${twt.user.screen_name}/status/${twt.id_str}`;
|
||||||
console.log("Successfully connected to Twitter!")
|
dClient.send(`${config.discord.webhookMessage} ${twitterLink}`);
|
||||||
);
|
})
|
||||||
|
|
||||||
stream.on("tweet", twt => {
|
.on("connected", () => console.info("Connected to Twitter!"))
|
||||||
if(twt.retweeted_status || twt.in_reply_to_status_id || twt.is_quote_status) return;
|
|
||||||
|
|
||||||
let twitterLink = `https://twitter.com/${twt.user.screen_name}/status/${twt.id_str}`;
|
.on("disconnect", () => console.warn("Disconnected from Twitter!"))
|
||||||
wClient.send(`${config.webhook_message} ${twitterLink}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
stream.on("disconnect", () =>
|
|
||||||
console.log("Disconnected from twitter!")
|
|
||||||
);
|
|
||||||
|
|
||||||
stream.on("reconnect", () =>
|
.on("reconnect", () => console.info("Reconnected to Twitter!"));
|
||||||
console.log("Reconnecting to Twitter!")
|
|
||||||
);
|
|
||||||
|
|
||||||
process.on("uncaughtException", err =>
|
process.on("uncaughtException", err =>
|
||||||
console.log(`Something (bad) happened: ${err}`)
|
console.error(`Something (bad) happened:\n${err}`)
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue