sqlite - 向电报机器人的所有用户发送消息
问题描述
我正在使用 pyTelegramBotAPI (telebot)。我需要向我的所有机器人用户发送消息,但经过几次尝试,我的代码根本不起作用(没有错误,但它不发送消息)。我已将 user_id 存储在 SQLLite 数据库中,名为“test”的表中。将不胜感激一些帮助。
我的代码:
import sqlite3
import telebot
TOKEN = ""
bot = telebot.TeleBot(TOKEN)
conn = sqlite3.connect('db/database.db', check_same_thread=False)
cursor = conn.cursor()
def db_table_val(user_id: int, user_name: str, user_surname: str, username: str):
cursor.execute('INSERT INTO test (user_id, user_name, user_surname, username) VALUES (?, ?, ?, ?)', (user_id, user_name, user_surname, username))
conn.commit()
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Welcome!')
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text.lower() == 'Hi!':
bot.send_message(message.chat.id, 'Hi! You are cool!')
us_id = message.from_user.id
us_name = message.from_user.first_name
us_sname = message.from_user.last_name
username = message.from_user.username
db_table_val(user_id=us_id, user_name=us_name, user_surname=us_sname, username=username)
@bot.message_handler(commands=['newrm'])
def step_Set_Rm(message):
cid = message.chat.id
userRm = message.text
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
cursor.execute("SELECT user_id FROM test")
users_list = cursor.fetchall()
bot.send_message(users_list, userRm)
conn.commit()
conn.close()
bot.send_message(message.chat.id, 'Отправлено!')
bot.polling(none_stop=True)
解决方案
您可以在列表中列出用户 ID,而无需使用任何第三方模块:
import telebot
TOKEN = ""
bot = telebot.TeleBot(TOKEN)
admin = # your chat id
users = []
@bot.message_handler(commands=['start'])
def start_message(msg):
bot.send_message(msg.chat.id, 'Welcome!')
if msg.chat.id not in users: # if the id isn't already in the users list
users.append(msg.chat.id)
@bot.message_handler(commands=['send_at_all'])
def sendAtAll(msg):
if msg.chat.id == admin: # only if YOU start the command the message will be sent
for id in users: # for every user that has start the bot
bot.send_message(id, "I'm sending this message at all the users")
# If an user wants to stop the notifications...
@bot.message_handler(commands=['unsubscribe'])
def sendAtAll(msg):
del users[msg.chat.id]
bot.send_message(msg.chat.id, "If you want to receive the notification click /start")
# Every time you are going to restart the bot polling the content of the users list will be deleated so...
@bot.message_handler(commands=['save_user_list'])
def sendAtAll(msg):
if msg.chat.id == admin:
bot.send_message(admin, users)
# the list will be sent to your telegram chat, when you activate the bot you can add the list manually
bot.polling()
为我糟糕的英语道歉
推荐阅读
- laravel - 哎呀,看起来 laravel 5.8 出了点问题
- sql - 如何将行中的一些值转置为列?
- r - 编写一个 R 函数来查找二进制子集
- docker - 使用 docker-compose 创建一个仅限内部的 Docker 覆盖网络
- asp.net - ASP.NET Form 会从 Barcode 中获取价值吗
- aspose.words - Aspose 生成的 doc 文件为某些用户将下划线变为空白
- linux - 如何从 Kubernetes 服务 laravel 后面的 HTTP 请求中读取客户端 IP 地址
- apache-spark - 捕获从 Spark 写入操作输出的文件名
- python - 通过套接字向客户端发送图像将不起作用 python
- mysql - SQL 表主键的索引从 3 开始