Пример #1
0
def respondTo(message, sender):

	MAINTENANCE = False

	message_id = message.get('message_id')
	date = message.get('date')
	text = message.get('text')
	sender = message.get('from')
	senderID = str(sender['id'])
	senderFirstName = sender['first_name']
	chat = message['chat']
	chat_id = str(chat['id'])

	logging.info('Message ' + text + ' from ' + senderFirstName + '(' + senderID + ')')

	answer = u''

	if MAINTENANCE:
		if senderID != '13278104':
			return u'SCWKbot wird gerade gewartet!'
		else:
			answer = answer + u'Achtung, Wartung aktiv!\n'

	user = UserController.get(senderID)
	if not user:
		user = User.create(senderID, senderFirstName, chat_id)
	
	textSplit = text.partition(' ')
	command = textSplit[0].lower()
	additional = None
	if textSplit[2]:
		additional = textSplit[2]

	if command.startswith(u'an'):
		return  answer + UserController.registerForEvent(user, additional)

	if command.startswith(u'ab'):
		return  answer + UserController.cancelForEvent(user, additional)

	if command.startswith(u'info'):
		return  answer + UserController.infoForEvent(user, additional)

	if command.startswith(u'fahrtlöschen'):
		return  answer + DriverController.delete(user, additional)
	if command.startswith(u'fahrt'):
		return  answer + DriverController.create(user, additional)

	if user.admin:
		if command.startswith(u'erstell'):
			return  answer + EventController.create(user, additional)
		if command.startswith(u'lösch'):
			return  answer + EventController.delete(user, additional)
		if command.startswith(u'updaterepeating'):
			return  answer + EventController.updateRepeating(user, additional)
		if command.startswith(u'fahrerliste'):
			return  answer + DriverController.listByUser(additional)
	return answer + getHelpText(user)
