Skip to content

Instantly share code, notes, and snippets.

@t-chab
Last active January 12, 2022 18:31
Show Gist options
  • Save t-chab/85414a01bfc3e7c70102 to your computer and use it in GitHub Desktop.
Save t-chab/85414a01bfc3e7c70102 to your computer and use it in GitHub Desktop.
telegram-bot Dockerfile, with youtube download plugin
FROM debian:latest
MAINTAINER [email protected]
# install app
#############
# install app from official repos, set perms, cleanup
# User telegramd is added manually to force uid to 5000, to avoid perms problems
RUN groupadd -r -g 5000 telegramd && \
useradd -m -s /bin/bash -u 5000 -c "Telegram Bot" -g telegramd telegramd && \
gpasswd -a telegramd users && \
# Update distro
apt-get update && apt-get dist-upgrade -y && \
# Install required dev deps
apt-get install -y lua5.2 libreadline-dev libconfig-dev libssl-dev liblua5.2-dev libevent-dev libjansson-dev python-dev && \
# Software deps
apt-get install -y dbus curl make unzip git redis-server g++ supervisor libav-tools atomicparsley python-pip && \
pip install --upgrade youtube_dl && \
cd /home/telegramd && \
git clone https://github.com/yagop/telegram-bot.git && \
cd telegram-bot && \
./launch.sh install && \
echo "#!/bin/sh" > /home/telegramd/run.sh && \
echo "/etc/init.d/redis-server restart" >> /home/telegramd/run.sh && \
echo "su - telegramd -c \"cd /home/telegramd/telegram-bot && ./tg/bin/telegram-cli -k ./tg/tg-server.pub -s ./bot/bot.lua -l 1 -E --disable-link-preview -L /home/telegramd/.telegram-cli/tgbot.log -d&\"" >> /home/telegramd/run.sh && \
chmod a+x /home/telegramd/run.sh && \
curl -O https://gist.githubusercontent.com/tchabaud/85414a01bfc3e7c70102/raw/1c7043b57d1ffe803c3d87b7651859059feca62c/ytdl.lua > /home/telegramd/telegram-bot/plugins/ytdl.lua && \
curl -O https://gist.githubusercontent.com/tchabaud/85414a01bfc3e7c70102/raw/1c7043b57d1ffe803c3d87b7651859059feca62c/ytmp3.lua > /home/telegramd/telegram-bot/plugins/ytmp3.lua && \
chown -R telegramd.users /home/telegramd
# docker settings
#################
ADD tbot.conf /etc/supervisor/conf.d/tbot.conf
# add custom environment file for application
VOLUME ["/home/telegramd/telegram-bot/data", "/home/telegramd/.telegram-cli", "/var/lib/redis"]
# run supervisor
ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]
#!/bin/sh
/etc/init.d/redis-server start
su - telegramd -c "cd /home/telegramd/telegram-bot && ./tg/bin/telegram-cli -k ./tg/tg-server.pub -s ./bot/bot.lua -l 1 -E --disable-link-preview -L /home/telegramd/.telegram-cli/tgbot.log -d&"
#!/usr/bin/env sh
docker run -d --restart=always -v /data/tg/tg-bot:/home/telegramd/telegram-bot/data -v /data/tg/tg-cli:/home/telegramd/.telegram-cli --name tbot tg
do
local function run(msg, matches)
-- To avoid interferences with ytmp3 plugin
local ytmp3cmd = '!mp3'
if string.sub(msg.text,1,string.len(ytmp3cmd)) == ytmp3cmd then
return ''
end
local receiver = get_receiver(msg)
local searchString = '%[download] Destination: '
local yt_code = matches[1]
local yt_url = 'http://www.youtube.com/watch?v='..yt_code
local command = 'youtube-dl --embed-thumbnail --add-metadata -f 18 -o "/tmp/%(title)s.%(ext)s" --restrict-filenames '..yt_url
local pattern = searchString..'.-\n'
print(command)
print(pattern)
local handle = io.popen(command)
local result = handle:read("*r")
local fileString = string.match(result, pattern)
handle:close()
local cleanFile = string.gsub(fileString, searchString, '')
local fileName = string.gsub(cleanFile, '\n$', '')
print(fileName)
local cb_extra = {file_path=fileName}
send_video(receiver, fileName, rmtmp_cb, cb_extra)
end
return {
description = 'Download YouTube video.',
usage = 'Type a youtube url to automatically download the video.',
patterns = {
'youtu.be/([_A-Za-z0-9-]+)',
'youtube.com/watch%?v=([_A-Za-z0-9-]+)'
},
run = run
}
end
do
local function run(msg, matches)
local receiver = get_receiver(msg)
local yt_url = matches[1]
local command = 'youtube-dl --add-metadata -f "bestaudio/best" -o "/tmp/%(title)s.%(ext)s" --restrict-filenames -x --audio-format mp3 --audio-quality 192K '..yt_url
local searchString = '%[ffmpeg] Destination: '
local pattern = searchString..'.-\n'
print(command)
print(pattern)
local handle = io.popen(command)
local result = handle:read("*a")
local fileString = string.match(result, pattern)
handle:close()
local cleanFile = string.gsub(fileString, searchString, '')
local fileName = string.gsub(cleanFile, '\n$', '')
print(fileName)
local cb_extra = {file_path=fileName}
send_audio(receiver, fileName, rmtmp_cb, cb_extra)
end
return {
description = 'Convert YouTube video to mp3 audio.',
usage = 'Type !mp3 [youtube_url] to download the sound of the video converted to mp3 file.',
patterns = {
'^!mp3 (.*)$'
},
run = run
}
end
@drig0
Copy link

drig0 commented Nov 26, 2015

I could not really use it

many denied permission messages

@t-chab
Copy link
Author

t-chab commented Jan 3, 2016

When do you have this denied permission messages ?

When you run docker ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment