2022-07-15 15:52:54 +03:00
|
|
|
import requests
|
|
|
|
import constant
|
|
|
|
import environments
|
|
|
|
|
|
|
|
class Message:
|
|
|
|
'''A class that builds and sends messages'''
|
|
|
|
def __init__(self, envs: environments.Environment):
|
|
|
|
self.envs = envs
|
|
|
|
|
|
|
|
def __build_message(self):
|
|
|
|
'''Building message from different parts'''
|
|
|
|
message = f'''{constant.ICON[self.envs.status]} {self.envs.status}: <b>{self.envs.github_workflow}</b>\n
|
2022-07-15 17:17:48 +03:00
|
|
|
Repository: <b>{self.envs.github_repository}</b>'''
|
2022-07-15 15:52:54 +03:00
|
|
|
|
|
|
|
version = self.envs.get_version()
|
|
|
|
if version is not None:
|
|
|
|
message = f'''{message}
|
2022-07-15 17:17:48 +03:00
|
|
|
Version: <b>{version}</b>'''
|
2022-07-15 15:52:54 +03:00
|
|
|
|
2022-07-15 17:17:48 +03:00
|
|
|
if self.envs.docker_tags != "":
|
2022-07-15 15:52:54 +03:00
|
|
|
message = f'''{message}
|
2022-07-15 17:17:48 +03:00
|
|
|
Docker image tags: <b>{self.envs.docker_tags}</b>'''
|
2022-07-15 15:52:54 +03:00
|
|
|
|
|
|
|
if self.envs.include_commit_info == "true":
|
|
|
|
message = f'''{message}
|
2022-07-15 17:17:48 +03:00
|
|
|
Author: <b>{self.envs.github_actor}</b>
|
|
|
|
Commit message: <b>{self.envs.commit}</b>
|
|
|
|
<a href="{self.envs.get_commit_link()}">See changes</a>'''
|
2022-07-15 15:52:54 +03:00
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
def send_message(self):
|
|
|
|
'''Sending message to telegram chat'''
|
2022-07-15 17:17:48 +03:00
|
|
|
message = self.__build_message() if self.envs.custom_message == "" else self.envs.custom_message
|
2022-07-15 15:52:54 +03:00
|
|
|
parameters = {
|
|
|
|
'chat_id': self.envs.chat_id,
|
|
|
|
'text': message,
|
|
|
|
'parse_mode': 'HTML',
|
|
|
|
'disable_web_page_preview': True
|
|
|
|
}
|
|
|
|
|
|
|
|
request = requests.get(self.envs.get_link(), params = parameters)
|
|
|
|
request.raise_for_status()
|