如何将 message.text 转换为字符串并将其保存到数据库。如何通过电报机器人下载照片。 TelegramBotAPI
How to convert message.text to string and save it to database. How to download photo by telegram bot. TelegramBotAPI
我想保存照片并将文件名和消息文本添加到数据库中。(也在这个数据库中我有请求和用户的状态,如何提出请求,这2列工作正常)
数据库:
CREATE TABLE main (
id STRING,
owner STRING,
status STRING,
photo STRING
);
完整代码:
import telebot as tl
import sqlite3
conn = sqlite3.connect("D:/Promo_Bot/codes.db", check_same_thread=False)
cur = conn.cursor()
bot = tl.TeleBot("1875698946:AAGKlLTNEjaJQAz4VOj4Pc3Jej3A1tj-WjY")
@bot.message_handler(commands=['start'])
def start(message):
bot.reply_to(message, "Привет")
@bot.message_handler(content_types=['photo'])
def photo(message):
bot.reply_to(message, "Запрос отправлен на модерацию")
link = message.text
with open(link, "wb") as save_file:
save_file.write(bot.download_file(bot.get_file(message.photo[0].file_id).file_path))
data = [(message.text, str(message.from_user.username), 'wait', link)]
cur.executemany("""INSERT INTO main
VALUES (?, ?, ?,
?)""", data)
conn.commit()
@bot.message_handler()
def no(message):
bot.reply_to(message, "Отправте номер на чеке и фото одним сообщение")
bot.polling()
问题:
我的程序将 None 保存到第一列,将 None.jfif 保存到第四列。
SQLite Studio Screenshot
谢谢
您正在 photo
函数内将 message.text
写入数据库。但是,该功能仅针对包含 photo
的消息触发。当 message
包含照片时,message.text is None
。照片的任何标题都将在 message.caption
.
中
我想保存照片并将文件名和消息文本添加到数据库中。(也在这个数据库中我有请求和用户的状态,如何提出请求,这2列工作正常)
数据库:
CREATE TABLE main (
id STRING,
owner STRING,
status STRING,
photo STRING
);
完整代码:
import telebot as tl
import sqlite3
conn = sqlite3.connect("D:/Promo_Bot/codes.db", check_same_thread=False)
cur = conn.cursor()
bot = tl.TeleBot("1875698946:AAGKlLTNEjaJQAz4VOj4Pc3Jej3A1tj-WjY")
@bot.message_handler(commands=['start'])
def start(message):
bot.reply_to(message, "Привет")
@bot.message_handler(content_types=['photo'])
def photo(message):
bot.reply_to(message, "Запрос отправлен на модерацию")
link = message.text
with open(link, "wb") as save_file:
save_file.write(bot.download_file(bot.get_file(message.photo[0].file_id).file_path))
data = [(message.text, str(message.from_user.username), 'wait', link)]
cur.executemany("""INSERT INTO main
VALUES (?, ?, ?,
?)""", data)
conn.commit()
@bot.message_handler()
def no(message):
bot.reply_to(message, "Отправте номер на чеке и фото одним сообщение")
bot.polling()
问题: 我的程序将 None 保存到第一列,将 None.jfif 保存到第四列。 SQLite Studio Screenshot
谢谢
您正在 photo
函数内将 message.text
写入数据库。但是,该功能仅针对包含 photo
的消息触发。当 message
包含照片时,message.text is None
。照片的任何标题都将在 message.caption
.