So this is my current code, when I excute it with all my information filled it, it gives me different errors here are a few examples
Traceback (most recent call last):
File "ansible_telegram_bot.py", line 1, in <module>
import requests
ImportError: No module named requests
Traceback (most recent call last):
File "ansible_telegram_bot.py", line 2, in <module>
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
ImportError: cannot import name 'Filters' from 'telegram.ext' (/usr/local/lib/python3.8/dist-packages/telegram/ext/__init__.py)
root@ubunt-ansible-semaphore:~/server-ansible/bot# python ansible_telegram_bot.py
File "ansible_telegram_bot.py", line 51
response = requests.post(run_playbook_url, headers={"Authorization": f"Bearer {context.user_data['token']}"}, json={"playbook_command": playbook_command})
^
SyntaxError: invalid syntax
each time the errors change refering to attibutes that dont work or just somthing wierd, could somone please guide me, thank you in advance
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import telegram
# Telegram Bot Token
TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
# Ansible Semaphore API Base URL
API_BASE_URL = "https://ansible.url.com/api/v1"
# Function to handle start command
def start(update, context):
update.message.reply_text("Welcome to Ansible Bot! Send /login to authenticate.")
# Function to handle login command
def login(update, context):
update.message.reply_text("Please enter your username:")
context.user_data['login_state'] = 'username'
# Function to handle username input
def handle_username(update, context):
context.user_data['username'] = update.message.text
update.message.reply_text("Please enter your password:")
context.user_data['login_state'] = 'password'
# Function to handle password input
def handle_password(update, context):
username = context.user_data['username']
password = update.message.text
# Make a POST request to login endpoint
response = requests.post(API_BASE_URL + "/auth/login", json={"username": username, "password": password})
if response.status_code == 200:
# Assuming your API returns a token upon successful login
token = response.json()["token"]
context.user_data["token"] = token
update.message.reply_text("Login successful! You can now run playbooks.")
else:
update.message.reply_text("Login failed. Please try again.")
# Function to handle playbook execution
def run_playbook(update, context):
# Check if user is authenticated
if "token" not in context.user_data:
update.message.reply_text("Please login first using /login command.")
return
# Assuming you have an endpoint to trigger playbook execution
# Example: API_BASE_URL + "/run_playbook"
run_playbook_url = "/project/1/task/8"
# Extract playbook command from user message
playbook_command = update.message.text
# Make a POST request to run playbook endpoint
response = requests.post(run_playbook_url, headers={"Authorization": "Bearer {}".format(context.user_data['token'])}, json={"playbook_command": playbook_command})
if response.status_code == 200:
update.message.reply_text("Playbook execution initiated.")
else:
update.message.reply_text("Failed to execute playbook. Please try again.")
# Function to handle unknown commands
def unknown(update, context):
update.message.reply_text("Sorry, I don't understand that command.")
# Create the Updater and pass it your bot's token
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# Register command handlers
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("login", login))
dp.add_handler(MessageHandler(Filters.command, unknown))
# Register message handler for handling username and password inputs
dp.add_handler(MessageHandler(Filters.text & Filters.update, handle_username))
dp.add_handler(MessageHandler(Filters.text & Filters.update, handle_password))
# Register message handler for running playbooks
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, run_playbook))
# Start the Bot
updater.start_polling()
updater.idle()
I'm pretty new to python so Im busy learning and trying to understand what and where Im going wrong I used postman to excute the playbooks using postman and thought telegram would be the best platform as long as I build the script properly
- I've checked all my dependancies
- I've install python on both windows 11 and Ubuntu and I get the same errors
- I have looked at my indents which was a problem before but is now fixed
this is what I've done so far