-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Nginx support for hosting static contents (#15)
* try to use name as webhook url * fixed wrong url * add nginx conf template and separated server configs * changed to SERVER LISTEN * add configs and scripts for serving static assets * fixed typo * updated readme * updated readme and .env.toml * updated readme add shields.io * updated links * updated LICENSE
- Loading branch information
1 parent
17096ef
commit 4fc6dce
Showing
10 changed files
with
179 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
# bot main configuration | ||
[bot] | ||
token = "" | ||
environment = "" | ||
token = "" # your bot token here | ||
name = "" # your bot name, for webhook uri | ||
environment = "" # "prod" or "dev" | ||
|
||
# for server of this bot | ||
[server] | ||
host = "" | ||
port = 443 | ||
port = 8443 | ||
listen = "0.0.0.0" | ||
|
||
# webhook configuration, can be same as [server], | ||
# change if your are behind a proxy | ||
[webhook] | ||
host = "" | ||
port = 443 | ||
|
||
# telegram webhook requires ssl | ||
[ssl] | ||
cert = "" | ||
priv = "" | ||
cert = "" # path to your fullchain.pem | ||
priv = "" # path to your priv.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
Random Public License (v1.1279127389172371293) | ||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
Version 2, December 2004 | ||
|
||
%$%^&()*Y*(HUIBXGSHJBUOSXUIHSUCIOHU(USISC(U)))ascascascasc | ||
OX8C()*()CS*()S*C)(SUCSO | ||
Copyright (C) 2004 Sam Hocevar <[email protected]> | ||
|
||
#0) HIOSCHIOHSCi(PSc()*S()C)(S*C)( | ||
#1) alskndialsdiasdijas | ||
#2) POSI)SV()jksbcjkasc | ||
#3) POSJCIOSJC(JCWJKLklandla) | ||
Everyone is permitted to copy and distribute verbatim or modified | ||
copies of this license document, and changing it is allowed as long | ||
as the name is changed. | ||
|
||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
|
||
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,93 @@ | ||
# Randomology | ||
A telegram bot to generate random stuff, I built this to chat with my friend by randomly | ||
<a href="https://github.com/ravachol-yang/randomology/actions"><img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/ravachol-yang/randomology/pytest.yml?style=for-the-badge&logo=github&label=tests"></a> <a href="https://t.me/randomology_bot"><img alt="Static Badge" src="https://img.shields.io/badge/telegram-grey?style=for-the-badge&logo=telegram"></a> | ||
|
||
|
||
A telegram bot to generate random stuff, I built this to chat with my friend randomly. | ||
Using [pyTelegramBotAPI](https://github.com/eternnoir/pyTelegramBotAPI) for talking with telegram server. | ||
The project structure is inspired by classic MVC web frameworks.. | ||
|
||
## Dependency | ||
- pyTelegrambotAPI | ||
- pytest (dev) | ||
- ffmpeg-python | ||
Install system dependencies: | ||
`ffmpeg` (for converting media), | ||
`nginx` (optional, for hosting static resources & proxy) | ||
``` shell | ||
# this is an example for debian-based distros | ||
sudo apt-get install ffmpeg | ||
# optional but highly recommanded | ||
sudo apt-get install nginx | ||
``` | ||
Use pip to install dependencies in `requirements.txt` | ||
``` shell | ||
python3 -m pip install -r requirements.txt | ||
``` | ||
|
||
## Testing | ||
run pytest | ||
## Test && Deploy | ||
The bot uses webhook in production environment, you will need a server reachable for telegram server and has `https` support (unless you want to use infinity polling...) | ||
**Here we go !!!!** | ||
### Configuration | ||
This repo includes a config example `.env.toml`. | ||
Copy the example config and change it to meet your own environment: | ||
``` shell | ||
# copy the config example, don't forget to change it | ||
cp .env.toml env.toml | ||
``` | ||
in `env.toml`: | ||
``` toml | ||
[bot] | ||
token = "" # your token here | ||
name = "" # your bot name, for webhook uri | ||
environment = "prod" # "prod" for production environment | ||
|
||
[server] | ||
host = "example.com" # set your own | ||
port = 8443 | ||
listen = "0.0.0.0" | ||
|
||
[webhook] | ||
host = "" | ||
port = 443 | ||
|
||
[ssl] | ||
cert = "/path/to/fullchain.pem" | ||
priv = "/path/to/priv.pem" | ||
``` | ||
### Testing | ||
run `pytest` && check if every test passes | ||
``` shell | ||
python3 -m pytest | ||
``` | ||
|
||
## Running | ||
In the project directory | ||
### Hosting static resources | ||
Static resources are hosted in `public/` and the bot-generated contents are under `storage/`, use `link_storage.sh` to create a symlic: | ||
``` shell | ||
./link_storage.sh | ||
# to remove it: | ||
# ./link_storage.sh remove | ||
``` | ||
copy and change the config file to configure Nginx: | ||
``` shell | ||
cp nginx.conf /etc/nginx/sites-available/example.com | ||
# don't forget to change it !! | ||
ln /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled | ||
``` | ||
restart `nginx.service` | ||
### Running | ||
In the project directory, run `python3` | ||
``` shell | ||
python3 bot.py | ||
``` | ||
> if you don't have https: | ||
telegram only allow webhooks with https, but you can still use `polling`: | ||
in `env.toml`,set `bot.environment` to `"dev"`: | ||
|
||
``` toml | ||
[bot] | ||
environment = "dev" | ||
``` | ||
now you can run it ! | ||
|
||
## LICENSE | ||
random LICENSE &^$%^&*()_)(*&^*) | ||
*(&%&^*&(*&%*^(&))) | ||
|
||
<a href="https://github.com/ravachol-yang/randomology/blob/master/LICENSE"><img alt="GitHub License" src="https://img.shields.io/github/license/ravachol-yang/randomology?style=for-the-badge"></a> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
|
||
FROM=$(pwd)/storage | ||
TO=$(pwd)/public/storage | ||
|
||
if [[ $1 == "remove" ]]; then | ||
rm $TO -v | ||
elif [[ -e $TO ]]; then | ||
echo $TO "already exists" | ||
else | ||
ln -s $FROM $TO -v | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Nginx config template | ||
# Don't forget to change it to meet your own env | ||
|
||
server { | ||
listen 80; | ||
listen 443 ssl; | ||
listen [::]:443 ssl; | ||
|
||
server_name example.com; | ||
|
||
ssl_certificate /path/to/fullchain.pem; | ||
ssl_certificate_key /path/to/privkey.pem; | ||
|
||
# redirect to https | ||
if ($scheme = http) { | ||
return 301 https://$server_name$request_uri; | ||
} | ||
|
||
location / { | ||
root /var/www/example.com/public; | ||
} | ||
|
||
# your webhook | ||
location /your-webhook-uri { | ||
proxy_pass http://127.0.0.1:8443/your-webhook-uri/; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
storage/** | ||
storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# allow nobody !!!!! | ||
User-agent: * | ||
Disallow: / |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters