Ejemplo n.º 1
0
def respond():
    # retrieve the message in JSON and then transform it to Telegram object
    update = telegram.Update.de_json(request.get_json(force=True), bot)

    chat_id = update.message.chat.id
    msg_id = update.message.message_id

    text = update.message.text.encode('utf-8').decode()
    print("got text message :", text)
    resp = get_response(text)
    try:
        if len(resp) > MAX_MSG_LEN:
            msglist = [
                resp[start:start + MAX_MSG_LEN]
                for start in range(0, len(resp), MAX_MSG_LEN)
            ]
            for msg in msglist:
                bot.sendMessage(chat_id=chat_id,
                                text=msg,
                                reply_to_message_id=msg_id)
        else:
            bot.sendMessage(chat_id=chat_id,
                            text=resp,
                            reply_to_message_id=msg_id)
    except Exception as e:
        # if things went wrong
        print(e)
        print(text)
        bot.sendMessage(chat_id=chat_id,
                        text="Произошла ошибка выполнения",
                        reply_to_message_id=msg_id)
    return 'ok'
Ejemplo n.º 2
0
Archivo: main.py Proyecto: apotl/golem
async def on_message(message):
    if message.author != client.user:
        tmp = await check_triggers(message)
        if message.channel.is_private == False or message.content.startswith("!help") or message.content.startswith("!list") :
            sender ="<@"+ message.author.id+">"
            tells = commands.get_tells(sender)
            for tell in tells:
                tmp = await client.send_message(message.channel,tell)
            if message.content.startswith(config.get_prefix()):
                command = get_command(message.content)
                if command == "grab":
                    async for log in client.logs_from(message.channel,limit=2):
                        if not log.content.startswith("!grab"):
                            commands.add_grab(log)
                            tmp = await client.send_message(message.channel,"Grab Successful")
                elif command == "list" and config.check_command(command):
                    tmp = await client.send_message(message.author,commands.list_places())
                    tmp2 = await client.send_message(message.channel,"I have PM'd you the list of places")
                else:
                    try:
                        args = message.content.split(' ',1)[1]
                    except:
                        args = ""
                    if config.check_command(command):
                        tmp = await client.send_message(message.channel,commands.get_response(message,command,args))
                    else:
                        tmp = await client.send_message(message.channel,commands.get_unsupported_msg(command))
Ejemplo n.º 3
0
def handle_command(command, channel):
    default_response = "No command was found. Type !help for help"

    print('Command is')
    print(command)
    response = None

    try:
        response = commands.get_response(command)
        print(response)

        if command.startswith('def_func'):
            lines = command.split("\n")
            first_line = lines[0]
            content = lines[1:]
            parameters = first_line.split(" ")
            def_command(parameters[1], parameters[2], content)

        # Add a method to handle !help

        if command.startswith('add_api'):
            command_parts = command.split(' ')
            key_word = command_parts[1]
            api = command_parts[2][1:-1]
            api = api.replace("&amp;", "&")
            command_dict[key_word] = api

        if command.startswith('call_api'):
            command_parts = command.split(' ')
            key_word = command_parts[1]
            query = command_parts[2]
            api = command_dict[key_word]
            url = api + query
            print(url)
            response = requests.get(url)

    except Exception as error:
        response = "Error: " + str(error)

    # Sends the response back to the channel
    slack_client.api_call("chat.postMessage",
                          channel=channel,
                          text=response or default_response)
Ejemplo n.º 4
0
def latest():
    return get_response("/latest")