From f158f1e66a241d6993491af279b22428dfbcc143 Mon Sep 17 00:00:00 2001 From: yen3k Date: Mon, 7 Aug 2023 11:09:04 +0200 Subject: [PATCH] gitea init --- .gitignore | 5 +++ Dockerfile | 18 ++++++++ Pipfile | 12 ++++++ README.md | 3 ++ push.sh | 1 + requirements.txt | 16 +++++++ seshsecretary.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 Pipfile create mode 100644 README.md create mode 100755 push.sh create mode 100644 requirements.txt create mode 100755 seshsecretary.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a28756e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +TOKEN +SAVED +CHATID +send.py +Pipfile.lock diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d8a50ed --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# syntax=docker/dockerfile:1 + +FROM python:3.10-slim-bullseye + +WORKDIR /app + +RUN apt-get update && apt-get install -y \ + gcc \ + libffi-dev \ + libssl-dev + +COPY requirements.txt requirements.txt + +RUN pip install -r requirements.txt + +COPY seshsecretary.py seshsecretary.py + +CMD ["python", "seshsecretary.py"] diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..4eb03ae --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +python-telegram-bot = "*" + +[requires] +python_version = "3.8" diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d8d639 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# SESH Secretary + +Associate telegram messages with a keyword to easily find them again diff --git a/push.sh b/push.sh new file mode 100755 index 0000000..48422e6 --- /dev/null +++ b/push.sh @@ -0,0 +1 @@ +docker buildx build --platform linux/arm64 -t yen3k/sesh-secretary:1.0 --push --file Dockerfile . diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..460339f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,16 @@ +# +# These requirements were autogenerated by pipenv +# To regenerate from the project's Pipfile, run: +# +# pipenv lock --requirements +# + +-i https://pypi.org/simple +certifi==2020.6.20 +cffi==1.14.2 +cryptography==3.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' +decorator==4.4.2 +pycparser==2.20; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3' +python-telegram-bot==12.8 +six==1.15.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3' +tornado==6.0.4; python_version >= '3.5' diff --git a/seshsecretary.py b/seshsecretary.py new file mode 100755 index 0000000..bd42b97 --- /dev/null +++ b/seshsecretary.py @@ -0,0 +1,108 @@ +#!/usr/bin/python + +from telegram.ext import Updater, CommandHandler, Filters +import os +from random import randrange +import sys + +SAVED_PATH = '/var/lib/seshsecretary/saved.txt' +CHATIDS_PATH = '/var/lib/seshsecretary/chatids.txt' + +def _list(update, context): + chat_id = update.effective_chat.id + + if not check_chat_id(chat_id): + return + + response = "" + with open(SAVED_PATH, "r") as f: + for line in f: + response += line.split()[0] + "\n" + if response == "": + context.bot.send_message(chat_id, "Ingen gemte beskeder :(", 'MARKDOWN') + else: + context.bot.send_message(chat_id, response, 'MARKDOWN') + +def random(update, context): + chat_id = update.effective_chat.id + + if not check_chat_id(chat_id): + return + + with open(SAVED_PATH, "r") as f: + lines = f.readlines() + message = lines[randrange(len(lines))] + message_id = message.split()[1] + from_chat_id = message.split()[2] + context.bot.forward_message(chat_id, from_chat_id, message_id) + +def get(update, context): + chat_id = update.effective_chat.id + + if not check_chat_id(chat_id): + return + + message_id = "" + name = update.message.text.split()[1].lower() + with open(SAVED_PATH, "r") as f: + for line in f: + if name == line.split()[0]: + message_id = line.split()[1] + from_chat_id = line.split()[2] + break + + if message_id == "": + update.message.reply_text( + "{} findes ik!".format(name)) + else: + context.bot.forward_message(chat_id, from_chat_id, message_id) + +def save(update, context): + chat_id = update.effective_chat.id + + if not check_chat_id(chat_id): + return + + name = update.message.text.split()[1].lower() + message_id = update.message.reply_to_message.message_id + cont = True + with open(SAVED_PATH, "r") as f: + for line in f: + if name == line.split()[0]: + cont = False + break + if cont: + with open(SAVED_PATH, "a") as f: + f.write(name + " " + str(message_id) + " " + str(chat_id) + "\n") + update.message.reply_text(name + " er gemt!") + else: + update.message.reply_text(name + " findes allerede :(") + +def main(): + if not os.path.isfile(SAVED_PATH): + open(SAVED_PATH, 'a').close() + if not os.path.isfile(CHATIDS_PATH): + sys.exit("No chat id supplied") + + token = os.getenv('SESHSECRETARY_TOKEN') + if token is None: + sys.exit("Missing environment variable: SESHSECRETARY_TOKEN") + + updater = Updater(token, use_context=True) + + updater.dispatcher.add_handler(CommandHandler("list", _list)) + updater.dispatcher.add_handler(CommandHandler("get", get)) + updater.dispatcher.add_handler(CommandHandler("random", random)) + updater.dispatcher.add_handler(CommandHandler("save", save, Filters.reply)) + + updater.start_polling() + updater.idle() + +def check_chat_id(chatid): + with open(CHATIDS_PATH) as f: + lines = f.readlines() + lines = [line.rstrip() for line in lines] + return str(chatid) in lines + +if __name__ == '__main__': + main()