예제 #1
0
def webhook_handler():
    if flask.request.method == "POST":

        # Retrieve the message in JSON and then transform it to Telegram object
        update = telegram.Update.de_json(flask.request.get_json(force=True),
                                         bot)

        if update.message:
            # Regular message
            text = update.message.text
            user_id = update.message.from_user.id
            chat_id = update.message.chat_id
            username = update.message.from_user.username
            message_id = None
        elif update.callback_query:
            # Callback query
            text = update.callback_query.data
            user_id = update.callback_query.from_user.id
            chat_id = update.callback_query.message.chat_id
            username = update.callback_query.from_user.username
            message_id = update.callback_query.message.message_id
        else:
            logging.error("Received unknown update!")
            return constants.RESPONSE_OK

        # User must have username
        if not username:
            bot.sendMessage(chat_id, constants.ERROR_NO_USERNAME)
            return constants.RESPONSE_OK

        # Retrieve/Create user
        user = User.get_by_id(user_id)
        if not user:
            # New user
            logging.info("User %s not found! Creating new user...", user_id)
            user = User(id=user_id, chat_id=chat_id, username=username)
            user.put()
        else:
            # Existing user
            user.last_activity_date = datetime.now()
            if username != user.username:
                logging.debug("User %s has changed username from %s to %s",
                              user_id, user.username, username)
                user.username = username
            user.put()

        commands.handle_input(user, text, message_id)

        return constants.RESPONSE_OK
예제 #2
0
 def lineReceived(self, line):
     if line.startswith('/'):
         line = line[1:]
         result = commands.handle_input(self, line)
         if result is not None:
             print result
     else:
         self.protocol.send_chat(line)
예제 #3
0
파일: irc.py 프로젝트: marek-danel/pyspades
 def privmsg(self, user, channel, msg):
     if user in self.ops or user in self.voices:
         prefix = '@' if user in self.ops else '+'
         alias = self.factory.aliases.get(user, user)
         if msg.startswith(self.factory.commandprefix) and user in self.ops:
             self.unaliased_name = user
             self.name = prefix + alias
             input = msg[len(self.factory.commandprefix):]
             result = commands.handle_input(self, input)
             if result is not None:
                 self.send("%s: %s" % (user, result))
         elif msg.startswith(self.factory.chatprefix):
             max_len = MAX_IRC_CHAT_SIZE - len(self.protocol.server_prefix) - 1
             msg = msg[len(self.factory.chatprefix):].strip()
             message = ("<%s> %s" % (prefix + alias, msg))[:max_len]
             message = message.decode('cp1252')
             print message.encode('ascii', 'replace')
             self.factory.server.send_chat(encode(message))
예제 #4
0
파일: irc.py 프로젝트: mrpizza123/pysnip
 def privmsg(self, user, channel, msg):
     if (user in self.owners or user in self.admins or
         user in self.moderators or user in self.guards or user in self.voices):
         if user in self.owners:
             prefix = '~'
             user_type = 'admin'
         elif user in self.admins:
             prefix = '&'
             user_type = 'admin'
         elif user in self.moderators:
             prefix = '@'
             user_type = 'moderator'
         elif user in self.guards:
             prefix = '%'
             user_type = 'guard'
         else:
             prefix = '+'
             user_type = 'none'
         alias = self.factory.aliases.get(user, user)
         if msg.startswith(self.factory.commandprefix) and (
             user in self.owners or user in self.admins or
             user in self.moderators or user in self.guards):
             self.unaliased_name = user
             self.name = prefix + alias
             input = msg[len(self.factory.commandprefix):]
             rights = self.rights
             self.rights = AttributeSet()
             self.rights.update(commands.rights.get(user_type, ()))
             self.rights.update(commands.rights.get('irc', ()))
             result = commands.handle_input(self, input)
             self.rights = rights
             if result is not None:
                 self.send("%s: %s" % (user, result))
         elif msg.startswith(self.factory.chatprefix):
             max_len = MAX_IRC_CHAT_SIZE - len(self.protocol.server_prefix) - 1
             msg = msg[len(self.factory.chatprefix):].strip()
             message = ("<%s> %s" % (prefix + alias, msg))[:max_len]
             message = message.decode('cp1252')
             print message.encode('ascii', 'replace')
             self.factory.server.send_chat(encode(message))