Created
January 28, 2025 06:46
-
-
Save vaavaa/ba990ccc2fdcac4538864f54af585699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from telegram import Update, constants | |
from telegram.ext import Updater, CommandHandler, MessageHandler, filters, CallbackContext, Application | |
from ollama import ChatResponse | |
from ollama import Client | |
from dotenv import load_dotenv | |
# Токен вашего бота | |
TOKEN = os.getenv("BOT_TOKEN", "TOKEN") | |
if TOKEN=="TOKEN": | |
load_dotenv() | |
TOKEN = os.getenv("BOT_TOKEN", "TOKEN") | |
async def start(update: Update, context: CallbackContext): | |
print(f"Бот запущен и ставит лайки за сообщения! 👍") | |
async def react_with_like(update: Update, context: CallbackContext): | |
message = update.message | |
chat_id = message.chat_id | |
message_id = message.message_id | |
try: | |
# Ставим реакцию "лайк" (👍) | |
await context.bot.set_message_reaction( | |
chat_id=chat_id, | |
message_id=message_id, | |
reaction=[constants.ReactionEmoji.EYES], | |
is_big=False # Можно изменить на True для увеличенной анимации | |
) | |
except Exception as e: | |
print(f"Ошибка при установке реакции: {e}") | |
async def analyze_message(update: Update, context: CallbackContext): | |
message_text = update.message.text or "Медиа-контент" | |
if not message_text: | |
return | |
client = Client( | |
host='http://192.168.68.120:11434', | |
) | |
# Инициализация клиента Ollama | |
try: | |
response: ChatResponse = client.chat(model='llama3.1:latest', messages=[ | |
{ | |
'role': 'user', | |
'content': f"Содержит ли сообщение информацию о технической ошибке, ответь да или нет: {message_text}", | |
}, | |
]) | |
print(f"Получили ответ: {response['message']['content']}") | |
if 'ДА' in str(response['message']['content']).upper(): | |
# Вызываем функцию с реакцией | |
await react_with_like(update, context) | |
except Exception as e: | |
print(f"Возникла ошибка с сообщением: {message_text} -- ошибка: {str(e)}") | |
print(f"Проанализировано сообщение: {message_text}") | |
def main(): | |
"""Start the bot.""" | |
# Create the Application and pass it your bot's token. | |
application = Application.builder().token(TOKEN).build() | |
# Регистрируем обработчики | |
application.add_handler(CommandHandler("start", start)) | |
application.add_handler(MessageHandler(filters.ALL, analyze_message)) | |
application.run_polling() | |
if __name__ == "__main__": | |
print("Bot alive") | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
httpcore==1.0.7 | |
httpx==0.28.1 | |
idna==3.10 | |
ollama==0.4.7 | |
pydantic==2.10.6 | |
pydantic_core==2.27.2 | |
python-telegram-bot==21.10 | |
requests==2.32.3 | |
sniffio==1.3.1 | |
typing_extensions==4.12.2 | |
urllib3==2.3.0 | |
python-dotenv==1.0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment