프로그램 사용/ai 프로그램
python 으로 telegram + fatherbot 연동하기
구차니
2026. 4. 8. 16:25
ollama 설치
| curl -fsSL https://ollama.com/install.sh | sh ollama run gemma3:1b |
python 라이브러리 설치
| pip3 install ollama pip3 install python-telegram-bot |
telegram bot 생성 및 token 얻기
[링크 : https://midoriiroplace.tistory.com/62] fatherbot 으로 텔레그램 봇 만들기(token 획득)
python 소스코드
| import ollama from telegram import Update from telegram.ext import filters, MessageHandler, ApplicationBuilder, ContextTypes, CommandHandler APIKEY='{{사용자 토큰}}' async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE): response = ollama.chat(model="gemma3:1b", messages=[{'role':'user','content':update.message.text}]) await context.bot.send_message(chat_id=update.effective_chat.id, text=response['message']['content'] ) if __name__ == '__main__': application = ApplicationBuilder().token(APIKEY).build() echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo) application.add_handler(echo_handler) application.run_polling() |
----
[링크 : https://dev-astra.tistory.com/490]
[링크 : https://midoriiroplace.tistory.com/62] fatherbot 으로 텔레그램 봇 만들기(token 획득)
[링크 : https://midoriiroplace.tistory.com/63] python으로 메시지 보내기 (chatid 획득)
[링크 : https://midoriiroplace.tistory.com/70]
[링크 : https://naradora78.tistory.com/39] 구버전 api 라서 현재 사용불가
[링크 : https://pypi.org/project/python-telegram-bot/]
gpt가 짜준게 이거 기반인 것 같은디..
가장 위에 소스로는 응답이 없고, 아무튼.. echo 얘제는 아래를 참고하면된다.
그러면.. 받고 이걸 ollama에 넘겨주고 받으면 되는건가..?
| import logging from telegram import Update from telegram.ext import filters, MessageHandler, ApplicationBuilder, ContextTypes, CommandHandler logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!") async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text) if __name__ == '__main__': application = ApplicationBuilder().token('USER_TOKEN').build() start_handler = CommandHandler('start', start) application.add_handler(start_handler) echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo) application.add_handler(echo_handler) application.run_polling() |
[링크 : https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions---Your-first-Bot]