installed telegram deps

This commit is contained in:
Dobromir Popov 2023-12-19 20:21:34 +02:00
parent b4d216ba70
commit 9435650d4f
3 changed files with 83 additions and 2 deletions

View File

@ -75,4 +75,5 @@ EXPOSE 8080 8081
# CMD ["npm", "start"]
CMD ["npm", "run", "start:demo"]
# CMD ["npm", "run", "start:demo"]
CMD ["npm", "run", "start:tele"]

View File

@ -0,0 +1,79 @@
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import requests
import json
import base64
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from io import BytesIO
from PIL import Image
# Set up logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
# Telegram Bot Token
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN_HERE'
# LLM API Endpoint
LLM_ENDPOINT = "http://localhost:11434/api/chat"
# Selenium WebDriver setup for screenshots
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi! Send me a message, and I will interact with LLM.')
def echo(update, context):
"""Echo the user message."""
user_message = update.message.text
response = query_llm(user_message)
update.message.reply_text(response)
def query_llm(user_message):
"""Query the LLM with the user's message."""
data = {
"model": "llama2",
"messages": [{"role": "user", "content": user_message}]
}
response = requests.post(LLM_ENDPOINT, json=data)
if response.status_code == 200:
response_data = response.json()
return response_data.get('message', {}).get('content', 'No response')
else:
return "Error: Unable to reach LLM"
def screenshot(update, context):
"""Take a screenshot of a webpage."""
url = ' '.join(context.args)
driver.get(url)
screenshot = driver.get_screenshot_as_png()
image_stream = BytesIO(screenshot)
image_stream.seek(0)
image = Image.open(image_stream)
image_stream.close()
image.save('screenshot.png')
update.message.reply_photo(photo=open('screenshot.png', 'rb'))
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning(f'Update "{update}" caused error "{context.error}"')
def main():
"""Start the bot."""
updater = Updater(TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_handler(CommandHandler("screenshot", screenshot, pass_args=True))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()

View File

@ -4,7 +4,8 @@
"main": "web/server.js",
"scripts": {
"start": "node web/server.js",
"start:demo": "NODE_ENV=demo node web/server.js"
"start:demo": "NODE_ENV=demo node web/server.js",
"start:tele": "python agent-py-bot/agent.py"
},
"dependencies": {
"body-parser": "^1.20.2",