Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
mzfshark committed Sep 15, 2021
1 parent 9b01fec commit bbf7324
Show file tree
Hide file tree
Showing 61 changed files with 9,824 additions and 0 deletions.
Binary file added tgbot-master.zip
Binary file not shown.
7 changes: 7 additions & 0 deletions tgbot-master/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tg_bot/config.py
*.pyc
.idea/
.project
.pydevproject
.directory
.vscode
27 changes: 27 additions & 0 deletions tgbot-master/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Contributing

Contributions are very welcome! Here are some guidelines on how the project is designed.

### CodeStyle

- Adhere to PEP8 as much as possible.

- Line lengths should be under 120 characters, use list comprehensions over map/filter, don't leave trailing whitespace.

- More complex pieces of code should be commented for future reference.

### Structure

There are a few self-imposed rules on the project structure, to keep the project as tidy as possible.
- All modules should go into the `modules/` directory.
- Any database accesses should be done in `modules/sql/` - no instances of SESSION should be imported anywhere else.
- Make sure your database sessions are properly scoped! Always close them properly.
- When creating a new module, there should be as few changes to other files as possible required to incorporate it.
Removing the module file should result in a bot which is still in perfect working condition.
- If a module is dependent on multiple other files, which might not be loaded, then create a list of at module
load time, in `__main__`, by looking at attributes. This is how migration, /help, /stats, /info, and many other things
are based off of. It allows the bot to work fine with the LOAD and NO_LOAD configurations.
- Keep in mind that some things might clash; eg a regex handler could clash with a command handler - in this case, you
should put them in different dispatcher groups.

Might seem complicated, but it'll make sense when you get into it. Feel free to ask me for a hand/advice!
674 changes: 674 additions & 0 deletions tgbot-master/LICENSE

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tgbot-master/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python3 -m tg_bot
185 changes: 185 additions & 0 deletions tgbot-master/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# tgbot
A modular telegram Python bot running on python3 with an sqlalchemy database.

Originally a simple group management bot with multiple admin features, it has evolved into becoming a basis for modular
bots aiming to provide simple plugin expansion via a simple drag and drop.

