Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Jul 31, 2017
0 parents commit 07007bd
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tg_bot/config.py
*.pyc
.idea/
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sqlalchemy
python-telegram-bot
Empty file added tg_bot/__init__.py
Empty file.
96 changes: 96 additions & 0 deletions tg_bot/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import logging
from pprint import pprint

from telegram.ext import Updater, CommandHandler, MessageHandler, BaseFilter

from tg_bot.config import Development as Configuration
from tg_bot.sql import add_note_to_db, get_note, rm_note


class SimaoFilter(BaseFilter):
def filter(self, message):
return 'simao' in message.text.lower()

simao_filter = SimaoFilter()


def test(bot, update):
# pprint(eval(str(update)))
update.effective_message.reply_text("Hola tester")


def save(bot, update, args):
chat_id = update.effective_chat.id
if len(args) >= 2:
notename = args[0]
del args[0]
note_data = " ".join(args)

add_note_to_db(chat_id, notename, note_data)
update.effective_message.reply_text("yas! added " + notename)
else:
update.effective_message.reply_text("Dude, theres no note")


def get(bot, update, args):
chat_id = update.effective_chat.id
if len(args) > 1:
notename = args[0]

note = get_note(chat_id, notename)
if note:
update.effective_message.reply_text(note.value)
return
else:
update.effective_message.reply_text("This doesn't exist")
else:
update.effective_message.reply_text("Get rekt")


def delete(bot, update, args):
chat_id = update.effective_chat.id
notename = args[0]

rm_note(chat_id, notename)
update.effective_message.reply_text("Successfully removed note")


def start(bot, update):
update.effective_message.reply_text("Yo, whadup?")


def reply_simshit(bot, update):
update.effective_message.reply_text("Did you mean: simshit?")


def main():
updater = Updater(Configuration.API_KEY)
dispatcher = updater.dispatcher

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

LOGGER = logging.getLogger(__name__)

test_handler = CommandHandler("test", test)
start_handler = CommandHandler("start", start)
save_handler = CommandHandler("save", save, pass_args=True)
get_handler = CommandHandler("get", get, pass_args=True)
delete_handler = CommandHandler("delete", delete, pass_args=True)
simao_handler = MessageHandler(simao_filter, reply_simshit)

dispatcher.add_handler(test_handler)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(save_handler)
dispatcher.add_handler(get_handler)
dispatcher.add_handler(delete_handler)
dispatcher.add_handler(simao_handler)

updater.start_polling()
updater.idle()


if __name__ == '__main__':
main()
19 changes: 19 additions & 0 deletions tg_bot/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from sqlalchemy import Column, Text, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class Notes(Base):
__tablename__ = "notes"
chat_id = Column(String(14), primary_key=True)
name = Column(Text, primary_key=True)
value = Column(Text, nullable=False)

def __init__(self, chat_id, name, value):
self.chat_id = chat_id
self.name = name
self.value = value

def __repr__(self):
return "<Note %s>" % self.name
14 changes: 14 additions & 0 deletions tg_bot/sample_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create a new config.py file in same dir and import, then extend this class.
class Config(object):
API_KEY = "YOUR KEY HERE"
OWNER_ID = "YOUR ID HERE" # If you dont know, run the bot and do /id in your private chat with it
LOGGER = True
SQLALCHEMY_DATABASE_URI = 'sqldbtype://username:pw@hostname:port/db_name'


class Production(Config):
LOGGER = False


class Development(Config):
LOGGER = True
34 changes: 34 additions & 0 deletions tg_bot/sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from tg_bot.models import Base, Notes
from tg_bot.config import Development as Configuration

engine = create_engine(Configuration.SQLALCHEMY_DATABASE_URI)

Base.metadata.create_all(engine)

Session = sessionmaker(autoflush=False)
sess = Session(bind=engine)


# Note: chat_id's are stored as strings because the int is over 32 bits.
def add_note_to_db(chat_id, notename, note_data):
prev = sess.query(Notes).get((str(chat_id), notename))
if prev:
prev.value = note_data
note = prev
else:
note = Notes(str(chat_id), notename, note_data)
sess.add(note)
sess.commit()


def get_note(chat_id, notename):
return sess.query(Notes).get((str(chat_id), notename))


def rm_note(chat_id, notename):
note = sess.query(Notes).get((str(chat_id), notename))
sess.delete(note)
sess.commit()

0 comments on commit 07007bd

Please sign in to comment.