프로그램 사용/ai 프로그램

python + telegram + ollama + selenium

구차니 2026. 4. 9. 22:04

openclaw와 telegram 조합으로 동료가 쓰고 있어서

비슷하게 해보려다가 claude 도 막혔지 이래저래 귀찮아서

local LLM + telegram 으로 하려다가

이래저래 귀찮아서(!)

local LLM은 ollama로 딸깍~ 하고

python + python-telegram-bot 조합으로 메신저 통신을 뚫고

수신한 메시지를 ollama의 가벼운 모델(일단 gemma3) 로 던지고

검색이 들어간 문장이 오면 검색을 제외한 단어를 인터넷 검색후(python + selenium)

결과를 다시 약간의 프롬프트와 합쳐 ollama로 던지고

응답을 받으면 메신저로 쏴주는 식으로 구현하니 나름 이렇게 에이전트를 만드는 거구나 싶어서

다른 ai 클라이언트나 에이전트들은 어떻게 만들어졌나 뜯어보고 싶어지네.

 

selenium은 wget 등으로 다음/네이버/구글 url + query 식으로 하면

바로 이동하지 않고 javascript를 통해 추가 링크로 이동하게 되어있어서 쓸 수 없다 보니

request 패키지로는 작동하지 않아 selenium을 쓸 수 밖에 없네..

 

[링크 : https://python-telegram-bot.org/]

 

telegram bot을 이용해서 메시지 보내는 방법

import asyncio
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters
from telegram.constants import ChatAction

async def handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
    chat_id = update.effective_chat.id

    # "입력 중..." 표시
    await context.bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)

    # 실제 작업 흉내 (지연)
    await asyncio.sleep(3)

    # 메시지 전송
    await update.message.reply_text("처리 완료!")

app = ApplicationBuilder().token("TOKEN").build()
app.add_handler(MessageHandler(filters.TEXT, handler))
app.run_polling()

[링크 : https://chatgpt.com/share/69d7a89e-0328-8322-aa93-5274cef6d386]

 

telegram bot 에 보낸 메시지 받는 방법(버전이 구버전 예제로 실행은 안됨)

from telegram.ext import Updater, MessageHandler, Filters

def handle_msg(update, context):
    print(update.message.text)

updater = Updater("BOT_TOKEN", use_context=True)
dp = updater.dispatcher

dp.add_handler(MessageHandler(Filters.text, handle_msg))

updater.start_polling()
updater.idle()

[링크 : https://chatgpt.com/share/69d7a934-835c-8320-b493-e07c126e9cd5]

 

python  에서 ollama 라이브러리를 이용하여 llama3 에게 물어보는 예제 코드

import ollama

response = ollama.chat(
    model='llama3',
    messages=[
        {
            'role': 'user',
            'content': '문제를 단계별로 생각 과정을 포함해서 풀어줘'
        }
    ]
)

print(response['message']['content'])

[링크 : https://chatgpt.com/share/69d7a89e-0328-8322-aa93-5274cef6d386]