This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anatoli Nicolae <[email protected]>
- Loading branch information
0 parents
commit 279b998
Showing
14 changed files
with
544 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
config.*.js | ||
!config.example.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:8-alpine | ||
|
||
# Create app directory | ||
WORKDIR /usr/src/app | ||
|
||
# Copy app source | ||
COPY . . | ||
|
||
# Install dependencies | ||
RUN yarn | ||
|
||
# Run app | ||
CMD [ "yarn", "start" ] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright 2018 thundersquared | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Config import and checks | ||
const config = require('./config') | ||
|
||
if (!config.telegram || !config.telegram.token || config.telegram.token.indexOf(':') < 0) { | ||
process.exit() | ||
} | ||
|
||
// Telegram modules loading | ||
const path = require('path') | ||
const Telegraf = require('telegraf') | ||
const TelegrafI18n = require('telegraf-i18n') | ||
const RateLimit = require('telegraf-ratelimit') | ||
const MySQLSession = require('telegraf-session-mysql') | ||
const commandParts = require('telegraf-command-parts') | ||
const fetch = require('./handler/fetch') | ||
const lang = require('./handler/lang').command | ||
|
||
const bot = new Telegraf(config.telegram.token, { | ||
username: config.telegram.username | ||
}) | ||
|
||
// DB connection if any | ||
if (config.mysql && config.mysql.host) { | ||
const session = new MySQLSession(config.mysql) | ||
bot.use(session.middleware()) | ||
} | ||
|
||
// Limit lookups | ||
const limiter = new RateLimit({ | ||
window: 5 * 60 * 1000, | ||
limit: 10, | ||
onLimitExceeded: (ctx, next) => { | ||
return ctx.reply(ctx.i18n.t('rate.limit')) | ||
} | ||
}) | ||
|
||
// Internationalization | ||
const i18n = new TelegrafI18n({ | ||
directory: path.resolve(__dirname, 'locales'), | ||
defaultLanguage: 'en', | ||
sessionName: 'session' | ||
}) | ||
|
||
// Apply middlewares | ||
bot.use(limiter) | ||
bot.use(i18n.middleware()) | ||
bot.use(commandParts()) | ||
bot.use((ctx, next) => { | ||
if (ctx.session) { | ||
ctx.session.lookups = ctx.session.lookups || 0 | ||
ctx.session.limit = ctx.session.limit || 0 | ||
ctx.session.lang = ctx.session.lang || 'en' | ||
|
||
ctx.i18n.locale(ctx.session.lang) | ||
} | ||
|
||
return next() | ||
}) | ||
|
||
// Start command | ||
bot.start((ctx) => { | ||
return ctx.reply(ctx.i18n.t('commands.start')) | ||
}) | ||
|
||
bot.command('help', (ctx) => ctx.reply(ctx.i18n.t('commands.help'), { | ||
parse_mode: 'Markdown' | ||
})) | ||
bot.command('lookup', fetch) | ||
bot.command('lang', lang) | ||
|
||
bot.on('text', fetch) | ||
|
||
bot.startPolling() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const config = { | ||
mysql: { | ||
host: 'localhost', | ||
database: 'tikvidbot', | ||
user: 'root', | ||
password: 'root' | ||
}, | ||
telegram: { | ||
username: 'tikvidbot', | ||
token: 'your:token' | ||
} | ||
} | ||
|
||
module.exports = config |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
const config = { | ||
mysql: { | ||
host: process.env.DB_HOST, | ||
database: process.env.DB_NAME, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASS | ||
}, | ||
telegram: { | ||
username: process.env.BOT_NAME, | ||
token: process.env.BOT_TOKEN | ||
} | ||
} | ||
|
||
const envConfFile = path.resolve(__dirname, `config.${process.env.NODE_ENV}.js`) | ||
|
||
if (process.env.NODE_ENV && fs.existsSync(envConfFile)) { | ||
Object.assign(config, require(envConfFile)) | ||
} | ||
|
||
module.exports = config |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const url = require('url') | ||
const Extra = require('telegraf/extra') | ||
const Markup = require('telegraf/markup') | ||
const werist = require('werist') | ||
const isDomainName = require('is-domain-name') | ||
const isHttpUrl = require('is-http-url') | ||
|
||
// Domain checks | ||
const check = ctx => { | ||
if (ctx.message.text) { | ||
let domain | ||
|
||
if (typeof ctx.state.command !== 'undefined') { | ||
if (ctx.state.command.command === 'lookup') { | ||
domain = ctx.state.command.args | ||
} | ||
} else { | ||
domain = ctx.message.text | ||
} | ||
|
||
if (isHttpUrl(domain)) { | ||
let query = url.parse(domain) | ||
domain = query.hostname | ||
} | ||
|
||
if (isDomainName(domain)) { | ||
if (ctx.session) { | ||
ctx.session.lookups++ | ||
} | ||
|
||
ctx.replyWithChatAction('typing') | ||
|
||
return lookup(ctx, domain) | ||
} else { | ||
if (ctx.message.chat.type !== 'group' || ctx.message.chat.type !== 'supergroup') { | ||
return ctx.reply(ctx.i18n.t('errors.domain')) | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// Lookup process | ||
const lookup = (ctx, domain) => { | ||
werist.lookup(domain, (err, data) => { | ||
if (err) { | ||
console.error(err) | ||
return ctx.reply(ctx.i18n.t('errors.whois')) | ||
} | ||
|
||
if (data) { | ||
let whois = data.split('<<<')[0] | ||
|
||
whois = `<pre>${whois}</pre>` | ||
|
||
if (ctx.session) { | ||
whois += ctx.i18n.t('rate.lookup', { | ||
lookups: ctx.session.lookups, | ||
domain: domain | ||
}) | ||
} | ||
|
||
return ctx.reply(whois, { | ||
disable_web_page_preview: true, | ||
parse_mode: 'HTML' | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = check |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const util = require('util') | ||
|
||
const languages = [ | ||
{ | ||
code: 'en', | ||
name: 'English' | ||
}, | ||
{ | ||
code: 'it', | ||
name: 'Italiano' | ||
}, | ||
] | ||
|
||
const command = ctx => { | ||
if (ctx.state.command.command === 'lang') return | ||
|
||
let lang = ctx.state.command.args | ||
|
||
if (languages.find(e => e.code === lang)) { | ||
lang = languages.find(e => e.code === lang) | ||
|
||
ctx.session.lang = lang.code | ||
ctx.i18n.locale(lang.code) | ||
|
||
return ctx.reply(ctx.i18n.t('commands.lang.changed', { | ||
language: lang.name | ||
})) | ||
} | ||
|
||
return ctx.reply(ctx.i18n.t('commands.lang.info', { | ||
language: languages.find(e => e.code === ctx.session.lang).name | ||
})) | ||
} | ||
|
||
module.exports = { | ||
languages, | ||
command | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
commands: | ||
start: Welcome! Start by having a look at /help 😉 | ||
help: | | ||
I’m a 🤖 that allows you to save TikTok videos locally. | ||
In order to get a video, just share it to this chat and I'll send the file. | ||
Note that there is a *10 requests limit* every 5 minutes. | ||
I was made at thundersquared.com ⚡️ and I’m open source❤️! | ||
github.com/thundersquared/tikvidbot | ||
lang: | ||
info: Your current language is set to ${language}! | ||
changed: Awesome! You've changed the language to ${language}! | ||
rate: | ||
limit: Rate limit exceeded | ||
lookup: "\n\nLookup #${lookups}: <strong>${domain}</strong>" | ||
errors: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
commands: | ||
start: Ciao! Inizia dando un'occhiata a /help 😉 | ||
help: | | ||
Sono un 🤖 che ti permetterà di salvare localmente i video di TikTok. | ||
Per ottenere un video, condividilo in questa chat e io ti invierò il file. | ||
Nota che c'è un *limite di 5 richieste* ogni 5 minuti. | ||
Sono stato fatto a thundersquared.com ⚡️ e sono open source❤️! | ||
github.com/thundersquared/tikvidbot | ||
lang: | ||
info: La lingua corrente è ${language}. | ||
changed: Ottimo! Hai cambiato lingua a ${language}. | ||
rate: | ||
limit: Limite richieste superato | ||
lookup: "\n\nLookup #${lookups}: <strong>${domain}</strong>" | ||
errors: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "tikvidbot", | ||
"version": "1.0.0", | ||
"description": "TikTok Video Download Telegram Bot", | ||
"main": "bot.js", | ||
"author": "thundersquared (https://thundersquared.com)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"telegraf": "^3.25.0", | ||
"telegraf-command-parts": "^1.0.3", | ||
"telegraf-i18n": "^6.4.0", | ||
"telegraf-ratelimit": "^2.0.0", | ||
"telegraf-session-mysql": "^5.1.0" | ||
}, | ||
"scripts": { | ||
"dev": "NODE_ENV=dev node bot.js", | ||
"prod": "NODE_ENV=prod node bot.js", | ||
"start": "NODE_ENV=prod node bot.js", | ||
"docker:build": "scripts/build.sh", | ||
"docker:run": "scripts/run.sh", | ||
"docker": "yarn docker:build && yarn docker:run" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker build -t sqrd/tikvidbot . |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
docker stop sqrd_tikvidbot | ||
docker rm sqrd_tikvidbot | ||
docker run -itd --name=sqrd_tikvidbot sqrd/tikvidbot |
Oops, something went wrong.