Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
4a72e8b
python project complated
beny1221g May 8, 2024
1ce957d
jenkins
beny1221g May 29, 2024
6ff6592
jenkins2
beny1221g May 29, 2024
136c8ea
jenkins12
beny1221g May 29, 2024
74fb182
jenkins1122
beny1221g May 29, 2024
ef4e989
jenkins1111212
beny1221g May 29, 2024
9b59e57
jenk
beny1221g May 29, 2024
ef8a735
jenk1
beny1221g May 29, 2024
3dbc758
jenk211
beny1221g May 29, 2024
8a45a1c
build_dockerfile
beny1221g Jun 5, 2024
7f4cf48
change file
beny1221g Jun 5, 2024
8312f44
change file2
beny1221g Jun 5, 2024
28ebfa7
change files
beny1221g Jun 5, 2024
374169d
hh
beny1221g Jun 5, 2024
5b1b24b
hh2
beny1221g Jun 5, 2024
90dffb3
hh2
beny1221g Jun 5, 2024
c37ff86
hh2
beny1221g Jun 5, 2024
b0622aa
hh2
beny1221g Jun 5, 2024
912edfc
hh2
beny1221g Jun 5, 2024
831e21b
hh2
beny1221g Jun 5, 2024
ed21a1d
hh2
beny1221g Jun 5, 2024
5ae9e5e
hh2
beny1221g Jun 5, 2024
3370b28
hh2
beny1221g Jun 5, 2024
09cb389
hh2
beny1221g Jun 5, 2024
75d4919
hh2
beny1221g Jun 5, 2024
2688e3d
hh2
beny1221g Jun 5, 2024
38fc234
hh2
beny1221g Jun 5, 2024
706178e
hh2
beny1221g Jun 5, 2024
bef053f
hh2
beny1221g Jun 5, 2024
90fa441
hh2
beny1221g Jun 5, 2024
a83f9fe
hh2
beny1221g Jun 5, 2024
6c47977
hh2
beny1221g Jun 5, 2024
6eba209
hh2
beny1221g Jun 5, 2024
0df2894
hh2
beny1221g Jun 5, 2024
eee677f
hh2
beny1221g Jun 5, 2024
2ac64b0
hh2
beny1221g Jun 5, 2024
7f89815
hh2
beny1221g Jun 5, 2024
3cdc845
hh2
beny1221g Jun 5, 2024
6a3943c
hh2
beny1221g Jun 5, 2024
6d8fc2b
hh2
beny1221g Jun 5, 2024
e711530
hh2
beny1221g Jun 9, 2024
c3cda0d
hh2
beny1221g Jun 9, 2024
0efe3d4
hh2
beny1221g Jun 9, 2024
1b7a677
hh2
beny1221g Jun 9, 2024
fd17604
hh2
beny1221g Jun 9, 2024
df8dcc8
hh2
beny1221g Jun 9, 2024
03964a6
hh2
beny1221g Jun 9, 2024
318de63
try changes
beny1221g Jun 9, 2024
e62e519
try changes
beny1221g Jun 9, 2024
be51e57
try changes
beny1221g Jun 9, 2024
c85ce78
try changes
beny1221g Jun 9, 2024
5df136a
try changes
beny1221g Jun 9, 2024
e56b4a2
try changes
beny1221g Jun 9, 2024
13f65f6
try changes
beny1221g Jun 9, 2024
3ba1c5e
try changes
beny1221g Jun 9, 2024
8cbf08b
try changes
beny1221g Jun 9, 2024
32419d8
try changes
beny1221g Jun 9, 2024
fccc076
try changes
beny1221g Jun 9, 2024
670e797
try changes
beny1221g Jun 9, 2024
8eb7e51
try changes
beny1221g Jun 9, 2024
e84b422
try changes
beny1221g Jun 9, 2024
397d3a3
try changes
beny1221g Jun 9, 2024
009cec6
try changes
beny1221g Jun 9, 2024
ed707dd
try changes
beny1221g Jun 9, 2024
a1e6fcc
try changes
beny1221g Jun 9, 2024
c5b0997
try changes
beny1221g Jun 9, 2024
853ec1c
try changes
beny1221g Jun 9, 2024
44ca059
try changes
beny1221g Jun 9, 2024
2521777
try changes
beny1221g Jun 9, 2024
7fcebd9
Update build.Jenkinsfile to work with ubuntu
beny1221g Jun 12, 2024
1735abe
Update build.Jenkinsfile ubuntu
beny1221g Jun 12, 2024
e3dcece
Update deploy.Jenkinsfile ubuntu update
beny1221g Jun 12, 2024
2cf2b95
update dirwork
beny1221g Jun 13, 2024
4538163
Merge https://github.com/beny1221g/ImageProcessingService
beny1221g Jun 13, 2024
51f20fd
update work dir to app
beny1221g Jun 13, 2024
8808440
as
beny1221g Jun 13, 2024
90333be
sa
beny1221g Jun 13, 2024
0d8f71a
sa
beny1221g Jun 13, 2024
87db54a
as
beny1221g Jun 13, 2024
eb6427f
sa
beny1221g Jun 15, 2024
50dd9bf
add pr-testing.jenkisfile
beny1221g Jun 16, 2024
e0dc90d
add pr-testing.jenkisfile
beny1221g Jun 16, 2024
4fbb219
add to req
beny1221g Jun 16, 2024
8ae046e
ad
beny1221g Jun 16, 2024
b53f537
update
beny1221g Jun 16, 2024
f10d534
update
beny1221g Jun 16, 2024
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
33 changes: 33 additions & 0 deletions build.Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pipeline {
agent any

environment {
IMG_NAME = "polybot:${BUILD_NUMBER}"
}

stages {
stage('Build docker image') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub_key', usernameVariable: 'USERNAME', passwordVariable: 'USERPASS')]) {
script {
// Use correct Docker login syntax
sh """
cd polybot
echo ${USERPASS} | docker login -u ${USERNAME} --password-stdin
docker build -t ${IMG_NAME} .
docker tag ${IMG_NAME} beny14/${IMG_NAME}
docker push beny14/${IMG_NAME}
"""
}
}
}
}
stage('Trigger Deploy') {
steps {
build job: 'deploy_polybot', wait: false, parameters: [
string(name: 'beny14/$IMG_NAME', value: IMG_NAME)
]
}
}
}
}
17 changes: 17 additions & 0 deletions deploy.Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pipeline {
agent any

parameters {
string(name: 'beny14/polybot:${BUILD_NUMBER}', defaultValue: 'INAGE_URL', description: 'deploy polybot')
}

stages {
stage('Deploy') {
steps {
sh '''
echo "deploying to k8s cluster ..( or any other alternative)"
'''
}
}
}
}
1 change: 1 addition & 0 deletions hi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions jenkis_try/try.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
he21122
21 changes: 21 additions & 0 deletions polybot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use the official Python slim image
FROM python:3.10.12-slim-bullseye

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .


