Cleaned up README and moved config info out of version control

Clarified naming of some things.
This commit is contained in:
Stan James 2018-01-28 22:29:50 -07:00
parent 68cf51aa3e
commit 01019f1186
6 changed files with 84 additions and 39 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Never commit config data
config.cnf
# Byte-compiled / optimized / DLL files
*.py[cod]
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
origin-telegram-logger-bot

View File

@ -1,23 +1,36 @@
# Telegram Group Chat Logger # Telegram Group Chat Logger
This is a bot that logs public Group Chats to an SQL Database. This is a bot that logs public Group Chats to a Postgres Database.
## Installation ## Installation
- Required: Python 3.X , PostgreSQL, Telegram Bot - Required: Python 3.x, pip, PostgreSQL
- Clone this repo - Clone this repo
- `pip install requirements.txt` - `pip install --upgrade -r requirements.txt`
## Bot/Group Setup ## Telegram Bot Setup
- Create a group - Create a bot by talking to BotFather: https://core.telegram.org/bots#creating-a-new-bot
- Store your Telegram Bot Token in environment variable `BOT_TOKEN`. It will look similar to this:
```
export TELEGRAM_BOT_TOKEN="4813829027:ADJFKAf0plousH2EZ2jBfxxRWFld3oK34ya"
```
- Create a Telegram group.
- Add your bot to the group like so: https://stackoverflow.com/questions/37338101/how-to-add-a-bot-to-a-telegram-group - Add your bot to the group like so: https://stackoverflow.com/questions/37338101/how-to-add-a-bot-to-a-telegram-group
- your bot only sees commands. Use `/setprivacy` with `@BotFather` in order to allow it to see all messages in a group. - Your bot only sees commands. Use `/setprivacy` with `@BotFather` in order to allow it to see all messages in a group.
## Running the bot ## Running the bot
- Create a Database, eg. `origindb` - Create a Postgres database.
- Store your Telegram Bot Token in an environment variable, eg. `echo $ORIGINTOKEN` ```
- Add your DB credentials to `model.py` eg. `engine = create_engine('postgresql://postgres:<password>@localhost:5432/<databasename>')` $psql
- Run: `python model.py` to setup the DB tables, etc. CREATE DATABASE telegram_bot_db;
```
- Copy `config.cnf.sample` to `config.cnf` and edit the databse connection URL. File should look like this:
```
[postgres]
postgres_url = postgresql://<user>:<password>@localhost:5432/<databasename>
```
- Run: `python model.py` to setup the DB tables.
- Run: `python bot.py` to start logger - Run: `python bot.py` to start logger
- When a messages is successfully logged, `message logged` will be displayed.

2
bot.py
View File

@ -92,7 +92,7 @@ def error(bot, update, error):
def main(): def main():
"""Start the bot.""" """Start the bot."""
# Create the EventHandler and pass it your bot's token. # Create the EventHandler and pass it your bot's token.
updater = Updater(os.environ["ORIGINTOKEN"]) updater = Updater(os.environ["TELEGRAM_BOT_TOKEN"])
# Get the dispatcher to register handlers # Get the dispatcher to register handlers
dp = updater.dispatcher dp = updater.dispatcher

2
config.cnf.sample Normal file
View File

@ -0,0 +1,2 @@
[postgres]
postgres_url = postgresql://<username>:<password>@localhost:5432/<databasename>

View File

@ -1,6 +1,10 @@
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func
from sqlalchemy.orm import relationship, backref from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("config.cnf")
postgres_url = config.get('postgres', 'postgres_url')
''' '''
This model has been referenced from: https://www.pythoncentral.io/sqlalchemy-orm-examples/ This model has been referenced from: https://www.pythoncentral.io/sqlalchemy-orm-examples/
@ -29,9 +33,10 @@ class Message(Base):
from sqlalchemy import create_engine from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:<password>@localhost:5432/origindb') engine = create_engine(postgres_url)
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
session = sessionmaker() session = sessionmaker()
session.configure(bind=engine) session.configure(bind=engine)
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
print "Created database model"

View File

@ -1,3 +1,4 @@
psycopg2==2.7.3.2 psycopg2==2.7.3.2
python-telegram-bot==9.0.0 python-telegram-bot==9.0.0
SQLAlchemy==1.2.2 SQLAlchemy==1.2.2
configparser==3.5.0