Пример #2
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        message = body['message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        chat = message['chat']
        chat_id = chat['id']

        if not text:
            logging.info('no text')
            return

        def reply(msg=None):
            send(msg, chat_id)

        lastUpdate = getCurrentUpdate(chat_id)
        if lastUpdate >= update_id:
            logging.info('Already at update ' + str(lastUpdate))
            return
        else:
            setCurrentUpdate(chat_id, update_id)

        if getSetting('userRegistration') != 'True' and not Entity.User.get(str(fr['id'])):
            reply('Registrierung ist geschlossen.')
            return

        # COMMANDS
        if text.startswith('/'):
            if UserController.isAdmin(str(fr['id'])):
                if text == '/start':
                    reply(u'Bot enabled')
                    setSetting('enabled', 'True')
                    return
                if text == '/stop':
                    reply(u'Bot disabled')
                    setSetting('enabled','False')
                    return
                if text == '/enableUserRegistration':
                    reply(u'User registration enabled')
                    setSetting('userRegistration', 'True')
                    return
                if text == '/disableUserRegistration':
                    reply(u'User registration disabled')
                    setSetting('userRegistration', 'False')
                    return
                if text.lower() == '/listusers':
                    allUsers = Entity.User.getAll()
                    answer = u''
                    for user in allUsers:
                        answer = answer + user.firstName + u'\t' + user.chatID + u'\n'
                    reply(answer)
                    return
                if text.startswith('/deleteUser '):
                    if UserController.deleteUser(str(fr['id']) , text.split()[1]):
                        reply(u'User ' + text.split()[1] + u' deleted.')
                        return
                if text.startswith('/setAdmin'):
                    split = text.split()
                    if len(split) == 3:
                        if UserController.setAdmin(str(fr['id']), split[1], (split[2] == 'True')):
                            reply(u'User ' + split[1] + u' admin set to ' + split[2])
                            return
                        else:
                            logging.warn("Set Admin request by " + str(fr['id']) + " not successful.")




            # END COMMANDS
            if getSetting('enabled') != 'True':
                logging.info('Bot is disabled')
                return

            # For dev
            if '/setName' in text:
                split = text.split()
                senderFirstName = split[split.index('/setName')+1]
                del split[split.index('/setName')+1]
                split.remove('/setName')
                text = ' '.join(split)
                message['text']=text
                message['from']['first_name']=senderFirstName


            if '/setUser' in text:
                split = text.split()
                senderID = split[split.index('/setUser')+1]
                del split[split.index('/setUser')+1]
                split.remove('/setUser')
                text = ' '.join(split)
                message['text']=text
                message['from']['id']=senderID

        Responder.respondTo(message, chat_id)
Пример #3
0
def respond_to_text(message, text, sender):
    MAINTENANCE = False

    message_id = message.get('message_id')
    date = message.get('date')
    sender = message.get('from')
    senderID = str(sender['id'])
    senderFirstName = sender['first_name']
    chat = message['chat']
    chat_id = str(chat['id'])

    def send(msg):
        main.send(msg, chat_id)

    logging.info('Message ' + text + ' from ' + senderFirstName + '(' +
                 senderID + ')')

    answer = u''

    if MAINTENANCE:
        if senderID != '13278104':
            send(u'SCWKbot wird gerade gewartet!')
        else:
            answer = answer + u'Achtung, Wartung aktiv!\n'

    user = UserController.get(senderID)
    if not user:
        user = User.create(senderID, senderFirstName, chat_id)

    textSplit = text.partition(' ')
    command = textSplit[0].lower()
    additional = None
    if textSplit[2]:
        additional = textSplit[2].strip()

    if command.startswith(u'an'):
        send(answer + UserController.registerForEvent(user, additional))
        return

    if command.startswith(u'ab'):
        send(answer + UserController.cancelForEvent(user, additional))
        return

    if command.startswith(u'info'):
        send(answer + UserController.infoForEvent(user, additional))
        return

    if command.startswith(u'fahrtlöschen'):
        send(answer + DriverController.delete(user, additional))
        return
    if command.startswith(u'fahrt'):
        send(answer + DriverController.create(user, additional))
        return

    if user.admin:
        if command.startswith(u'erstell'):
            send(answer + EventController.create(user, additional))
            return
        if command.startswith(u'lösch'):
            send(answer + EventController.delete(user, additional))
            return
        if command.startswith(u'updaterepeating'):
            send(answer + EventController.updateRepeating(user, additional))
            return
        if command.startswith(u'fahrerliste'):
            send(answer + DriverController.listByUser(additional))
            return
    send(answer + getHelpText(user))
Пример #4
0
	def post(self):
		urlfetch.set_default_fetch_deadline(60)
		body = json.loads(self.request.body)
		logging.info('request body:')
		logging.info(body)
		self.response.write(json.dumps(body))

		update_id = body['update_id']
		message = body['message']
		message_id = message.get('message_id')
		date = message.get('date')
		text = message.get('text')
		fr = message.get('from')
		chat = message['chat']
		chat_id = chat['id']

		if not text:
			logging.info('no text')
			return

		def reply(msg=None):
			if msg:
				resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
					'chat_id': str(chat_id),
					'text': msg.encode("utf-8"),
					'disable_web_page_preview': 'true',
				})).read()
			else:
				logging.error('no msg specified')
				resp = None

			logging.info('send response:')
			logging.info(resp)

		lastUpdate = getCurrentUpdate(chat_id)
		if lastUpdate >= update_id:
			logging.info('Already at update ' + str(lastUpdate))
			return
		else:
			setCurrentUpdate(chat_id, update_id)

		if getSetting('userRegistration') != 'True' and not Entity.User.get(str(fr['id'])):
			reply('Registrierung ist geschlossen.')
			return

		# COMMANDS
		if text.startswith('/'):
			if UserController.isAdmin(str(fr['id'])):
				if text == '/start':
					reply(u'Bot enabled')
					setSetting('enabled', 'True')
					return
				if text == '/stop':
					reply(u'Bot disabled')
					setEnabled('enabled','False')
					return
				if text == '/enableUserRegistration':
					reply(u'User registration enabled')
					setSetting('userRegistration', 'True')
					return
				if text == '/disableUserRegistration':
					reply(u'User registration disabled')
					setSetting('userRegistration', 'False')
					return
				if text.lower() == '/listusers':
					allUsers = Entity.User.getAll()
					answer = u''
					for user in allUsers:
						answer = answer + user.firstName + u'\t' + user.chatID + u'\n'
					reply(answer)
					return
				if text.startswith('/deleteUser '):
					if UserController.deleteUser(str(fr['id']) , text.split()[1]):
						reply(u'User ' + text.split()[1] + u' deleted.')
						return
				if text.startswith('/setAdmin'):
					split = text.split()
					if len(split) == 3:
						if UserController.setAdmin(str(fr['id']), split[1], (split[2] == 'True')):
							reply(u'User ' + split[1] + u' admin set to ' + split[2])
							return
						else:
							logging.warn("Set Admin request by " + str(fr['id']) + " not successful.")




		# END COMMANDS
		if getSetting('enabled') != 'True':
			logging.info('Bot is disabled')
			return

		# For dev
		if '/setName' in text:
			split = text.split()
			senderFirstName = split[split.index('/setName')+1]
			del split[split.index('/setName')+1]
			split.remove('/setName')
			text = ' '.join(split)
			message['text']=text
			message['from']['first_name']=senderFirstName


		if '/setUser' in text:
			split = text.split()
			senderID = split[split.index('/setUser')+1]
			del split[split.index('/setUser')+1]
			split.remove('/setUser')
			text = ' '.join(split)
			message['text']=text
			message['from']['id']=senderID

		# CUSTOMIZE FROM HERE
		rep = Responder.respondTo(message, chat_id)
		if rep:
			reply(rep)