Skip to content

Commit

Permalink
Update notify_telegram.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyassesalama authored Dec 5, 2024
1 parent 2f6c4c6 commit ca16080
Showing 1 changed file with 6 additions and 45 deletions.
51 changes: 6 additions & 45 deletions .github/workflows/notify_telegram.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
import os
import subprocess
import requests
import re

def get_git_commit_info():
commit_author = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%an']).decode('utf-8')
commit_message = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%s']).decode('utf-8')
commit_hash = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%H']).decode('utf-8')
commit_hash_short = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%h']).decode('utf-8')
return commit_author, commit_message, commit_hash, commit_hash_short
# Switch to the 'host' branch
subprocess.run(['git', 'checkout', 'host'], check=True)

def escape_markdown_v2(text):
escape_chars = r'_~`#+-=|{}.!'
return re.sub(r'([%s])' % re.escape(escape_chars), r'\\\1', text)

def main():
bot_token = os.environ['BOT_TOKEN']
chat_id = os.environ['CHAT_ID']
topic_id = os.environ.get('TOPIC_ID')

commit_author, commit_message, commit_hash, commit_hash_short = get_git_commit_info()

message = (
f"A new [commit](https://github.com/Sketchware-Pro/Sketchware-Pro/commit/{commit_hash}) has been merged to the repository by *{commit_author}*.\n\n"
f"*What has changed:*\n>{commit_message}\n\n"
f"I'm currently building it and will send you the APKs here within ~6 mins if the build is successful.\n\n#{commit_hash_short}"
)

escaped_message = escape_markdown_v2(message)

url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {
"chat_id": chat_id,
"text": escaped_message,
"parse_mode": "markdownv2",
"disable_web_page_preview": True
}
if topic_id:
payload["message_thread_id"] = topic_id

response = requests.post(url, json=payload)
if response.status_code != 200:
print(f"Failed to send message: {response.status_code} {response.text}")
else:
print("Message sent successfully.")

if __name__ == "__main__":
main()
# List all files in the current directory
for root, dirs, files in os.walk('.'):
for file in files:
print(os.path.join(root, file))

0 comments on commit ca16080

Please sign in to comment.