Replies: 4 comments 1 reply
-
import os Load .env fileload_dotenv() Telegram Bot TokenTELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") Function to read allowed user IDs and budgetsdef read_allowed_users_and_budgets(): Command to display allowed users and budgetsdef show_allowed_users(update, context): Function to update .env filedef update_env_file(key, value): Command to add a new userdef add_user(update, context): Main function to run the botdef main():
if name == 'main': |
Beta Was this translation helpful? Give feedback.
-
`import os Load .env fileload_dotenv() Telegram Bot TokenTELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") Function to read allowed user IDs and budgetsdef read_allowed_users_and_budgets(): Command to display allowed users and budgetsdef show_allowed_users(update, context): Function to update .env filedef update_env_file(key, value): Command to add a new userdef add_user(update, context): Main function to run the botdef main():
if name == 'main': |
Beta Was this translation helpful? Give feedback.
-
pip install python-telegram-bot python-dotenv |
Beta Was this translation helpful? Give feedback.
-
i sucsess it here : https://github.com/khanfar/controlenvbot |
Beta Was this translation helpful? Give feedback.
-
i do this : import os
from dotenv import load_dotenv
from telegram.ext import Updater, CommandHandler
Load .env file
load_dotenv()
Telegram Bot Token
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
Function to read allowed user IDs and budgets
def read_allowed_users_and_budgets():
allowed_users = os.getenv("ALLOWED_TELEGRAM_USER_IDS").split(',')
budgets = os.getenv("USER_BUDGETS").split(',')
return allowed_users, budgets
Command to display allowed users and budgets
def show_allowed_users(update, context):
allowed_users, budgets = read_allowed_users_and_budgets()
message = "Allowed Users and Budgets:\n"
for user, budget in zip(allowed_users, budgets):
message += f"User: {user}, Budget: {budget}\n"
update.message.reply_text(message)
Function to update .env file
def update_env_file(key, value):
os.environ[key] = value
with open('.env', 'w') as f:
for env_key, env_value in os.environ.items():
f.write(f"{env_key}={env_value}\n")
Command to add a new user
def add_user(update, context):
new_user = context.args[0]
new_budget = context.args[1]
allowed_users, budgets = read_allowed_users_and_budgets()
allowed_users.append(new_user)
budgets.append(new_budget)
update_env_file("ALLOWED_TELEGRAM_USER_IDS", ','.join(allowed_users))
update_env_file("USER_BUDGETS", ','.join(budgets))
update.message.reply_text("User added successfully.")
Main function to run the bot
def main():
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dp = updater.dispatcher
if name == 'main':
main()
Beta Was this translation helpful? Give feedback.
All reactions