示例#1
0
class TelegramBot:
    gimage_api = GoogleImagesAPI()

    def __init__(self, token):
        boturl = 'https://api.telegram.org/bot'
        logger.info('Secret bot URL: {0}{1}/'.format(boturl, token))
        self.api = TelegramAPI(url='{0}{1}/'.format(boturl, token))

        # Make this bot self-aware
        myself = self.api.get_me()
        self.id = myself['id']
        self.first_name = myself['first_name']
        self.username = myself['username']

        # Define valid commands
        self.commands = {
            'image': self.image
        }

    def image(self, text):
        if text is None:
            return 'Need an argument for image search. Try "/image cute cats"'
        image = self.gimage_api.random_image(query=text)
        if image is None:
            found = "nothing"
        else:
            found = image['url']
        returntext = ('Searched for {}, found {}'.format(text, found))
        return returntext

    def command(self, command, text):
        logger.info('Parsing command {} with data: {}'.format(command, text))

        # Check if command has been adressed to a specific bot
        split_command = command.split('@')
        if len(split_command) > 1 and split_command[1] != self.username:
            # Not for us
            return None

        clean_command = split_command[0].lstrip('/')
        if clean_command in self.commands:
            return self.commands[clean_command](text)
        return None

    def handle_message(self, message):
        if 'text' in message:
            text = message['text'].split(maxsplit=1)
            command_args = text[1] if len(text) == 2 else None
            return self.command(text[0], command_args)
        return None

    def respond(self, message):
        chat_id = message['chat']['id']
        returntext = self.handle_message(message)
        if returntext:
            try:
                self.api.send_message(chat_id, text=returntext)
            except:
                logger.exception("Failed to send message.")
        return returntext
示例#2
0
class TelegramBot:
    gimage_api = GoogleImagesAPI()

    def __init__(self, token):
        boturl = 'https://api.telegram.org/bot'
        logger.info('Secret bot URL: {0}{1}/'.format(boturl, token))
        self.api = TelegramAPI(url='{0}{1}/'.format(boturl, token))

        # Make this bot self-aware
        myself = self.api.get_me()
        self.id = myself['id']
        self.first_name = myself['first_name']
        self.username = myself['username']

        # Define valid commands
        self.commands = {'img': self.image}

    def image(self, text):
        if text is None:
            return 'Need an argument for image search. Try "/image cute cats"'
        image = self.gimage_api.random_image(query=text)
        if image is None:
            found = "nothing"
        else:
            found = image['link']
        returntext = ('Searched for {}, found {}'.format(text, found))
        return returntext

    def command(self, command, text):
        logger.info('Parsing command {} with data: {}'.format(command, text))

        # Check if command has been adressed to a specific bot
        split_command = command.split('@')
        if len(split_command) > 1 and split_command[1] != self.username:
            # Not for us
            return None

        clean_command = split_command[0].lstrip('/')
        if clean_command in self.commands:
            return self.commands[clean_command](text)
        return None

    def handle_message(self, message):
        if 'text' in message:
            text = message['text'].split(maxsplit=1)
            command_args = text[1] if len(text) == 2 else None
            return self.command(text[0], command_args)
        return None

    def respond(self, message):
        chat_id = message['chat']['id']
        returntext = self.handle_message(message)
        if returntext:
            try:
                self.api.send_message(chat_id, text=returntext)
            except:
                logger.exception("Failed to send message.")
        return returntext
示例#3
0
    def __init__(self, token):
        boturl = 'https://api.telegram.org/bot'
        logger.info('Secret bot URL: {0}{1}/'.format(boturl, token))
        self.api = TelegramAPI(url='{0}{1}/'.format(boturl, token))

        # Make this bot self-aware
        myself = self.api.get_me()
        self.id = myself['id']
        self.first_name = myself['first_name']
        self.username = myself['username']

        # Define valid commands
        self.commands = {'img': self.image}
示例#4
0
    def __init__(self, token):
        boturl = 'https://api.telegram.org/bot'
        logger.info('Secret bot URL: {0}{1}/'.format(boturl, token))
        self.api = TelegramAPI(url='{0}{1}/'.format(boturl, token))

        # Make this bot self-aware
        myself = self.api.get_me()
        self.id = myself['id']
        self.first_name = myself['first_name']
        self.username = myself['username']

        # Define valid commands
        self.commands = {
            'image': self.image
        }
class TelegramBot:
    num_api = NumbersAPI()
    cat_api = CatFactAPI()
    urban_api = UrbanDictionaryAPI()

    def __init__(self, token):
        boturl = 'https://api.telegram.org/bot'
        logger.info('Secret bot URL: {0}{1}/'.format(boturl, token))
        self.api = TelegramAPI(url='{0}{1}/'.format(boturl, token))

        # Make this bot self-aware
        myself = self.api.get_me()
        self.id = myself['id']
        self.first_name = myself['first_name']
        self.username = myself['username']

        # Define valid commands
        self.commands = {
            'number': self.number,
            'cat': self.cat,
            'define': self.define
        }

    def number(self, text):
        try:
            number = int(text.split()[0])
        except ValueError:
            return "I do not recognize that as a number"
        except AttributeError:
            return "Please provide a number"
        result = self.num_api.number(number)
        return result

    def cat(self, text):
        return self.cat_api.facts(number=1)[0]

    def define(self, text):
        definition = self.urban_api.top_definition(term=text)
        if definition is None:
            return 'No definition of "{}"'.format(text)
        returntext = "Definition of {}:\n\n{}".format(definition['word'], definition['definition'])
        if definition['example']:
            returntext += "\n\nExample:\n{}".format(definition['example'])
        return returntext

    def command(self, command, text):
        logger.info('Parsing command {} with data: {}'.format(command, text))

        # Check if command has been adressed to a specific bot
        split_command = command.split('@')
        if len(split_command) > 1 and split_command[1] != self.username:
            # Not for us
            return None

        clean_command = split_command[0].lstrip('/')
        if clean_command in self.commands:
            return self.commands[clean_command](text)
        return None

    def handle_message(self, message):
        if 'text' in message:
            text = message['text'].split(maxsplit=1)
            command_args = text[1] if len(text) == 2 else None
            return self.command(text[0], command_args)
        return None

    def respond(self, message):
        chat_id = message['chat']['id']
        returntext = self.handle_message(message)
        if returntext:
            try:
                self.api.send_message(chat_id, text=returntext)
            except:
                logger.exception("Failed to send message.")
        return returntext