Exemplo n.º 1
0
class RadixEnschedeBot:
    db = None
    TOKEN = ""
    URL = "https://api.telegram.org/bot{}/".format(TOKEN)
    ADMIN = 0

    help_text = """
    === Basic commands ===
    (commands marked with "*" will be answered privately)
    /help * --- shows all commands.
    
    /info * --- some info about Tally
    /nick henk  --- change your nickname in this group to "henk" (max 12 letters).
    /nicks  --- overview of all nicknames 
    /products  --- overview of all added products
    /all_tallies  --- overview of all tallies
    /tallies * --- overview of your tallies
    /debtors --- list of people with a positive amount of (normal) tallies.
    /add grolschbier  --- add a product named "grolschbier" (max 12 letters)
    /all_history 10  --- show last 10 transactions (default 5, max. 99)
    /history 5 * --- show last 5 transactions by you (default 10, max. 99)
    /thanks henk  --- give henk a tally to thank him for something. (-1 for henk +1 for you)
    
    === Tally-ing ===
    You can Tally positivly and negatively between 1 and 99 
    Examples:
    +1  --- add one tally 
    16  --- add 16 tallies 
    -4  --- remove 4 tallies
    
    You can also tally some specific product
    Example:
    +1 coke  --- add one coke
    
    You can also tally some for someone else
    Example:
    sjaak 5  --- add 5 tallies for sjaak
    
    You can also tally some specific product for someone else
    Example:
    sjaak -3 beer  --- remove 3 beers for sjaak
    
    === STFU Tally ===
    If you start your sentence with a dot, Tally will ignore it.
    Example
    . hey Tally! Tally! hey Tally! TALLY!!!"""

    info_text = """Tally is a simple Telegram-bot He is created because someone was to lazy to stand up and tally his 
    beer. This someone rather preferred program a complete Telegram-bot. You're free to use Tally for your own 
    purposes, however Tally is confronted with alcoholic beverages on a regular basis. Therefore Tally, 
    nor it's maker can guarantee that Tally is and will stay functioning at a socially acceptable level. Honestly, 
    we wouldn't be surprised if Tally would get Korsakoff, or have some troubles with (temporary) black-outs. Let 
    alone that he stays alive. 
    
    - Wouter ([email protected])"""

    NOT_WHITESPACE = re.compile(r'[^s]')

    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/home/wouter/tally_out'
        self.stderr_path = '/home/wouter/tally_err'
        self.pidfile_path = '/tmp/tally.pid'
        self.pidfile_timeout = 5
        self.db = DBHelper()
        with open("config.json", "r") as data_file:
            data = json.load(data_file)
        self.ADMIN = data['ADMIN']
        self.TOKEN = data['TOKEN']
        self.URL = "https://api.telegram.org/bot{}/".format(self.TOKEN)

    def get_url(self, url):
        response = requests.get(url)
        content = response.content.decode("utf8")
        return content

    def get_json_from_url(self, url):
        content = self.get_url(url)
        js = json.loads(content)
        return js

    def get_updates(self, offset=None):
        url = self.URL + "getUpdates?timeout=1000"
        if offset:
            url += "&offset={}".format(offset)
        js = self.get_json_from_url(url)
        return js

    def get_last_chat_id_and_text(self, updates):
        num_updates = len(updates["result"])
        last_update = num_updates - 1
        text = updates["result"][last_update]["message"]["text"]
        chat_id = updates["result"][last_update]["message"]["chat"]["id"]
        return (text, chat_id)

    def get_last_update_id(self, updates):
        update_ids = []
        for update in updates["result"]:
            update_ids.append(int(update["update_id"]))
        return max(update_ids)

    def send_message(self, text, chat_id, reply_markup=None):
        text = urllib.parse.quote_plus(text)
        url = self.URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
        if reply_markup:
            url += "&reply_markup={}".format(reply_markup)
        self.get_url(url)

    def run(self):
        last_update_id = None
        while True:
            try:
                updates = self.get_updates(last_update_id)
                if "result" in updates:
                    if len(updates["result"]) > 0:
                        last_update_id = self.get_last_update_id(updates) + 1
                        self.extract_messages(updates)
            except ConnectionError as e:
                continue
            json_path = os.path.dirname(
                os.path.abspath(__file__)) + '/post.json'
            jsonFile = Path(json_path)
            print(jsonFile.is_file())
            if jsonFile.is_file():
                with open(json_path, 'r') as f:
                    data = f.read().replace('\n', '')
                    for tallyPost in self.decode_stacked(data):
                        x = tallyPost["amount"] + " " + tallyPost["product"]
                        self.handle_message(tallyPost["group"], x,
                                            tallyPost["user"], "", 'group')
                    f.close()
                os.remove(json_path)

    def decode_stacked(self, document, pos=0, decoder=json.JSONDecoder()):
        while True:
            match = self.NOT_WHITESPACE.search(document, pos)
            if not match:
                return
            pos = match.start()

            try:
                obj, pos = decoder.raw_decode(document, pos)
            except json.JSONDecodeError:
                # do something sensible if there's some error
                raise
            yield obj

    def extract_messages(self, updates):
        for update in updates["result"]:
            try:
                text = update["message"]["text"]
                chat = update["message"]["chat"]["id"]
                telegram_id = update["message"]["from"]["id"]
                name = update["message"]["from"]["first_name"]
                type = update["message"]["chat"]["type"]
                self.handle_message(chat, text, telegram_id, name, type)

            except Exception as e:
                print(e)
                traceback.print_stack()
                print(update)
                print("")

    def personal_message(self, chat, text, telegram_id, name):
        if str(telegram_id) != str(self.ADMIN):
            self.send_message("Add me to a group :)", telegram_id)
            return

        split_text = text.split()
        switcher = {'/add_chat': self.add_chat}
        fun = switcher.get(split_text[0], self.command_not_found)
        fun(chat, split_text, telegram_id)

    def add_chat(self, chat, split_text, telegram_id):
        self.db.add_chat(int(split_text[1]))
        self.send_message("Added " + str(split_text[1]), self.ADMIN)

    def handle_message(self, chat, text, telegram_id, name, type):
        text = text.lower()

        # Check if in group
        if type != 'group' and type != 'supergroup':
            self.personal_message(chat, text, telegram_id, name)
            return
        # Check if chat allowed
        if not self.db.check_chat(chat):
            self.send_message(
                "Ask Wouter van Harten (+31)6 833 24 277 to whitelist <" +
                str(chat) + ">", chat)
            self.send_message(
                "Activity from unknown chat <" + str(chat) +
                ">, maybe you can whitelist it with '/add_chat " + str(chat) +
                "' ?", self.ADMIN)
            return

        # Check for STFU
        if text[0] == ".":
            return
        # Check for command
        if text[0] == '/':
            self.handle_command(chat, text, telegram_id)
            return

        split_text = text.split()

        nsp = NumericStringParser()
        try:
            int(nsp.eval(split_text[0]).real)
            self.tally(split_text, chat, telegram_id, name)
            return
        except Exception as e:
            print(e)

        # Try for username
        user = self.db.get_user_by_name(chat, split_text[0])
        if user != False:
            del split_text[0]
            try:
                int(nsp.eval(split_text[0]).real)
                self.tally(split_text, chat, user.telegram_id, split_text[0],
                           False)
                return
            except Exception:
                self.send_message("unknown amount: " + split_text[0], chat)
                pass
            return
        self.send_message("Que? (" + text + ")", chat)

    def tally(self, split_text, chat, telegram_id, name, make_new_user=True):
        nsp = NumericStringParser()
        # We only drink an integer amount of real beer
        amount = int(nsp.eval(split_text[0]).real)
        if abs(amount) > 99:
            self.send_message(
                "Tally between -100 and 100, " + str(amount) + " given", chat)
            return
        if abs(amount) < 0.5:
            self.send_message("That's a bunch of nothing you have there", chat)
            return
        user = self.db.get_user_by_telegram_id(telegram_id)
        if (not make_new_user) & (user == False):
            self.send_message("Unknown user: "******"Unknown product: " + split_text[1], chat)
                return
        purchase = Purchase(user, product, amount, self.db.get_chat(chat))
        # Get old score and new score
        all_tallies = self.db.get_all_tallies(chat, user)
        if product.name in all_tallies.keys():
            new_score = copy.copy(all_tallies[product.name])
            old_score = new_score - amount
        else:
            old_score = 0
            new_score = amount
        # Tallied and balance message:
        message = "Tallied {1!s} {3!s} for {0!s} (current balance is {2!s} {3!s}).".format(
            user.name, amount, new_score, product.name)
        # Attach some additional message if called for If user remains on the wrong end with a positive tally,
        # add a simple notification & sometimes a personal message:
        if (old_score >= 0) and (new_score > 0) and (amount > 0):
            message += "\n{0!s} has run out of {3!s} and is consuming another person's {3!s}!".format(
                user.name, amount, new_score, product.name)
            # Every fourth product or tally of at least 4 products, remind the user personally
            if new_score % 4 == 0:
                self.snark(user, new_score, product)
            elif amount >= 4:
                self.snark(user, new_score, product)
        # If a user remains on the wrong end with a negative tally, a more encouraging message:
        elif (old_score >= 0) and (new_score > 0) and (amount < 0):
            message += "\n{0!s}, thank you for adding some {3!s} to your stock. You did not add enough to return to " \
                       "Tally's good graces, though!".format(
                user.name, amount, new_score, product.name)
        # Notify those who add exactly enough:
        elif (old_score >= 0) and (new_score == 0) and (amount < 0):
            message += "\n{0!s}, thank you for adding some {3!s} to your stock. Tally likes those who do their " \
                       "bookkeeping to the letter!".format(
                user.name, amount, new_score, product.name)
        # Warn a user if their last item is tallied:
        elif (old_score < 0) and (new_score >= 0):
            message += "\nBetter enjoy that {3!s}, {0!s}! You've depleted your stock!".format(
                user.name, amount, new_score, product.name)
            self.send_message(
                "{0!s}, your last {3!s} was just tallied!".format(
                    user.name, amount, new_score, product.name), telegram_id)
        # Send message & commit purchase to database
        self.send_message(message, chat)
        self.db.add_purchase(purchase)
        return

    def snark(self, user, new_score, product):
        # Unpack input
        productname = product.name
        telegram_id, username = user.telegram_id, user.name
        # Messages
        messages = [
            "Beste {0!s}, ter ere van deze speciale gelegenheid wil ik graag iets van de wijsheid van onze voormalige "
            "koningin met je delen:\n'Hee majesteit, ga eens {1!s} halen!'".
            format(username, productname),
            "Beste {0!s}, wat advies: {1!s} {2!s} schuld is {1!s} {2!s} schuld teveel!"
            .format(username, new_score, productname),
            "Beste {0!s}, wist je dat Albert Heijn XL tot 22:00 open is en ze daar {2!s} hebben?"
            .format(username, new_score, productname),
            "Beste {0!s}, voor jou is het geen {2!s}tijd, maar supermarkttijd!"
            .format(username, new_score, productname),
            "Je creëert nu een {2!s}probleem, en nee dat is geen poar neem".
            format(username, new_score, productname),
            "2 woorden, {3!s} letters: {2!s} halen!".format(
                username, new_score, productname,
                len(productname) + 5)
        ]
        # Random integer
        i = randint(0, len(messages))
        message = messages[i]
        # Alexcheck
        if (new_score > 19):
            extra_message = "\n\nOverweeg alsjeblieft het volgende zelfhulp nummer te bellen: Alex bierservice " \
                            "053-4338460 "
            message += extra_message
        else:
            extra_message = "\n\nRaadpleeg je agenda en https://www.biernet.nl/bier/aanbiedingen om dit probleem op " \
                            "te lossen. "
            message += extra_message
        # Send random message
        self.send_message(message, telegram_id)
        return

    def handle_command(self, chat, text, telegram_id):
        switcher = {
            '/help': self.show_help,
            '/info': self.show_info,
            '/nick': self.set_nick,
            '/nicks': self.show_nicks,
            '/products': self.show_products,
            '/debtors': self.show_debtors,
            '/all_tallies': self.show_all_tallies,
            '/tallies': self.show_tallies,
            '/add': self.add_product,
            '/all_history': self.show_all_history,
            '/history': self.show_history,
            '/thanks': self.thank_user,
            '/update': self.update_tally
        }
        split_text = text.split()
        command = split_text[0].split("@")[0]
        fun = switcher.get(command, self.command_not_found)
        fun(chat, split_text, telegram_id)
        return

    def command_not_found(self, chat, split_text, telegram_id):
        self.send_message("Command not found: " + split_text[0], chat)

    def show_help(self, chat, split_text, telegram_id):
        self.send_message(self.help_text, telegram_id)
        self.send_message(
            "Message answered privately. \nIf you didn't get my message, send me a private message and try again.",
            chat)

    def show_info(self, chat, split_text, telegram_id):
        self.send_message(self.info_text, telegram_id)
        self.send_message(
            "Message answered privately. \nIf you didn't get my message, send me a private message and try again.",
            chat)

    def set_nick(self, chat, split_text, telegram_id):
        if len(split_text) < 2:
            self.send_message("Provide new nick", chat)
            return
        if len(split_text[1]) > 12:
            self.send_message("Nick too long", chat)
            return
        if len(split_text[1]) < 2:
            self.send_message("Nick too short", chat)
            return
        user = self.db.get_user_by_telegram_id(telegram_id)
        old_nick = user.name
        user.name = split_text[1]
        self.db.add_user(user)
        self.send_message("Nick changed from " + old_nick + " to " + user.name,
                          chat)

    def show_nicks(self, chat, split_text, telegram_id):
        users = self.db.get_all_users(chat)
        message = "=== NICKS === \n"
        for user in users:
            message += str(user.telegram_id) + ": " + user.name + "\n"
        self.send_message(message, chat)

    def show_products(self, chat, split_text, telegram_id):
        products = self.db.get_all_products(chat)
        message = "=== PRODUCTS ===\n"
        for product in products:
            message += product.name + "\n"
        self.send_message(message, chat)

    def show_debtors(self, chat, split_text, telegram_id):
        users = self.db.get_all_users(chat)
        message = "=== DEBTORS ===\n"
        for user in users:
            shown_name = False
            totals = self.db.get_all_tallies(chat, user)
            for key, value in totals.items():
                if value > 0:
                    if not shown_name:
                        message += user.name + ":\n"
                        shown_name = True
                    message += key + ": " + str(value) + "\n"
            if shown_name:
                message += "\n"
        self.send_message(message, chat)

    def show_all_tallies(self, chat, split_text, telegram_id):
        users = self.db.get_all_users(chat)
        message = "=== All tallies ===\n"
        for user in users:
            shown_name = False
            totals = self.db.get_all_tallies(chat, user)
            for key, value in totals.items():
                if value != 0:
                    if not shown_name:
                        message += user.name + ":\n"
                        shown_name = True
                    message += key + ": " + str(value) + "\n"
            if shown_name:
                message += "\n"
        message += "Total:\n"
        allTallies = self.db.get_total_tallies(chat)
        for key, value in allTallies.items():
            message += key + ": " + str(value) + "\n"
        self.send_message(message, chat)

    def show_tallies(self, chat, split_text, telegram_id):
        message = "=== Tallies ===\n"
        user = self.db.get_user_by_telegram_id(telegram_id)
        totals = self.db.get_all_tallies(chat, user)
        for key, value in totals.items():
            message += key + ": " + str(value) + "\n"
        self.send_message(message, telegram_id)
        self.send_message(
            "Message answered privately. \nIf you didn't get my message, send me a private message and try again.",
            chat)

    def add_product(self, chat, split_text, telegram_id):
        if telegram_id != self.ADMIN:
            self.send_message(
                "🖕🖕🖕🖕🖕🖕🖕🖕🖕🖕🖕🖕🖕erw",
                chat)
            return
        self.db.add_product(Product(split_text[1], self.db.get_chat(chat)))
        self.show_products(chat, split_text, telegram_id)
        return

    def show_all_history(self, chat, split_text, telegram_id):
        if len(split_text) > 1:
            try:
                amount = int(split_text[1])
            except ValueError:
                self.send_message(
                    "Enter integer amount smaller than 99, " +
                    str(split_text[1]) + " given.", chat)
                return
        else:
            amount = 10
        purchases = self.db.get_last_purchases(chat, amount)
        message = "=== All history ===\n"
        for purchase in purchases:
            message += "(" + parse(purchase.date).strftime("%m/%d %H:%M") + ") " + str(purchase.amount) + " " + \
                       purchase.product.name + " by " + purchase.user.name + "\n"
        self.send_message(message, chat)

    def show_history(self, chat, split_text, telegram_id):
        if len(split_text) > 1:
            try:
                amount = int(split_text[1])
            except ValueError:
                self.send_message(
                    "Enter integer amount, " + split_text[1] + " given.", chat)
                return
        else:
            amount = 10
        user = self.db.get_user_by_telegram_id(telegram_id)
        if not user:
            self.send_message("User not found", chat)
            return
        purchases = self.db.get_last_purchases(chat, amount, user)
        message = "=== History ===\n"
        for purchase in purchases:
            message += "(" + parse(purchase.date).strftime("%m/%d %H:%M") + ") " + str(purchase.amount) + " " + \
                       purchase.product.name + " by " + purchase.user.name + "\n"
        self.send_message(message, telegram_id)
        self.send_message(
            "Message answered privately. \nIf you didn't get my message, send me a private message and try again.",
            chat)

    def thank_user(self, chat, split_text, telegram_id):
        if len(split_text) < 2:
            self.send_message("No receiver given", chat)
            return
        receiver = self.db.get_user_by_name(chat, split_text[1])
        if receiver == False:
            self.send_message("Unknown user: "******"Thanks " + receiver.name + "! \nI don't know what you did " + receiver.name + ", but " + user.name + \
                  " would like to thank you! \n" + user.name + \
                  " has granted you a tally from his/her own pocket \nEnjoy, Tally"
        self.send_message(message, chat)

    def update_tally(self, chat, split_text, telegram_id):
        if telegram_id != self.ADMIN:
            self.send_message("🖕🖕🖕🖕🖕🖕", chat)
            return
        f = open(
            os.path.dirname(os.path.abspath(__file__)) + "/" + "update_tally",
            "w+")
        self.send_message("I will update shortly..", chat)
        f.close()

    def test(self):
        jsonFile = Path('post.json')
        if jsonFile.is_file():
            with open('post.json', 'r') as f:
                data = f.read().replace('\n', '')
                for tallyPost in self.decode_stacked(data):
                    x = tallyPost["amount"] + " " + tallyPost["product"]
                    self.handle_message(tallyPost["group"], x,
                                        tallyPost["user"], "", 'group')
                f.close()
            os.remove('post.json')
Exemplo n.º 2
0
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    print(content_type, chat_type, chat_id)
    db = DBHelper()
    pprint(msg)

    dataBase.addChat_id(chat_id, db)
    dataBase.addUtente(msg['from']['id'], db, chat_type,
                       msg['from']['username'], msg['from']['first_name'])
    if (content_type == 'text'):
        dataBase.addMex(chat_id, msg['from']['id'], msg['text'], db)

        if ("/ann" in msg['text']) & (msg['from']['id'] == 223772637):
            for chat in dataBase.lista_chat:
                try:
                    bot.sendMessage(chat.chat_id, msg['text'][5:])
                    print("message sent")
                except telepot.exception.BotWasBlockedError:
                    print("user block the bot")

        if ('reply_to_message' in msg) & (msg['text'].lower() == "fugo nome"):
            last_name = ""
            if 'last_name' in msg['reply_to_message']['from']:
                last_name = msg['reply_to_message']['from']['last_name']

            bot.sendMessage(chat_id,
                            "si chiama: " +
                            msg['reply_to_message']['from']['first_name'] +
                            " " + last_name,
                            reply_to_message_id=msg['message_id'])

        if (msg['text'] == "ciao"):
            bot.sendMessage(chat_id, "afangul")

        if (msg['text'].lower() == "buonanotte"):
            n = randint(0, 6)
            print(n)
            now = datetime.datetime.now()
            print(now)
            if (now.hour >= 18 - 2) & (now.hour <= 20 - 2) & (n <= 3):
                bot.sendMessage(chat_id,
                                "mamma mia che poppante ahasha",
                                reply_to_message_id=msg['message_id'])
            elif (now.hour > 20 - 2) & (now.hour <= 22 - 2) & (n <= 3):
                bot.sendMessage(chat_id,
                                "di già",
                                reply_to_message_id=msg['message_id'])
            elif (now.hour >= 22 - 2) & (now.hour <= 2) & (n <= 4):
                bot.sendMessage(chat_id,
                                "buonanotte °^°",
                                reply_to_message_id=msg['message_id'])

        if (msg['text'] == "/start"):
            bot.sendMessage(
                chat_id,
                "Ciao, per conoscere tutte le funzionalità di questo bot digita /help"
            )

        if (msg['text'] == "/help"):
            bot.sendMessage(
                chat_id,
                "Questo bot è in grado di rispondere ad alcune domande postegli. E possiede alcuni comandi speciali: \n\n» /rfoto o rfoto/Rfoto: manda una foto casuale con didascalia casuale inviate nel gruppo stesso al costo di 2 jjm coin, per rimuovere una foto non gradita rispondere alla stessa digitando il comando /rimuovi \n\n» \"Jjm scherzo\" (in risposta a un utente): al costo di 200 jjm coin farà un bello scherzetto all'utente scelto\n\n» \"jjm coin\": mostrerà i coin che possiedi.\n\nPer conoscere ulteriori informazioni riguardo ai coin premi /help_coin"
            )

        if (msg['text'] == "/help_coin"):
            bot.sendMessage(
                chat_id,
                "I coin vengono guadagnati: \n 1 ogni 10 messaggi inviati\n 10 ogni complimento fatto a un utente (premere /complimento per la lista dei complimenti)\n\nI coin vengono persi:\n -50 ogni insulto fatto a un utente VIP (premere /insulto per la lista degli insulti)\n\nSe si vuole donare dei coin a un altro utente basta premere in risposta a un utente /dona seguito da uno spazio e l'importo che si vuole donare. Es: /dona 100\n\nQuando un utente raggiunge 1000 coin diventa Vip, il che significa che verrà difeso dal bot in caso di insulti da parte di altri utenti e guadagnerà 50 coin ad ogni insulto ricevuto, prelevati direttamente dall'utente che lo ha insultato."
            )

        if "?" in msg['text']:
            metti = True
            mex = msg['text'].lower()
            for domanda in domande:
                if (domanda.domanda == mex):
                    metti = False
                    r = randint(-1, len(domanda.risposte) - 1)
                    if (len(domanda.risposte)) > 0:
                        bot.sendMessage(chat_id,
                                        domanda.risposte[r],
                                        reply_to_message_id=msg['message_id'])
                        break
            if (metti):
                domanda = Domanda()
                domanda.domanda = mex
                domande.append(domanda)
                db.add_domanda(mex)
                print(domande[0].domanda)

        for chat in dataBase.lista_chat:
            if (chat.chat_id == chat_id):
                if (chat.gioco == True):
                    for sol in chat.gioco_soluzione:
                        if (sol.lower() in msg['text'].lower()):
                            for utente in dataBase.utenti:
                                if msg['from']['id'] == utente.id:
                                    chat.gioco = False
                                    del chat.gioco_soluzione[:]
                                    #giochi = giochi-1
                                    utente.coin += 50
                                    bot.sendMessage(
                                        chat_id,
                                        "BRAVO HAI VINTOOO 50 jjm COIN!",
                                        reply_to_message_id=msg['message_id'])
                                    db.add_utente_coin(msg['from']['id'], 50,
                                                       "COIN")
                                    break

        if (msg['text'] == "gioco reset") | (msg['text'] == "/gioco_reset"):
            for chat in dataBase.lista_chat:
                if (chat.chat_id == chat_id):
                    if (chat.gioco == True):
                        chat.gioco = False
                        del chat.gioco_soluzione[:]
                        bot.sendMessage(chat_id,
                                        "_gioco resettato_",
                                        parse_mode='Markdown')
                        break
                    else:
                        bot.sendMessage(chat_id,
                                        "_nessun gioco in corso_",
                                        parse_mode='Markdown')

        for utente in dataBase.utenti:
            if (utente.id == msg['from']['id']) & (utente.scherzo == True):
                utente.mex_scherzo += 1
                if (utente.mex_scherzo >= 7):
                    utente.mex_scherzo = 0
                    utente.scherzo = False
                r = randint(-1, len(insulti) - 1)
                insulto = insulti[r]
                bot.sendMessage(chat_id,
                                msg['text'] + " e sono" + " " + insulto)

        if (("gay" in msg['text']) |
            ("Gay" in msg['text'])) & (chat_type == "supergroup"):
            for utente in dataBase.utenti:
                if msg['from']['id'] == utente.id:
                    utente.coin += 1
                    db.add_utente_coin(msg['from']['id'], 1, "COIN")
                    break

        if (msg['text'] == "jjm coin") | (msg['text'] == "Jjm coin"):
            for utente in dataBase.utenti:
                if msg['from']['id'] == utente.id:
                    bot.sendMessage(chat_id,
                                    "Hai *" + str(utente.coin) + " jjm coin*.",
                                    reply_to_message_id=msg['message_id'],
                                    parse_mode='Markdown')
                    break

        if msg['text'] == "lista":
            for utente in dataBase.utenti:
                print(utente.id)

        if (msg['text'] == "/insulto"):
            if ('reply_to_message' in msg):
                if (msg['from']['id'] == 223772637):
                    if (insulti.__contains__(
                            msg['reply_to_message']['text'].lower())) == False:
                        insulti.append(msg['reply_to_message']['text'].lower())
                        db.add_chat("insulti",
                                    msg['reply_to_message']['text'].lower())
                else:
                    bot.sendMessage(chat_id,
                                    "_Chiedi il permesso a_ @ermanichino",
                                    reply_to_message_id=msg['message_id'],
                                    parse_mode='Markdown')
            else:
                bot.sendMessage(chat_id, insulti)

        if (msg['text'] == "/complimento"):
            if ('reply_to_message' in msg):
                if (msg['from']['id'] == 223772637):
                    if (complimenti.__contains__(
                            msg['reply_to_message']['text'].lower())) == False:
                        complimenti.append(
                            msg['reply_to_message']['text'].lower())
                        db.add_chat("complimenti",
                                    msg['reply_to_message']['text'].lower())
                else:
                    bot.sendMessage(chat_id,
                                    "_Chiedi il permesso a_ @ermanichino",
                                    reply_to_message_id=msg['message_id'],
                                    parse_mode='Markdown')
            else:
                bot.sendMessage(chat_id, complimenti)

        if (msg['text'] == "/epiteto"):
            if ('reply_to_message' in msg):
                if (msg['from']['id'] == 223772637):
                    if (epiteti.__contains__(
                            msg['reply_to_message']['text'])) == False:
                        epiteti.append(msg['reply_to_message']['text'])
                        db.add_chat("epiteti",
                                    msg['reply_to_message']['text'].lower())
                else:
                    bot.sendMessage(chat_id,
                                    "_Chiedi il permesso a_ @ermanichino",
                                    reply_to_message_id=msg['message_id'],
                                    parse_mode='Markdown')
            else:
                bot.sendMessage(chat_id, epiteti)

        if (msg['text'] == "addCoin") & (msg['from']['id'] == 223772637):
            for utente in dataBase.utenti:
                if utente.id == 223772637:
                    utente.coin += 10000
                    db.add_utente_coin(utente.id, 10000, "COIN")
                    break

        if ('reply_to_message' in msg):
            if ('photo' in msg['reply_to_message']):
                if (msg['text'] == "/rimuovi"):
                    for chat in dataBase.lista_chat:
                        if chat.chat_id == chat_id:
                            id_foto = msg['reply_to_message']['photo'][-1][
                                'file_id']
                            if (id_foto in chat.foto):
                                chat.foto.remove(id_foto)
                                db.delete_photo(id_foto)
                                bot.sendMessage(chat_id,
                                                "_Foto rimossa_",
                                                parse_mode='Markdown')
                            else:
                                bot.sendMessage(
                                    chat_id,
                                    "_Foto già rimossa o non presente_",
                                    parse_mode='Markdown')

            elif ('animation' in msg['reply_to_message']):
                0
            else:
                for domanda in domande:
                    if domanda.domanda == msg['reply_to_message'][
                            'text'].lower():
                        id_d = db.get_id(
                            "domande", "ID",
                            msg['reply_to_message']['text'].lower())
                        domanda.addRisp(msg['text'], id_d, db)
                        print(domanda.risposte[0])
                        break
                for insulto in insulti:
                    if (insulto in msg['text'].lower()) & (
                        ("è" in msg['text']) == False) & (
                            ("È" in msg['text']) == False) & (
                                msg['reply_to_message']['from']['is_bot']
                                == False):
                        utenteInsultato = Utente()
                        for utente in dataBase.utenti:
                            if utente.id == msg['reply_to_message']['from'][
                                    'id']:
                                utenteInsultato = utente
                                if utente.coin >= 1000:
                                    r = randint(-1, len(epiteti) - 1)
                                    bot.sendMessage(
                                        chat_id,
                                        epiteti[r],
                                        reply_to_message_id=msg['message_id'])
                        if utente.id == msg['from']['id']:
                            if (utenteInsultato.coin >= 1000):
                                utenteInsultato.coin += 50
                                db.add_utente_coin(utenteInsultato.id, 50,
                                                   "COIN")
                                if utente.coin < 50:
                                    utente.coin = 0
                                else:
                                    utente.coin -= 50
                                    db.add_utente_coin(utente.id, -50, "COIN")

            if (chat_type == 'supergroup') & (
                    msg['reply_to_message']['from']['id'] != msg['from']['id']
            ) & (msg['reply_to_message']['from']['is_bot'] == False):
                for complimento in complimenti:
                    if (complimento == msg['text'].lower()):
                        for utente in dataBase.utenti:
                            if utente.id == msg['from']['id']:
                                utente.coin += 10
                                db.add_utente_coin(utente.id, 10, "COIN")
                                break

            if ("/componi" in msg['text']):
                for chat in dataBase.lista_chat:
                    if (chat.chat_id == chat_id):
                        for utente in dataBase.utenti:
                            if (utente.id == msg['from']['id']):
                                if (utente.coin >= 10):
                                    utente.coin -= 10
                                    mex = msg['reply_to_message'][
                                        'text'].lower()
                                    m_words = mex.split()
                                    last_word = m_words[-1]
                                    list_mex = list()
                                    list_one = list()
                                    trovato = False
                                    for message in chat.messaggi:
                                        mess = message.lower()
                                        list_m = mess.split()
                                        if (last_word in list_m) & (len(
                                                list_m) > 1) & (mex != mess):
                                            if (list_m[0] == last_word):
                                                list_m.remove(last_word)
                                            list_mex.append(list_m)
                                            trovato = True
                                    if (trovato):
                                        r2 = randint(-1, len(list_mex) - 1)
                                        mex1 = list_mex[r2]
                                        mex2 = ""
                                        for m in mex1:
                                            mex2 = mex2 + " " + m
                                        bot.sendMessage(
                                            chat_id, "" + mex + "" + mex2)
                                        break
                                    elif (trovato == False):
                                        for message in chat.messaggi:
                                            mess = message.lower()
                                            list_m = mess.split()
                                            i = 0
                                            for w in reversed(m_words):
                                                list_m2 = m_words
                                                if (len(m_words) / 2):
                                                    if (i == 3):
                                                        break
                                                if (w in list_m) & (
                                                        len(list_m) >
                                                        1) & (mex != mess):
                                                    del list_m2[list_m2.
                                                                index(w):]
                                                    del list_m[0:list_m.
                                                               index(w)]
                                                    list_mex.append(list_m)
                                                    list_one.append(list_m2)
                                                    break
                                                i += 1
                                        r1 = randint(-1, len(list_mex) - 1)
                                        mex1 = list_one[r1]
                                        mex2 = list_mex[r1]
                                        m1 = ""
                                        for m in mex1:
                                            m1 = m1 + " " + m
                                        m2 = ""
                                        for m in mex2:
                                            m2 = m2 + " " + m
                                        bot.sendMessage(
                                            chat_id, "" + m1 + "" + m2)
                                        break
                                    else:
                                        bot.sendMessage(
                                            chat_id,
                                            "trovato un cazz o non hai 10 coin"
                                        )
                                        break

            if ("/dona" in msg['text']) & (msg['text'].__contains__("-")
                                           == False):
                num = msg['text'][6:]
                pagato = False
                utentePagante = Utente()
                utentePagato = Utente()
                for utente in dataBase.utenti:
                    if utente.id == msg['from']['id']:
                        if utente.coin >= int(num):
                            pagato = True
                            utentePagante = utente
                        else:
                            bot.sendMessage(
                                chat_id,
                                "Uaglio non hai tutti sti soldi!",
                                reply_to_message_id=msg['message_id'])
                            break
                    if utente.id == msg['reply_to_message']['from']['id']:
                        utentePagato = utente
                if pagato:
                    utentePagante.coin -= int(num)
                    db.add_utente_coin(utentePagante.id, int("-" + num),
                                       "COIN")
                    utentePagato.coin += int(num)
                    db.add_utente_coin(utentePagato.id, int(num), "COIN")
                    bot.sendMessage(
                        chat_id,
                        "Hai donato *" + num + " coin* a _" +
                        msg['reply_to_message']['from']['first_name'] +
                        "_ con successo.",
                        reply_to_message_id=msg['message_id'],
                        parse_mode='Markdown')

            if (msg['text'] == "jjm scherzo") | (msg['text'] == "Jjm scherzo"):
                utenteInfame = Utente()
                utenteScherzato = Utente()
                for utente in dataBase.utenti:
                    if utente.id == msg['from']['id']:
                        utenteInfame = utente
                    if utente.id == msg['reply_to_message']['from']['id']:
                        utenteScherzato = utente
                if (utenteInfame.coin >= 200) & (utenteScherzato.scherzo
                                                 == False):
                    utenteInfame.coin -= 200
                    db.add_utente_coin(msg['from']['id'], -200, "COIN")
                    utenteScherzato.scherzo = True
                elif utenteScherzato.scherzo == True:
                    bot.sendMessage(
                        chat_id,
                        "Lascia sto stu puveret che è già sotto scherz!",
                        reply_to_message_id=msg['message_id'])
                else:
                    bot.sendMessage(
                        chat_id,
                        "Non hai abbastanza soldi per sta bravata!",
                        reply_to_message_id=msg['message_id'])

        if ((msg['text'] == "rgioco") | (msg['text'] == "Rgioco") |
            (msg['text'] == "/rgioco")):
            for chat in dataBase.lista_chat:
                if (chat.chat_id == chat_id):
                    if (chat.gioco == False) & (len(chat.mex_gioco) > 50):
                        chat.gioco = True
                        #giochi = giochi+1
                        n = 0

                        n = randint(-1, len(chat.mex_gioco) - 1)
                        mex = chat.mex_gioco[n]
                        utente_sol = chat.mex_utenti[n]
                        username_utente = db.get_item("utenti", "USERNAME",
                                                      utente_sol)
                        firstname_utente = db.get_item("utenti", "FIRST_NAME",
                                                       utente_sol)
                        if (username_utente != None):
                            chat.gioco_soluzione.append(username_utente)
                        chat.gioco_soluzione.append(firstname_utente)
                        bot.sendMessage(
                            chat_id,
                            "*Indovina chi ha mandato il messaggio:*\n\n" +
                            mex +
                            "\n\n_Per rispondere digita il nome/username della persona._",
                            parse_mode='Markdown')
                        #bot.sendMessage(chat_id, username_utente+" "+firstname_utente)
                        break
                    elif (len(chat.mex_gioco) < 50):
                        bot.sendMessage(
                            chat_id,
                            "_troppi pochi messaggi per iniziare il gioco..._",
                            parse_mode='Markdown')
                    else:
                        bot.sendMessage(chat_id,
                                        "_c'è già un gioco in corso_",
                                        parse_mode='Markdown')

        if ((msg['text'] == "rfoto") | (msg['text'] == "Rfoto") |
            (msg['text'] == "/rfoto")):
            for chat in dataBase.lista_chat:
                if (chat.chat_id == chat_id):
                    if (len(chat.foto) > 0):
                        for utente in dataBase.utenti:
                            if (utente.id == msg['from']['id']):
                                if (utente.coin >= 2):
                                    utente.coin -= 2
                                    r = randint(-1, len(chat.messaggi) - 1)
                                    r2 = randint(-1, len(chat.foto) - 1)
                                    bot.sendPhoto(chat_id, chat.foto[r2],
                                                  chat.messaggi[r])
                                    break
                                else:
                                    bot.sendMessage(
                                        chat_id,
                                        "Non hai abbastanza *jjm coin* per questa funzione.",
                                        reply_to_message_id=msg['message_id'],
                                        parse_mode='Markdown')
                                    break
                    else:
                        bot.sendMessage(
                            chat_id,
                            "_Non sono ancora state inviate immagini su questo gruppo._",
                            parse_mode='Markdown')
                        break

    if (content_type == 'photo'):
        file_id = msg['photo'][-1]['file_id']
        dataBase.addFoto(chat_id, file_id, db, msg['from']['id'])