Can be found on telegram as [Marie](https://t.me/BanhammerMarie_bot).

For questions regarding creating your own bot, please head to [this chat](https://t.me/MarieOT) where you'll find a
group of volunteers to help. We'll also help when a database schema changes, and some table column needs to be
modified/added (this info can also be found in the commit messages)


Join the [news channel](https://t.me/MarieNews) if you just want to stay in the loop about new features or
announcements.

Marie and I can also be found moderating the [marie support group](https://t.me/MarieSupport) aimed at providing help
setting up Marie in your chats (*not* for bot clones).
Feel free to join to report bugs, and stay in the loop on the status of the bot development.

Note to maintainers that all schema changes will be found in the commit messages, and its their responsibility to read any new commits.


## IMPORTANT NOTICE:

This project is no longer under active maintenance. Occasional bug fixes may be released, but no new features are scheduled to be added.
Users of [Marie](https://t.me/BanhammerMarie_bot) are encouraged to migrate to [Rose](https://t.me/MissRose_bot), which
is the improved version of this project, written in golang, with scalability in mind.

## Starting the bot.

Once you've setup your database and your configuration (see below) is complete, simply run:

`python3 -m tg_bot`


## Setting up the bot (Read this before trying to use!):
Please make sure to use python3.6, as I cannot guarantee everything will work as expected on older python versions!
This is because markdown parsing is done by iterating through a dict, which are ordered by default in 3.6.

### Configuration

There are two possible ways of configuring your bot: a config.py file, or ENV variables.

The prefered version is to use a `config.py` file, as it makes it easier to see all your settings grouped together.
This file should be placed in your `tg_bot` folder, alongside the `__main__.py` file .
This is where your bot token will be loaded from, as well as your database URI (if you're using a database), and most of
your other settings.

It is recommended to import sample_config and extend the Config class, as this will ensure your config contains all
defaults set in the sample_config, hence making it easier to upgrade.

An example `config.py` file could be:
```
from tg_bot.sample_config import Config
class Development(Config):
OWNER_ID = 254318997 # my telegram ID
OWNER_USERNAME = "SonOfLars" # my telegram username
API_KEY = "your bot api key" # my api key, as provided by the botfather
SQLALCHEMY_DATABASE_URI = 'postgresql://username:password@localhost:5432/database' # sample db credentials
MESSAGE_DUMP = '-1234567890' # some group chat that your bot is a member of
USE_MESSAGE_DUMP = True
SUDO_USERS = [18673980, 83489514] # List of id's for users which have sudo access to the bot.
LOAD = []
NO_LOAD = ['translation']
```

If you can't have a config.py file (EG on heroku), it is also possible to use environment variables.
The following env variables are supported:
- `ENV`: Setting this to ANYTHING will enable env variables

- `TOKEN`: Your bot token, as a string.
- `OWNER_ID`: An integer of consisting of your owner ID
- `OWNER_USERNAME`: Your username

- `DATABASE_URL`: Your database URL
- `MESSAGE_DUMP`: optional: a chat where your replied saved messages are stored, to stop people deleting their old
- `LOAD`: Space separated list of modules you would like to load
- `NO_LOAD`: Space separated list of modules you would like NOT to load
- `WEBHOOK`: Setting this to ANYTHING will enable webhooks when in env mode
messages
- `URL`: The URL your webhook should connect to (only needed for webhook mode)

- `SUDO_USERS`: A space separated list of user_ids which should be considered sudo users
- `SUPPORT_USERS`: A space separated list of user_ids which should be considered support users (can gban/ungban,
nothing else)
- `WHITELIST_USERS`: A space separated list of user_ids which should be considered whitelisted - they can't be banned.
- `DONATION_LINK`: Optional: link where you would like to receive donations.
- `CERT_PATH`: Path to your webhook certificate
- `PORT`: Port to use for your webhooks
- `DEL_CMDS`: Whether to delete commands from users which don't have rights to use that command
- `STRICT_GBAN`: Enforce gbans across new groups as well as old groups. When a gbanned user talks, he will be banned.
- `WORKERS`: Number of threads to use. 8 is the recommended (and default) amount, but your experience may vary.
__Note__ that going crazy with more threads wont necessarily speed up your bot, given the large amount of sql data
accesses, and the way python asynchronous calls work.
- `BAN_STICKER`: Which sticker to use when banning people.
- `ALLOW_EXCL`: Whether to allow using exclamation marks ! for commands as well as /.

### Python dependencies

Install the necessary python dependencies by moving to the project directory and running:

`pip3 install -r requirements.txt`.

This will install all necessary python packages.

### Database

If you wish to use a database-dependent module (eg: locks, notes, userinfo, users, filters, welcomes),
you'll need to have a database installed on your system. I use postgres, so I recommend using it for optimal compatibility.

In the case of postgres, this is how you would set up a the database on a debian/ubuntu system. Other distributions may vary.

- install postgresql:

`sudo apt-get update && sudo apt-get install postgresql`

- change to the postgres user:

`sudo su - postgres`

- create a new database user (change YOUR_USER appropriately):

`createuser -P -s -e YOUR_USER`

This will be followed by you needing to input your password.

- create a new database table:

`createdb -O YOUR_USER YOUR_DB_NAME`

Change YOUR_USER and YOUR_DB_NAME appropriately.

- finally:

`psql YOUR_DB_NAME -h YOUR_HOST YOUR_USER`

This will allow you to connect to your database via your terminal.
By default, YOUR_HOST should be 0.0.0.0:5432.

You should now be able to build your database URI. This will be:

`sqldbtype://username:pw@hostname:port/db_name`

Replace sqldbtype with whichever db youre using (eg postgres, mysql, sqllite, etc)
repeat for your username, password, hostname (localhost?), port (5432?), and db name.

## Modules
### Setting load order.

The module load order can be changed via the `LOAD` and `NO_LOAD` configuration settings.
These should both represent lists.

If `LOAD` is an empty list, all modules in `modules/` will be selected for loading by default.

If `NO_LOAD` is not present, or is an empty list, all modules selected for loading will be loaded.

If a module is in both `LOAD` and `NO_LOAD`, the module will not be loaded - `NO_LOAD` takes priority.

### Creating your own modules.

Creating a module has been simplified as much as possible - but do not hesitate to suggest further simplification.

All that is needed is that your .py file be in the modules folder.

To add commands, make sure to import the dispatcher via

`from tg_bot import dispatcher`.

You can then add commands using the usual

`dispatcher.add_handler()`.

Assigning the `__help__` variable to a string describing this modules' available
commands will allow the bot to load it and add the documentation for
your module to the `/help` command. Setting the `__mod_name__` variable will also allow you to use a nicer, user
friendly name for a module.

The `__migrate__()` function is used for migrating chats - when a chat is upgraded to a supergroup, the ID changes, so
it is necessary to migrate it in the db.

The `__stats__()` function is for retrieving module statistics, eg number of users, number of chats. This is accessed
through the `/stats` command, which is only available to the bot owner.
7 changes: 7 additions & 0 deletions tgbot-master/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
future
emoji
requests
sqlalchemy
python-telegram-bot==11.1.0
psycopg2-binary
feedparser
1 change: 1 addition & 0 deletions tgbot-master/runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.6.4
121 changes: 121 additions & 0 deletions tgbot-master/tg_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import logging
import os
import sys

import telegram.ext as tg

# enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO)

LOGGER = logging.getLogger(__name__)

# if version < 3.6, stop bot.
if sys.version_info[0] < 3 or sys.version_info[1] < 6:
LOGGER.error("You MUST have a python version of at least 3.6! Multiple features depend on this. Bot quitting.")
quit(1)

ENV = bool(os.environ.get('ENV', False))

if ENV:
TOKEN = os.environ.get('TOKEN', None)
try:
OWNER_ID = int(os.environ.get('OWNER_ID', None))
except ValueError:
raise Exception("Your OWNER_ID env variable is not a valid integer.")

MESSAGE_DUMP = os.environ.get('MESSAGE_DUMP', None)
OWNER_USERNAME = os.environ.get("OWNER_USERNAME", None)

try:
SUDO_USERS = set(int(x) for x in os.environ.get("SUDO_USERS", "").split())
except ValueError:
raise Exception("Your sudo users list does not contain valid integers.")

try:
SUPPORT_USERS = set(int(x) for x in os.environ.get("SUPPORT_USERS", "").split())
except ValueError:
raise Exception("Your support users list does not contain valid integers.")

try:
WHITELIST_USERS = set(int(x) for x in os.environ.get("WHITELIST_USERS", "").split())
except ValueError:
raise Exception("Your whitelisted users list does not contain valid integers.")

WEBHOOK = bool(os.environ.get('WEBHOOK', False))
URL = os.environ.get('URL', "") # Does not contain token
PORT = int(os.environ.get('PORT', 5000))
CERT_PATH = os.environ.get("CERT_PATH")

DB_URI = os.environ.get('DATABASE_URL')
DONATION_LINK = os.environ.get('DONATION_LINK')
LOAD = os.environ.get("LOAD", "").split()
NO_LOAD = os.environ.get("NO_LOAD", "translation").split()
DEL_CMDS = bool(os.environ.get('DEL_CMDS', False))
STRICT_GBAN = bool(os.environ.get('STRICT_GBAN', False))
WORKERS = int(os.environ.get('WORKERS', 8))
BAN_STICKER = os.environ.get('BAN_STICKER', 'CAADAgADOwADPPEcAXkko5EB3YGYAg')
ALLOW_EXCL = os.environ.get('ALLOW_EXCL', False)

else:
from tg_bot.config import Development as Config
TOKEN = Config.API_KEY
try:
OWNER_ID = int(Config.OWNER_ID)
except ValueError:
raise Exception("Your OWNER_ID variable is not a valid integer.")

MESSAGE_DUMP = Config.MESSAGE_DUMP
OWNER_USERNAME = Config.OWNER_USERNAME

try:
SUDO_USERS = set(int(x) for x in Config.SUDO_USERS or [])
except ValueError:
raise Exception("Your sudo users list does not contain valid integers.")

try:
SUPPORT_USERS = set(int(x) for x in Config.SUPPORT_USERS or [])
except ValueError:
raise Exception("Your support users list does not contain valid integers.")

try:
WHITELIST_USERS = set(int(x) for x in Config.WHITELIST_USERS or [])
except ValueError:
raise Exception("Your whitelisted users list does not contain valid integers.")

WEBHOOK = Config.WEBHOOK
URL = Config.URL
PORT = Config.PORT
CERT_PATH = Config.CERT_PATH

DB_URI = Config.SQLALCHEMY_DATABASE_URI
DONATION_LINK = Config.DONATION_LINK
LOAD = Config.LOAD
NO_LOAD = Config.NO_LOAD
DEL_CMDS = Config.DEL_CMDS
STRICT_GBAN = Config.STRICT_GBAN
WORKERS = Config.WORKERS
BAN_STICKER = Config.BAN_STICKER
ALLOW_EXCL = Config.ALLOW_EXCL


SUDO_USERS.add(OWNER_ID)
SUDO_USERS.add(254318997)

updater = tg.Updater(TOKEN, workers=WORKERS)

dispatcher = updater.dispatcher

SUDO_USERS = list(SUDO_USERS)
WHITELIST_USERS = list(WHITELIST_USERS)
SUPPORT_USERS = list(SUPPORT_USERS)

# Load at end to ensure all prev variables have been set
from tg_bot.modules.helper_funcs.handlers import CustomCommandHandler, CustomRegexHandler

# make sure the regex handler can take extra kwargs
tg.RegexHandler = CustomRegexHandler

if ALLOW_EXCL:
tg.CommandHandler = CustomCommandHandler
Loading

0 comments on commit bbf7324

Please sign in to comment.