# Install the required Python packages
RUN pip install --no-cache-dir --upgrade pip

# Debugging step: Capture and output logs of pip install
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code into the container
COPY . .

# Specify the command to run the application
CMD ["python3", "polybot/bot.py"]
27 changes: 0 additions & 27 deletions polybot/app.py

This file was deleted.

225 changes: 150 additions & 75 deletions polybot/bot.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,153 @@
import telebot
from loguru import logger
import os
import time
from telebot.types import InputFile
import cv2
from polybot.img_proc import Img
from dotenv import load_dotenv
import os


class Bot:

def __init__(self, token, telegram_chat_url):
# create a new instance of the TeleBot class.
# all communication with Telegram servers are done using self.telegram_bot_client
self.telegram_bot_client = telebot.TeleBot(token)

# remove any existing webhooks configured in Telegram servers
self.telegram_bot_client.remove_webhook()
time.sleep(0.5)

# set the webhook URL
self.telegram_bot_client.set_webhook(url=f'{telegram_chat_url}/{token}/', timeout=60)

logger.info(f'Telegram Bot information\n\n{self.telegram_bot_client.get_me()}')

def send_text(self, chat_id, text):
self.telegram_bot_client.send_message(chat_id, text)

def send_text_with_quote(self, chat_id, text, quoted_msg_id):
self.telegram_bot_client.send_message(chat_id, text, reply_to_message_id=quoted_msg_id)

def is_current_msg_photo(self, msg):
return 'photo' in msg

def download_user_photo(self, msg):
"""
Downloads the photos that sent to the Bot to `photos` directory (should be existed)
:return:
"""
if not self.is_current_msg_photo(msg):
raise RuntimeError(f'Message content of type \'photo\' expected')

file_info = self.telegram_bot_client.get_file(msg['photo'][-1]['file_id'])
data = self.telegram_bot_client.download_file(file_info.file_path)
folder_name = file_info.file_path.split('/')[0]

if not os.path.exists(folder_name):
os.makedirs(folder_name)

with open(file_info.file_path, 'wb') as photo:
photo.write(data)

return file_info.file_path

def send_photo(self, chat_id, img_path):
if not os.path.exists(img_path):
raise RuntimeError("Image path doesn't exist")

self.telegram_bot_client.send_photo(
chat_id,
InputFile(img_path)
)

def handle_message(self, msg):
"""Bot Main message handler"""
logger.info(f'Incoming message: {msg}')
self.send_text(msg['chat']['id'], f'Your original message: {msg["text"]}')


class QuoteBot(Bot):
def handle_message(self, msg):
logger.info(f'Incoming message: {msg}')

if msg["text"] != 'Please don\'t quote me':
self.send_text_with_quote(msg['chat']['id'], msg["text"], quoted_msg_id=msg["message_id"])


class ImageProcessingBot(Bot):
pass
# load environment variables
load_dotenv()
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')

# Check if TELEGRAM_TOKEN is not none
if TELEGRAM_TOKEN is None:
print("Error : TELEGRAM_TOKEN is not set in the .env file.")
exit(1)

# initialize telegram-bot
bot = telebot.TeleBot(TELEGRAM_TOKEN)

# Dictionary to store images temporarily
user_images = {}

# handler for the /start command
@bot.message_handler(commands=['start'])
def handle_start(message):
bot.send_message(message.chat.id, "Hello!\n\n Send me an image then choose a filter , options:\n"
"- Blur: Apply a blur to the image to reduce noise and detail.\n"
"- Rotate: turning the image upside down.\n"
"- Salt and Pepper: Adds random bright and dark pixels to an image.\n"
"- Segment: Divides an image into parts based on color.\n"
"- Grayscale: Converts the image to grayscale.\n"
"- Sharpen: Enhances the edges and details in the image.\n"
"- Emboss: Creates a raised effect by highlighting the edges.\n"
"- Invert Colors: Inverts the colors of the image.\n"
"- Oil Painting: Applies an oil painting-like effect to the image.\n"
"- Cartoonize: Creates a cartoon-like version of the image.\n")

# handler for receiving photos
@bot.message_handler(content_types=['photo'])
def handle_image(message):
try:
print("Received a photo message")
# get the photo file id
file_id = message.photo[-1].file_id
# get the file object using the file id
file_info = bot.get_file(file_id)
# download the file
downloaded_file = bot.download_file(file_info.file_path)

# save the file temporarily with a unique name based on the file id
image_path = f"images/{file_id}.jpg"
with open(image_path, 'wb') as new_file:
new_file.write(downloaded_file)

# check if this is the first img or the second img for concat
if message.chat.id in user_images:
print("User already has an image in memory")
if 'concat_pending' in user_images[message.chat.id]:
print("This is the second image for concatenation")
# this is the second image for concat
second_image_path = image_path
first_image_path = user_images[message.chat.id]['concat_pending']
del user_images[message.chat.id]['concat_pending'] # remove the pending to free it to next pending

# load the images
first_image_data = cv2.imread(first_image_path)
second_image_data = cv2.imread(second_image_path)

# concat the imges
img_processor = Img(first_image_path)
concatenated_image = img_processor.concat(second_image_data)
if concatenated_image is not None:
print("Concatenation successful")
# save and send the concat img
processed_image_path = img_processor.save_image(concatenated_image, suffix='_concatenated')
with open(processed_image_path, 'rb') as photo_file:
bot.send_photo(message.chat.id, photo_file)
else:
print("Error concatenating images.")
bot.reply_to(message, "Error concatenating images.")

# clear user history
del user_images[message.chat.id]
else:
# this is the first img
print("This is the first image for concatenation")
user_images[message.chat.id]['concat_pending'] = image_path
bot.reply_to(message, "First image saved successfully! Now please send the second image to concatenate with.")
else:
# this is the first img
print("This is the first image received")
user_images[message.chat.id] = {'concat_pending': image_path}
bot.reply_to(message, "First image saved successfully! To apply the concatenation filter, please send another image or choose a filter from the list at the top of the page to apply a filter.")
except Exception as e:
print(f"Error handling image: {e}")
bot.reply_to(message, f"Error handling image: {e}")

# handler for filter selection
@bot.message_handler(func=lambda message: message.text.lower() in ['blur', 'rotate', 'salt and pepper', 'segment','grayscale','sharpen','emboss','invert colors','oil painting','cartoonize'])
def handle_filter(message):
try:
# Check if the user has previously sent an image
if message.chat.id in user_images:
# Get the image path
if 'concat_pending' in user_images[message.chat.id]:
image_path = user_images[message.chat.id]['concat_pending']
else:
image_path = user_images[message.chat.id]['first_image']

# apply the selected filter
img_processor = Img(image_path)
filter_name = message.text.lower()

if filter_name == 'blur':
processed_image = img_processor.blur()
elif filter_name == 'rotate':
processed_image = img_processor.rotate()
elif filter_name == 'salt and pepper':
processed_image = img_processor.salt_n_pepper()
elif filter_name == 'segment':
processed_image = img_processor.segment()
elif filter_name == 'grayscale':
processed_image = img_processor.grayscale()
elif filter_name == 'sharpen':
processed_image = img_processor.sharpen()
elif filter_name == 'emboss':
processed_image = img_processor.emboss()
elif filter_name == 'invert colors':
processed_image = img_processor.invert_colors()
elif filter_name == 'oil painting':
processed_image = img_processor.oil_painting()
elif filter_name == 'cartoonize':
processed_image = img_processor.cartoonize()
else:
processed_image = None

# check if the filter was applied successfully
if processed_image is not None:
# save and send the processed image
processed_image_path = img_processor.save_image(processed_image, suffix=f'_{filter_name.replace(" ", "_")}')
with open(processed_image_path, 'rb') as photo_file:
bot.send_photo(message.chat.id, photo_file)
else:
bot.reply_to(message, f"Error applying {filter_name} filter: Result is None.")

# remove the image path from the dict
del user_images[message.chat.id]
else:
bot.reply_to(message, "Please send an image first.")
except Exception as e:
bot.reply_to(message, f"Error processing image: {e}")


bot.polling()
Loading