From b818402ed8b6eaefc28f504c83ce8bb750ad0b1a Mon Sep 17 00:00:00 2001 From: MGlants Date: Fri, 14 Jun 2024 04:34:10 +0300 Subject: [PATCH] feat: added gammu --- apps/gammu-telegram/Dockerfile | 36 ++++++++++++++++++++++ apps/gammu-telegram/ci/latest.py | 9 ++++++ apps/gammu-telegram/entrypoint.sh | 6 ++++ apps/gammu-telegram/metadata.yaml | 12 ++++++++ apps/gammu-telegram/scripts/gammu.conf.pre | 21 +++++++++++++ apps/gammu-telegram/scripts/telegram.py | 35 +++++++++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 apps/gammu-telegram/Dockerfile create mode 100644 apps/gammu-telegram/ci/latest.py create mode 100755 apps/gammu-telegram/entrypoint.sh create mode 100644 apps/gammu-telegram/metadata.yaml create mode 100644 apps/gammu-telegram/scripts/gammu.conf.pre create mode 100755 apps/gammu-telegram/scripts/telegram.py diff --git a/apps/gammu-telegram/Dockerfile b/apps/gammu-telegram/Dockerfile new file mode 100644 index 0000000..c0def1b --- /dev/null +++ b/apps/gammu-telegram/Dockerfile @@ -0,0 +1,36 @@ +# syntax=docker/dockerfile:experimental + +# Use the Alpine Linux 3 base image +FROM docker.io/alpine:3.20.0 + +# Define build arguments +ARG TARGETPLATFORM +ARG TARGETARCH +ARG TARGETOS + +# Set the maintainer label +LABEL xyz.glants.image.target_platform=$TARGETPLATFORM +LABEL xyz.glants.image.target_architecture=$TARGETARCH +LABEL xyz.glants.image.target_os=$TARGETOS +LABEL org.opencontainers.image.title="gammu-telegram" +LABEL org.opencontainers.image.source="https://github.com/gammu/gammu" + +RUN apk add --update --no-cache python3 py3-requests bash gammu-smsd envsubst && \ + rm -rf /var/cache/apk/* + +ADD --chown=nobody:nobody ./scripts /scripts +ADD --chown=nobody:nobody ./entrypoint.sh /entrypoint.sh +RUN mkdir /data +RUN chown nobody:nobody /data + +RUN mkdir /app +RUN chown nobody:nobody /app +# Set the user to nobody +USER nobody + +ENV GAMMU_DEVICE=/dev/ttyUSB0 +ENV GAMMU_CONNECTION=at + +ENTRYPOINT ["/entrypoint.sh"] + +CMD ["gammu-smsd", "-c", "/app/gammu.conf"] diff --git a/apps/gammu-telegram/ci/latest.py b/apps/gammu-telegram/ci/latest.py new file mode 100644 index 0000000..4c747cc --- /dev/null +++ b/apps/gammu-telegram/ci/latest.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +def get_latest(channel): + return "v0.0.1" + +if __name__ == "__main__": + import sys + channel = sys.argv[1] + print(get_latest(channel)) \ No newline at end of file diff --git a/apps/gammu-telegram/entrypoint.sh b/apps/gammu-telegram/entrypoint.sh new file mode 100755 index 0000000..f5d947f --- /dev/null +++ b/apps/gammu-telegram/entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/bash +mkdir -p /data/{inbox,outbox,sent,error} +envsubst < /scripts/gammu.conf.pre \ + > ~/app/gammu.conf +# Run the original command +exec "$@" diff --git a/apps/gammu-telegram/metadata.yaml b/apps/gammu-telegram/metadata.yaml new file mode 100644 index 0000000..7f07f67 --- /dev/null +++ b/apps/gammu-telegram/metadata.yaml @@ -0,0 +1,12 @@ +--- +app: "gammu-telegram" +base: false +semantic_versioning: true +channels: + - name: "stable" + platforms: + - linux/arm64 + - linux/amd64 + stable: true + tests: + enabled: false diff --git a/apps/gammu-telegram/scripts/gammu.conf.pre b/apps/gammu-telegram/scripts/gammu.conf.pre new file mode 100644 index 0000000..c587167 --- /dev/null +++ b/apps/gammu-telegram/scripts/gammu.conf.pre @@ -0,0 +1,21 @@ +[gammu] +device = ${GAMMU_DEVICE} +connection = ${GAMMU_CONNECTION} + +[smsd] +Service = files +PIN = XXXX +LogFile = /dev/stdout +HangupCalls = 1 +RunOnReceive = /usr/bin/python3 /scripts/sms_received.py +InboxPath = /data/inbox/ +OutboxPath = /data/outbox/ +SentSMSPath = /data/sent/ +ErrorSMSPath = /data/error/ +debuglevel = 1 +InboxFormat = unicode +OutboxFormat = unicode +TransmitFormat = auto +DeliveryReport = sms +DeliveryReportDelay = 7200 +CheckSecurity = 0 \ No newline at end of file diff --git a/apps/gammu-telegram/scripts/telegram.py b/apps/gammu-telegram/scripts/telegram.py new file mode 100755 index 0000000..5aace91 --- /dev/null +++ b/apps/gammu-telegram/scripts/telegram.py @@ -0,0 +1,35 @@ +# Receive messages from GSM modem via Gammu to Telegram +# https://github.com/captainmarshin/phone/ +# +#!/usr/bin/env python + +import os +import sys +import requests +from urllib.parse import quote +numparts = int(os.environ["DECODED_PARTS"]) +phone = os.environ["GAMMU_NUMBER"] +sender = str(os.environ["SMS_1_NUMBER"]) +botkey = os.environ["TELEGRAM_BOTKEY"] +chatid = os.environ["TELEGRAM_CHATID"] +text = "" + +if numparts == 0: + text = os.environ["SMS_1_TEXT"] +else: + for i in range(1, numparts + 1): + varname = "DECODED_%d_TEXT" % i + if varname in os.environ: + text = text + os.environ[varname] +text = text.replace("<#>", "") +# Construct the Telegram message with HTML formatting +html_formatted_message = "{}\n{} ({})".format(phone, text, sender) +encoded_message = quote(html_formatted_message, safe='') +headers = { "Accept": "application/json", "Content-Type": "application/json" } +# Construct the Telegram API URL with the properly formatted message +chat = "https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}&parse_mode=HTML".format(botkey, chatid, encoded_message) +# Save the chat_url to a log file +log_filename = "/dev/stdout" +with open(log_filename, "a") as log_file: + log_file.write(chat + "\n") +notify = requests.get(chat, headers = headers) \ No newline at end of file