Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
🎵
Browse files Browse the repository at this point in the history
Signed-off-by: Anatoli Nicolae <[email protected]>
  • Loading branch information
anatolinicolae committed Nov 8, 2018
0 parents commit 279b998
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
config.*.js
!config.example.js
13 changes: 13 additions & 0 deletions Dockerfile
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" ]
21 changes: 21 additions & 0 deletions LICENSE.md
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.
73 changes: 73 additions & 0 deletions bot.js
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()
14 changes: 14 additions & 0 deletions config.example.js
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
23 changes: 23 additions & 0 deletions config.js
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
72 changes: 72 additions & 0 deletions handler/fetch.js
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
38 changes: 38 additions & 0 deletions handler/lang.js
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
}
19 changes: 19 additions & 0 deletions locales/en.yaml
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:
19 changes: 19 additions & 0 deletions locales/it.yaml
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:
23 changes: 23 additions & 0 deletions package.json
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"
}
}
1 change: 1 addition & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker build -t sqrd/tikvidbot .
3 changes: 3 additions & 0 deletions scripts/run.sh
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
Loading

0 comments on commit 279b998

Please sign in to comment.