gitea init

This commit is contained in:
yen3k 2023-08-07 11:09:04 +02:00
commit f158f1e66a
7 changed files with 163 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
TOKEN
SAVED
CHATID
send.py
Pipfile.lock

18
Dockerfile Normal file
View File

@ -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"]

12
Pipfile Normal file
View File

@ -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"

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# SESH Secretary
Associate telegram messages with a keyword to easily find them again

1
push.sh Executable file
View File

@ -0,0 +1 @@
docker buildx build --platform linux/arm64 -t yen3k/sesh-secretary:1.0 --push --file Dockerfile .

16
requirements.txt Normal file
View File

@ -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'

108
seshsecretary.py Executable file
View File

@ -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()