Skip to content

Commit

Permalink
feat: added gammu
Browse files Browse the repository at this point in the history
  • Loading branch information
mglants committed Jun 14, 2024
1 parent 4c58a4b commit b818402
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
36 changes: 36 additions & 0 deletions apps/gammu-telegram/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
9 changes: 9 additions & 0 deletions apps/gammu-telegram/ci/latest.py
Original file line number Diff line number Diff line change
@@ -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))
6 changes: 6 additions & 0 deletions apps/gammu-telegram/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
12 changes: 12 additions & 0 deletions apps/gammu-telegram/metadata.yaml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions apps/gammu-telegram/scripts/gammu.conf.pre
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions apps/gammu-telegram/scripts/telegram.py
Original file line number Diff line number Diff line change
@@ -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 = "<b>{}</b>\n{} <b>({}</b>)".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)

0 comments on commit b818402

Please sign in to comment.