110 lines
3.2 KiB
Python
Executable File
110 lines
3.2 KiB
Python
Executable File
#!/usr/bin/python
|
|
|
|
from telegram import Update
|
|
from telegram.ext import Application, CommandHandler, filters
|
|
import os
|
|
from random import randrange
|
|
import sys
|
|
|
|
SAVED_PATH = '/var/lib/seshsecretary/saved.txt'
|
|
CHATIDS_PATH = '/var/lib/seshsecretary/chatids.txt'
|
|
|
|
|
|
async 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 == "":
|
|
await context.bot.send_message(chat_id, "Ingen gemte beskeder :(", 'MARKDOWN')
|
|
else:
|
|
await context.bot.send_message(chat_id, response, 'MARKDOWN')
|
|
|
|
async 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]
|
|
await context.bot.forward_message(chat_id, from_chat_id, message_id)
|
|
|
|
async 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 == "":
|
|
await update.message.reply_text(
|
|
"{} findes ik!".format(name))
|
|
else:
|
|
await context.bot.forward_message(chat_id, from_chat_id, message_id)
|
|
|
|
async 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")
|
|
await update.message.reply_text(name + " er gemt!")
|
|
else:
|
|
await 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")
|
|
|
|
application = Application.builder().token(token).build()
|
|
|
|
application.add_handler(CommandHandler("list", _list))
|
|
application.add_handler(CommandHandler("get", get))
|
|
application.add_handler(CommandHandler("random", random))
|
|
application.add_handler(CommandHandler("save", save, filters.REPLY))
|
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
|
|
|
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()
|