Created
January 31, 2019 11:39
-
-
Save romanpeters/e6fc3fe654a22be0c9427c577f566c5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import time | |
import random | |
import datetime | |
import telepot | |
from pprint import pprint | |
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton, InlineQueryResultPhoto | |
import database as db | |
from redacted import API_KEY, CHAT_URL, BOT | |
BOTNAME = '@' + BOT | |
COMMAND_DICT = dict() | |
KEYWORD_DICT = dict() | |
class Decorator(object): | |
""" Base decorator """ | |
def __init__(self, string_key: str): | |
self.storage = dict() | |
self.string_key = string_key.lower() | |
def __call__(self, func): | |
self.storage[self.string_key] = func | |
return func | |
class Command(Decorator): | |
""" Command decorator """ | |
def __init__(self, string_key: str): | |
super().__init__(string_key) | |
self.storage = COMMAND_DICT | |
class Keyword(Decorator): | |
""" Keyword decorator """ | |
def __init__(self, string_key: str): | |
super().__init__(string_key) | |
self.storage = KEYWORD_DICT | |
def chat(msg): | |
"""on chat message""" | |
content_type, chat_type, chat_id = telepot.glance(msg) | |
pprint(msg) | |
session = db.Session() | |
user = db.User(id=msg['from']['id'], first_name=msg['from'].get('first_name'), last_name=msg['from'].get('last_name'), | |
username=msg['from'].get('username')) | |
session.merge(user) | |
session.commit() | |
if content_type == 'text': | |
print(msg['chat']['id'], msg['text']) | |
words = msg['text'].split() | |
for keyword, func in KEYWORD_DICT.items(): | |
if keyword in words: | |
func(msg) | |
if msg['text'][0] == '/': | |
command = command_parser(msg['text']) | |
if command: | |
COMMAND_DICT[command](msg) | |
@Keyword('ja') | |
def yee(msg): | |
session = db.Session() | |
user = session.query(db.User).filter_by(id=msg['from']['id']).first() | |
user.yees = user.yees + 1 | |
session.merge(user) | |
session.commit() | |
bot.sendSticker(msg['chat']['id'], "CAADBAAD9AAD8g85Ckn_Ea5lfb9IAg") | |
bot.sendMessage(msg['chat']['id'], f"y{'e' * user.yees}") | |
try: | |
bot.kickChatMember(msg['chat']['id'], msg['from']['id']) | |
except: | |
pass | |
else: | |
bot.sendMessage(msg['from']['id'], redacted.CHAT_URL) | |
@Keyword('candela') | |
def candela(msg): | |
bot.sendSticker(msg['chat']['id'], "CAADBAAD8wAD8g85CjYEZI_PSVYjAg") | |
@Command('yeederboard') | |
def yeederboard(msg): | |
session = db.Session() | |
users = session.query(db.User) | |
session.close() | |
scores = sorted(users, key=lambda k: k.yees) | |
message = ["```\n--YEEDERBOARD--"] | |
for u in scores[::-1]: | |
message.append(f"{u.yees} {u.first_name}") | |
message.append("```") | |
bot.sendMessage(msg['chat']['id'], '\n'.join(message), parse_mode='Markdown') | |
def start(msg): | |
"""Welke opdrachten zijn er?""" | |
chat_id = msg['chat']['id'] | |
unique_values = [] | |
commands = [] | |
for value in switch_case: | |
if switch_case[value] not in unique_values: | |
if value is not 'start': | |
unique_values.append(switch_case[value]) | |
commands.append(f"/{value} - {switch_case[value].__doc__}") | |
bot.sendMessage(chat_id, "".format('\n'.join(sorted(commands)))) | |
def command_parser(command_input: str) -> str: | |
"""Parses commands""" | |
command = command_input.split()[0][1:500].lower() # limit + make lowercase | |
if '@' in command: | |
i = command.index('@') | |
if command[i:i+len(BOTNAME)] == BOTNAME: | |
command = command[:i] | |
else: # ignore command | |
return | |
return command | |
if __name__ == '__main__': | |
bot = telepot.Bot(API_KEY) | |
answerer = telepot.helper.Answerer(bot) | |
print('Listening...') | |
bot.message_loop({'chat': chat, | |
}) | |
while 1: | |
time.sleep(10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment