Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .telegramToken
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5393750537:AAFuPAfoN_FqhsWTKmTBthZq9-muTZosOv0
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
FROM python:3.8.12-slim-buster

# YOUR COMMANDS HERE
# ....
# ....

COPY app.py /
COPY mainApp.py /
COPY .telegramToken /
COPY requirements.txt /tmp/
COPY utils.py /
RUN pip install --requirements /tmp/requirements.txt


CMD ["python3", "app.py"]
39 changes: 29 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import telebot
from utils import search_download_youtube_video
from loguru import logger
import time
from utils import search_download_youtube_video


class Bot:
Expand All @@ -17,6 +18,9 @@ def _bot_internal_handler(self, messages):
self.current_msg = message
self.handle_message(message)

def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")

def start(self):
"""Start polling msgs from users, this function never returns"""
logger.info(f'{self.__class__.__name__} is up and listening to new messages....')
Expand All @@ -34,17 +38,22 @@ def send_text_with_quote(self, text, message_id):
def is_current_msg_photo(self):
return self.current_msg.content_type == 'photo'

def download_user_photo(self, quality=0):
def download_user_photo(self, quality=3):
"""
Downloads photos sent to the Bot to `photos` directory (should be existed)
:param quality: integer representing the file quality. Allowed values are [0, 1, 2, 3]
:return:
"""
self.send_text("Downloading image")
if self.current_msg.content_type != 'photo':
raise RuntimeError(f'Message content of type \'photo\' expected, but got {self.current_msg["content_type"]}')
raise RuntimeError(
f'Message content of type \'photo\' expected, but got {self.current_msg["content_type"]}')

file_info = self.bot.get_file(self.current_msg.photo[quality].file_id)
data = self.bot.download_file(file_info.file_path)
filed = open(file_info.file_path, "wb")
filed.write(data)
filed.close()

# TODO save `data` as a photo in `file_info.file_path` path

Expand All @@ -56,19 +65,29 @@ def handle_message(self, message):

class QuoteBot(Bot):
def handle_message(self, message):
if message.text != 'Don\'t quote me please':
if message.text != 'Dont quote me please':
self.send_text_with_quote(message.text, message_id=message.message_id)


class YoutubeBot(Bot):
pass

def handle_message(self, message):
if self.is_current_msg_photo():
self.download_user_photo()

if __name__ == '__main__':
with open('.telegramToken') as f:
_token = f.read()

my_bot = Bot(_token)
my_bot.start()
elif "download video" in message.text.lower():
self.send_text("Searching Video: {}".format(message.text.lower().replace('download video', '')))
ytb = search_download_youtube_video(message.text.lower().replace('download video', ''))
for video in ytb:
self.send_text("Video Name: {}".format(video["filename"]))
self.send_text("Video url: {}".format(video["url"]))
else:
self.send_text(f'Your original message: {message.text}')

if __name__ == '__main__':
with open('.telegramToken') as f:
_token = f.read()

my_bot = Bot(_token)
my_bot.start()
12 changes: 12 additions & 0 deletions mainApp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from app import YoutubeBot



token = open(".telegramToken").read()

YB = YoutubeBot(token = token)
YB.start()




Binary file added photos/file_7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.