Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug with text max length #4

Merged
merged 1 commit into from
Jan 11, 2024
Merged
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
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
tg-sendmail (0.3.0) stable; urgency=medium

* Fixed bug with too long messages (max 4096 characters)

-- Oleg Smedyuk <oleg.smedyuk@gmail.com> Thu, 11 Jan 2024 20:45:00 +0200

tg-sendmail (0.2.2) stable; urgency=medium

* Added nodename to the message header
Expand Down
41 changes: 29 additions & 12 deletions src/sendmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from mimetypes import guess_type
from urllib import request

TG_MAX_TEXT_LENGTH = 4096

config = configparser.ConfigParser()
config_filepaths = [
'sendmail.ini', '/etc/tg-sendmail.ini', '/etc/sendmail.ini',
Expand Down Expand Up @@ -128,7 +130,14 @@ def generate_message(email):


def send(message, token, chat_id):
long_message = ''
url = f'https://api.telegram.org/bot{token}/sendMessage'
if len(message) > TG_MAX_TEXT_LENGTH:
long_message = message
message = (
f'{message[:500]}\n\n'
'<b>Message is too long. See attached file below</b>'
)
payload = {
'chat_id': chat_id,
'text': message,
Expand All @@ -140,34 +149,42 @@ def send(message, token, chat_id):
}
data = json.dumps(payload).encode()
req = request.Request(url, data, headers)
with urllib.request.urlopen(req) as response:
with request.urlopen(req) as response:
res = response.read()
logger.debug('Response: %s', res.decode())
if long_message:
send_file("long_message.txt", long_message.encode(), token, chat_id)


def send_file(filepath, token, chat_id):
body = encode_multipart_formdata(filepath)
def send_file(filepath, content, token, chat_id):
body = encode_multipart_formdata(filepath, content)
print(body)
headers = {
'Content-Type': 'multipart/form-data; boundary=boundary',
}
url = f'https://api.telegram.org/bot{token}/sendDocument?chat_id={chat_id}'
req = request.Request(url, body, headers)
with urllib.request.urlopen(req) as response:
with request.urlopen(req) as response:
res = response.read()
logger.debug('Response: %s', res.decode())


def encode_multipart_formdata(filepath):
filename = os.path.basename(filepath)
mimetype, _ = guess_type(filepath)
mimetype = mimetype or 'application/octet-stream'
def read_content(filepath):
with open(filepath, 'rb') as file:
content = file.read()
return file.read()


def get_content_type(filepath):
mimetype, _ = guess_type(filepath)
return mimetype or 'application/octet-stream'


def encode_multipart_formdata(filepath: str, content: bytes):
return b'\r\n'.join([
b'--boundary',
b'Content-Disposition: form-data; name="document"; '
b'filename="%s"' % filename.encode(),
b'Content-Type: %s' % mimetype.encode(),
b'filename="%s"' % os.path.basename(filepath).encode(),
b'Content-Type: %s' % get_content_type(filepath).encode(),
b'',
b'%s' % content,
b'--boundary--',
Expand All @@ -189,7 +206,7 @@ def encode_multipart_formdata(filepath):
if not valid_credentials:
logger.warning('Please fill /etc/tg-sendmail.ini configuration file!')
exit(1)
send_file(args.send_file, bot_token, chat_id)
send_file(args.send_file, read_content(args.send_file), bot_token, chat_id)
exit()
email = prepare_email(args.F, args.f, args.remains, args.t)
logger.debug('Prepared email: %s', email.as_string())
Expand Down