Package: thorunimore
Thor BotGatekeeper bot for the Unimore Informatica unofficial Telegram group network[Website|PyPI]NOTE: This bot will be replaced soon with its rewrite,Loki Bot. Development on this version has ceased.FunctionalityIf added as an administrator to a Telegram group, this bot will instantly kick any joining member who hasn't passed verification.Verification is performed by:visiting the bot's homepagepressing the "Verify" buttonlogging in via Google with a@studenti.unimore.itaccountfollowing the deep link to Telegrampressing the "Start" button in the bot chatanswering the few questions the bot asks about the user's configurationAdditionally, verified users of the bot may choose to make their real name available for lookups via a bot command.Verified members joining a monitored group which made their real name available are announced by the bot in the group.Installation via PyPIThis method is recommended only for development purposes.Create a new venv and enter it:$python-mvenvvenv$sourcevenv/bin/activateDownload through PyPI:$pipinstallthorunimoreInstall the packages required to connect to the desired SQL database:For PostgreSQL:$pipinstallpsycopg2-binarySet the following environment variables:The URI of the SQL database you want to useexportSQLALCHEMY_DATABASE_URI="postgresql://steffo@/thor_dev"A Google OAuth 2.0 client id and client secretexportGOOGLE_CLIENT_ID="000000000000-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com"exportGOOGLE_CLIENT_SECRET="aaaaaaaaaaaaaaaaaaaaaaaa"A random string of characters used to sign Telegram dataexportSECRET_KEY="Questo è proprio un bel test."api_id and api_hash for a Telegram applicationexportTELEGRAM_API_ID="1234567"exportTELEGRAM_API_HASH="abcdefabcdefabcdefabcdefabcdefab"The username and token of the Telegram botexportTELEGRAM_BOT_USERNAME="thorunimorebot"exportTELEGRAM_BOT_TOKEN="1111111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"The desired logging level and formatexportLOG_LEVEL="DEBUG"exportLOG_FORMAT="{asctime}\t| {name}\t| {message}"The url at which web is hostedexportBASE_URL="http://lo.steffo.eu:30008"The url to join the Telegram groupexportGROUP_URL="https://t.me/joinchat/AAAAAAAAAAAAAAAAAAAAAA"Run both of the project's processes simultaneously:$python-mthorunimore.telegram&$python-mthorunimore.web&Configuring as a SystemD unitThis section assumes the project's files are located in/opt/thorunimore.Installgunicornin the previously created venv:$pipinstallgunicornCreate thebot-thorunimoresystemd unit by creating the/etc/systemd/system/bot-thorunimore.servicefile:[Unit]Name=bot-thorunimoreDescription=A moderator bot for the Unimore Informatica groupRequires=network-online.target postgresql.serviceAfter=network-online.target nss-lookup.target[Service]Type=execUser=thorunimoreWorkingDirectory=/opt/thorunimoreExecStart=/opt/thorunimore/venv/bin/python -OO -m thorunimore.telegramEnvironment=PYTHONUNBUFFERED=1[Install]WantedBy=multi-user.targetCreate theweb-thorunimoresystemd unit by creating the/etc/systemd/system/web-thorunimore.servicefile:[Unit]Name=web-thorunimoreDescription=Thorunimore Gunicorn ServerWants=network-online.target postgresql.serviceAfter=network-online.target nss-lookup.target[Service]Type=execUser=thorunimoreWorkingDirectory=/opt/thorunimoreExecStart=/opt/thorunimore/venv/bin/gunicorn -b 127.0.0.1:30008 thorunimore.web.__main__:reverse_proxy_app[Install]WantedBy=multi-user.targetCreate the/etc/systemd/system/bot-thorunimore.d/override.confand/etc/systemd/system/web-thorunimore.d/override.confcontaining the previously configured variables, so that they are passed to the SystemD unit:[Service]Environment="SQLALCHEMY_DATABASE_URI=postgresql://thorunimore@/thor_prod"Environment="GOOGLE_CLIENT_ID=000000000000-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com"Environment="GOOGLE_CLIENT_SECRET=aaaaaaaaaaaaaaaaaaaaaaaa"Environment="SECRET_KEY=Questo è proprio un bel server."Environment="TELEGRAM_API_ID=1234567"Environment="TELEGRAM_API_HASH=abcdefabcdefabcdefabcdefabcdefab"Environment="TELEGRAM_BOT_USERNAME=thorunimorebot"Environment="TELEGRAM_BOT_TOKEN=1111111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"Environment="LOG_LEVEL=DEBUG"Environment="LOG_FORMAT={asctime}\t| {name}\t| {message}"Environment="BASE_URL=https://thor.steffo.eu"Environment="GROUP_URL=https://t.me/joinchat/AAAAAAAAAAAAAAAAAAAAAA"Start (and optionally enable) both services:#systemctlstart"bot-thorunimore""web-thorunimore"#systemctlenable"bot-thorunimore""web-thorunimore"Reverse-proxy the web service with a web server such as Apache HTTPd:<VirtualHost*:80>ServerName"thor.steffo.eu"Redirectpermanent"/""https://thor.steffo.eu/"</VirtualHost><VirtualHost*:443>ServerName"thor.steffo.eu"ProxyPass"/""http://127.0.0.1:30008/"ProxyPassReverse"/""http://127.0.0.1:30008/"RequestHeaderset"X-Forwarded-Proto"expr=%{REQUEST_SCHEME}SSLEngineonSSLCertificateFile"/root/.acme.sh/*.steffo.eu/fullchain.cer"SSLCertificateKeyFile"/root/.acme.sh/*.steffo.eu/*.steffo.eu.key"</VirtualHost>#a2ensiterp-thorunimoreInstallation via DockerThis method is recommended for production deployments.Two Docker images are provided,thorunimore-webandthorunimore-telegram, which only require configuration of the environment and setup of a reverse proxy.