Add init system integration
This commit is contained in:
parent
018ad977c2
commit
99a8a3420f
@ -1,8 +1,10 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
import sys
|
||||||
|
sys.path.append("/var/telelog")
|
||||||
from telethon import TelegramClient, events, functions, types
|
from telethon import TelegramClient, events, functions, types
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
client = TelegramClient('telelog', api_id, api_hash)
|
client = TelegramClient('/var/telelog/log', api_id, api_hash)
|
||||||
|
|
||||||
@client.on(events.NewMessage)
|
@client.on(events.NewMessage)
|
||||||
async def log(event):
|
async def log(event):
|
||||||
@ -10,7 +12,7 @@ async def log(event):
|
|||||||
if event.sender_id != myid:
|
if event.sender_id != myid:
|
||||||
#Incoming message
|
#Incoming message
|
||||||
try:
|
try:
|
||||||
f = open(f'logs/{event.chat_id}')
|
f = open(f'/var/telelog/logs/{event.chat_id}')
|
||||||
except IOError:
|
except IOError:
|
||||||
chat = await event.get_input_chat()
|
chat = await event.get_input_chat()
|
||||||
rpl = "This account does not accept private messages. "
|
rpl = "This account does not accept private messages. "
|
||||||
@ -18,11 +20,11 @@ async def log(event):
|
|||||||
rpl += "Please reach out to me in a common group if applicable. "
|
rpl += "Please reach out to me in a common group if applicable. "
|
||||||
await event.respond(rpl)
|
await event.respond(rpl)
|
||||||
await client(functions.contacts.BlockRequest(chat))
|
await client(functions.contacts.BlockRequest(chat))
|
||||||
f = open(f'logs/{event.chat_id}', "a")
|
f = open(f'/var/telelog/logs/{event.chat_id}', "a")
|
||||||
f.write("← ")
|
f.write("← ")
|
||||||
else:
|
else:
|
||||||
#Outgoing message
|
#Outgoing message
|
||||||
f = open(f'logs/{event.chat_id}', "a")
|
f = open(f'/var/telelog/logs/{event.chat_id}', "a")
|
||||||
f.write("→ ")
|
f.write("→ ")
|
||||||
f.write(event.raw_text + "\n")
|
f.write(event.raw_text + "\n")
|
||||||
f.close()
|
f.close()
|
||||||
@ -44,7 +46,7 @@ async def log(event):
|
|||||||
rpl += os.popen("ls logs").read()
|
rpl += os.popen("ls logs").read()
|
||||||
await event.edit(rpl)
|
await event.edit(rpl)
|
||||||
else:
|
else:
|
||||||
chat = f'logs/{arr[1]}'
|
chat = f'/var/telelog/logs/{arr[1]}'
|
||||||
f = open(chat)
|
f = open(chat)
|
||||||
await event.edit(f.read())
|
await event.edit(f.read())
|
||||||
|
|
@ -1,13 +1,15 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
import sys
|
||||||
|
sys.path.append("/var/telelog")
|
||||||
from telethon.sync import TelegramClient
|
from telethon.sync import TelegramClient
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
from telethon.tl.types import User
|
from telethon.tl.types import User
|
||||||
|
|
||||||
with TelegramClient('telelog', api_id, api_hash) as client:
|
with TelegramClient('/var/telelog/log', api_id, api_hash) as client:
|
||||||
for dialog in client.iter_dialogs():
|
for dialog in client.iter_dialogs():
|
||||||
entity = dialog.entity
|
entity = dialog.entity
|
||||||
if isinstance(entity, User):
|
if isinstance(entity, User):
|
||||||
f = open(f'logs/{dialog.id}', "a")
|
f = open(f'/var/telelog/logs/{dialog.id}', "a")
|
||||||
f.write("Message history for " + str(dialog.id) + ":\n")
|
f.write("Message history for " + str(dialog.id) + ":\n")
|
||||||
f.close()
|
f.close()
|
7
init/openrc
Normal file
7
init/openrc
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#!/sbin/openrc-run
|
||||||
|
|
||||||
|
description="Telegram logger"
|
||||||
|
command="/usr/bin/telelog"
|
||||||
|
command_user="log:log"
|
||||||
|
command_background=true
|
||||||
|
pidfile="/run/ubot.pid"
|
12
init/systemd
Normal file
12
init/systemd
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Telegram logger
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=log
|
||||||
|
ExecStart=/usr/bin/telelog
|
||||||
|
Restart=always
|
||||||
|
RestartSec=0
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
63
install
63
install
@ -1,5 +1,9 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Preliminary sanity checks
|
||||||
|
[ $(id -u) != 0 ] && echo "Please run this installer as root." && exit 1
|
||||||
|
[ $0 != ./install ] && echo "Not launched from repository root." && exit 1
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
mkconfig(){
|
mkconfig(){
|
||||||
echo "Please get your credentials from https://my.telegram.org."
|
echo "Please get your credentials from https://my.telegram.org."
|
||||||
@ -13,13 +17,58 @@ mkconfig(){
|
|||||||
echo "myid = $myid" >> config.py
|
echo "myid = $myid" >> config.py
|
||||||
}
|
}
|
||||||
|
|
||||||
# Preliminary sanity checks
|
mkuser(){
|
||||||
[ $(id -u) = 0 ] && echo "Please do not run as root." && exit 1
|
# Create user account, distro mess awaits here...
|
||||||
[ ! -f log.py ] && echo "log.py not found, quitting." && exit 1
|
# Debian has both useradd and adduser
|
||||||
[ $0 != ./install ] && echo "Not launched from current directory." && exit 1
|
if [ -e /sbin/useradd ]
|
||||||
|
then
|
||||||
|
useradd -r telelog
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
# Alpine only has adduser (Busybox)
|
||||||
|
if [ -e /sbin/adduser ]
|
||||||
|
then
|
||||||
|
adduser -S telelog
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
addfiles(){
|
||||||
|
# Install executable and config
|
||||||
|
install -dm770 -o root -g telelog /var/telelog
|
||||||
|
install -m640 -o root -g telelog config.py /var/ubot/config.py
|
||||||
|
install -m755 -o root -g root bin/telelog /usr/bin/telelog
|
||||||
|
|
||||||
|
# Install service files on installed inits
|
||||||
|
if [ -e /sbin/openrc ]
|
||||||
|
then
|
||||||
|
install -m755 init/openrc /etc/init.d/telelog
|
||||||
|
ln -s /sbin/openrc /etc/periodic/15min/openrc
|
||||||
|
rc-update add telelog default
|
||||||
|
elif [ -e /lib/systemd/systemd ]
|
||||||
|
then
|
||||||
|
install -m644 init/systemd /etc/systemd/system/telelog.service
|
||||||
|
systemctl enable telelog
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
[ $(which apt) ] && apt install python3-pip
|
||||||
|
[ $(which apk) ] && apk add py3-pip
|
||||||
|
pip3 install -U telethon
|
||||||
|
# Generate config if not built yet
|
||||||
[ ! -f config.py ] && mkconfig
|
[ ! -f config.py ] && mkconfig
|
||||||
pip3 install -U telethon --user
|
# Create a new user
|
||||||
mkdir logs
|
mkuser
|
||||||
[ -f setup.py ] && python3 setup.py
|
# Install files if config is (now) present
|
||||||
|
[ -f config.py ] && addfiles
|
||||||
|
|
||||||
|
# Run Telethon for the first time
|
||||||
|
echo "Please log in to Telegram now."
|
||||||
|
umask 0077
|
||||||
|
[ -f setup.py ] && su bot -s /usr/bin/python3 $PWD/setup.py
|
||||||
|
|
||||||
|
# Start the service
|
||||||
|
service telelog start
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user