i write this code:
import sqlite3
from typing import Final
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, filters, ContextTypes, CallbackContext, ChatMemberHandler
TOKEN: Final = ''
BOT_USERNAME: Final = '@NeoTecnoAlCentroBot'
#Id dell'user principale (IO - SmokeDeath10)
ID_USER_PRINCIPALE = '5836602747'
#Crea connessione a database
conn = sqlite3.connect('comandi.db')
c = conn.cursor()
#Crea le tabelle se non esistono già
c.execute('''CREATE TABLE IF NOT EXISTS comandi (nome text, messaggio text)''')
c.execute('''CREATE TABLE IF NOT EXISTS comandi (user_id integer, username text)''')
c.execute('''CREATE TABLE IF NOT EXISTS utenti (user_id integer, username text)''')
#Aggiungi l'utente principale agli utenti
c.execute("INSERT INTO utenti VALUES (?,?)", (ID_USER_PRINCIPALE, None))
conn.commit()
def crea_comando(update: Update, context: CallbackContext) -> None:
"""Permette agli utenti autorizzati di creare un comando personalizzato."""
user_id = update.message.from_user.id
c.execute("SELECT * FROM utenti WHERE user_id=?", (user_id,))
if c.fetchone() is not None:
args = context.args
if len(args) >= 2:
nome_comando = args[0]
messaggio = ' '.join(args[1:])
c.execute("INSERT INTO comandi VALUES (?,?)", (nome_comando, messaggio))
conn.commit()
update.message.reply_text(f'Comando {nome_comando} creato con successo.')
else:
update.message.reply_text('Uso: /crea_comando <nome_comando> <messaggio>')
else:
update.message.reply_text('Non hai i permessi per creare comandi.')
def autorizza_utente(update: Update, context: CallbackContext) -> None:
"""Permette all'utente principale di autorizzare un altro utente."""
username = update.message.from_user.username
c.execute("SELECT * FROM utenti WHERE username=?", (username,))
if c.fetchone() is not None:
args = context.args
if len(args) == 1:
username = args[0][1:] # Rimuove il carattere '@'
c.execute("SELECT user_id FROM utenti WHERE username=?", (username,))
user_id = c.fetchone()
if user_id is not None:
c.execute("INSERT INTO utenti VALUES (?,?)", (user_id[0], username))
conn.commit()
update.message.reply_text(f'Utente @{username} autorizzato con successo.')
else:
update.message.reply_text('Username non trovato.')
else:
update.message.reply_text('Uso: /autorizza_utente @username')
else:
update.message.reply_text('Non hai i permessi per autorizzare utenti.')
def aggiorna_utente(update: Update, context: CallbackContext) -> None:
"""Aggiorna l'username e l'ID dell'utente quando entra nel gruppo o cambia il suo username."""
user_id = update.chat_member.new_chat_member.user.id
username = update.chat_member.new_chat_member.user.username
c.execute("INSERT OR REPLACE INTO utenti VALUES (?,?)", (user_id, username))
conn.commit()
def rimuovi_utente(update: Update, context: CallbackContext) -> None:
"""Rimuove l'username e l'ID dell'utente quando viene rimosso dal gruppo."""
user_id = update.chat_member.old_chat_member.user.id
c.execute("DELETE FROM utenti WHERE user_id=?", (user_id,))
conn.commit()
def tagga_utenti(update: Update, context: CallbackContext) -> None:
"""Tagga gli utenti in un singolo messaggio o in più messaggi."""
c.execute("SELECT username FROM utenti")
utenti = [row[0] for row in c.fetchall()]
for i in range(0, len(utenti), 10):
tags = ' '.join(f'@{utente}' for utente in utenti[i:i+10])
update.message.reply_text(tags)
def invia_privato(update: Update, context: CallbackContext) -> None:
"""Invia un messaggio privato a tutti i membri del gruppo."""
if update.message.from_user.username == ID_USER_PRINCIPALE:
args = context.args
if args:
messaggio = ' '.join(args)
c.execute("SELECT user_id FROM utenti")
utenti = [row[0] for row in c.fetchall()]
for utente in utenti:
context.bot.send_message(chat_id=utente, text=messaggio)
else:
update.message.reply_text('Uso: /invia_privato <messaggio>')
else:
update.message.reply_text('Non hai i permessi per inviare messaggi privati.')
def echo(update: Update, context: CallbackContext) -> None:
"""Risponde con messaggi preimpostati o comandi personalizzati."""
comando = update.message.text[1:]
c.execute("SELECT messaggio FROM comandi WHERE nome=?", (comando,))
messaggio = c.fetchone()
if messaggio is not None:
update.message.reply_text(messaggio[0])
def main() -> None:
"""Avvia il bot."""
updater = Updater('7498845724:AAEI2GTjXMZgGo3S99iBw_OgoKh7Zshf-Oc', use_context=True)
# Ottieni il dispatcher per registrare i gestori
dp = updater.dispatcher
# su differenti comandi - rispondi in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("crea_comando", crea_comando, pass_args=True))
dp.add_handler(CommandHandler("autorizza_utente", autorizza_utente, pass_args=True))
dp.add_handler(CommandHandler("tagga_utenti", tagga_utenti))
dp.add_handler(CommandHandler("invia_privato", invia_privato, pass_args=True))
dp.add_handler(ChatMemberHandler(aggiorna_utente, ChatMemberHandler.CHAT_MEMBER_UPDATED))
dp.add_handler(ChatMemberHandler(rimuovi_utente, ChatMemberHandler.CHAT_MEMBER_UPDATED))
# su messaggi non comando - rispondi con lo stesso messaggio in Telegram
dp.add_handler(MessageHandler(Filters.command, echo))
# Avvia il Bot
updater.start_polling()
# Esegui il bot fino a quando non si preme Ctrl-C o il processo non riceve SIGINT,
# SIGTERM o SIGABRT. Questo dovrebbe essere usato maggiormente per l'esecuzione del bot
# in background.
updater.idle()
if __name__ == '__main__':
main()
But when I start main the terminal print this issues:
Traceback (most recent call last):
File “C:UsersadminPycharmProjectsNeoTecnoAlCentroBotPrincipale.venvScriptsBot.py”, line 135, in
main()
File “C:UsersadminPycharmProjectsNeoTecnoAlCentroBotPrincipale.venvScriptsBot.py”, line 109, in main
updater = Updater(‘7498845724:AAEI2GTjXMZgGo3S99iBw_OgoKh7Zshf-Oc’, use_context=True)
TypeError: Updater.init() got an unexpected keyword argument ‘use_context’
I looked at the version of python-telegram-bot to see if it was compatible if it is above 12.0
Domenico Aiello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.