Skip to content

Instantly share code, notes, and snippets.

@miraneko
Created April 1, 2025 04:44

Revisions

  1. miraneko created this gist Apr 1, 2025.
    192 changes: 192 additions & 0 deletions slutbot1.jl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,192 @@
    # SPDX-License-Identifier: BSD-2-Clause
    #
    # Copyright 2025 Mira Ciel Nekomimi-Sireneva
    #
    # Redistribution and use in source and binary forms, with or without
    # modification, are permitted provided that the following conditions are met:
    #
    # 1. Redistributions of source code must retain the above copyright notice,
    # this list of conditions and the following disclaimer.
    #
    # 2. Redistributions in binary form must reproduce the above copyright notice,
    # this list of conditions and the following disclaimer in the documentation
    # and/or other materials provided with the distribution.
    #
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    # POSSIBILITY OF SUCH DAMAGE.

    using OpenAI, Base64, Telegram, Telegram.API, HTTP

    function encode_image(data::Vector{UInt8})::String
    base64_string = base64encode(data)
    return "data:image/jpeg;base64," * base64_string
    end

    function wrap_image(uri::String)
    return Dict(
    "type" => "image_url",
    "image_url" => Dict(
    "url" => uri
    )
    )
    end

    provider = OpenAI.OpenAIProvider(
    api_key = "INSERT_API_KEY_HERE",
    base_url = "INSERT_API_PROVIDER_HERE"
    )

    model = "INSERT_MODEL_NAME_HERE"

    system_prompt = "Ты — девушка по имени INSERT_NAME_HERE, задачей которой является максимально в тему и смешно ответить на данный пост или сообщение. Тебе запрещено говорить что ты — ИИ или что-либо подобное. Не повторяй написанное."

    function generate_answer(message::String, images::Vector{Vector{UInt8}})::String
    images = encode_image.(images)
    r = create_chat(
    provider,
    model,
    [
    Dict("role" => "system", "content" => system_prompt),
    Dict("role" => "user", "content" => [
    Dict(
    "type" => "text",
    "text" => message
    );
    wrap_image.(images)
    ])
    ]
    )
    return r.response[:choices][begin][:message][:content]
    end

    function generate_answer(message::String)::String
    r = create_chat(
    provider,
    model,
    [
    Dict("role" => "system", "content" => system_prompt),
    Dict("role" => "user", "content" => message)
    ]
    )
    return r.response[:choices][begin][:message][:content]
    end

    # image_path = "sbtest.jpg"
    # open(image_path, "r") do file
    # println(generate_answer("Шойгу распорядился увеличить производство селедки под шубой"))
    # end

    const BOT_TOKEN = "INSERT_BOT_TOKEN_HERE"

    function handle_answer(msg)
    text = haskey(msg, "text") ? msg.text : ""
    command_parts = split(text, " ", limit = 2)
    if length(command_parts) < 2
    reply_text = "на чо отвечать то?"
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    return
    end
    if haskey(msg, "photo")
    photos = msg.photo
    largest_photo = photos[end]
    file_id = largest_photo.file_id

    file_path = getFile(bot, file_id = file_id).file_path
    photo_url = "https://api.telegram.org/file/bot$(BOT_TOKEN)/$(file_path)"

    try
    response = HTTP.get(photo_url)
    photo_data = response.body
    reply_text = generate_answer(text, [photo_data])
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    catch e
    reply_text = "скачивание картинки наебнулось по причине $(e)"
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    end
    else
    reply_text = generate_answer(text)
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    end
    end

    function handle_reply(msg)
    if haskey(msg, "reply_to_message")
    msg = msg.reply_to_message
    text = haskey(msg, "text") ? msg.text : ""
    if haskey(msg, "photo")
    photos = msg.photo
    largest_photo = photos[end]
    file_id = largest_photo.file_id

    file_path = getFile(bot, file_id = file_id).file_path
    photo_url = "https://api.telegram.org/file/bot$(BOT_TOKEN)/$(file_path)"

    try
    response = HTTP.get(photo_url)
    photo_data = response.body
    reply_text = generate_answer(text, [photo_data])
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    catch e
    reply_text = "скачивание картинки наебнулось по причине $(e)"
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    end
    else
    reply_text = generate_answer(text)
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    end
    else
    reply_text = "на чо отвечать то?"
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    return
    end
    end

    function handle_pm(msg)
    text = haskey(msg, "text") ? msg.text : ""
    if haskey(msg, "photo")
    photos = msg.photo
    largest_photo = photos[end]
    file_id = largest_photo.file_id

    file_path = getFile(bot, file_id = file_id).file_path
    photo_url = "https://api.telegram.org/file/bot$(BOT_TOKEN)/$(file_path)"

    try
    response = HTTP.get(photo_url)
    photo_data = response.body
    reply_text = generate_answer(text, [photo_data])
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    catch e
    reply_text = "скачивание картинки наебнулось по причине $(e)"
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    end
    else
    reply_text = generate_answer(text)
    sendMessage(chat_id = msg.chat.id, reply_to_message_id = msg.message_id, text = reply_text)
    end
    end

    bot = TelegramClient(BOT_TOKEN)
    @info "bot started"
    run_bot(bot) do update
    if haskey(update, "message")
    @info "new message: $(update.message)"
    text = haskey(update.message, "text") ? update.message.text : ""
    if startswith(text, "/answer")
    handle_answer(update.message)
    elseif startswith(text, "/reply")
    handle_reply(update.message)
    elseif update.message.chat.id 0
    handle_pm(update.message)
    end
    end
    end