加入组ID

This commit is contained in:
liuwei
2025-02-06 12:19:48 +08:00
parent 25e5e26a7b
commit 19c94364a4
3 changed files with 8 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ USE message_archive;
CREATE TABLE messages ( CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
group_id varchar(20) not null ,
timestamp VARCHAR(20) NOT NULL, timestamp VARCHAR(20) NOT NULL,
sender VARCHAR(255) NOT NULL, sender VARCHAR(255) NOT NULL,
content TEXT NULL, content TEXT NULL,

View File

@@ -9,7 +9,7 @@ db_config = {
} }
def archive_message(timestamp_str, sender, content, message_type, attachment_url=None): def archive_message(group_id, timestamp_str, sender, content, message_type, attachment_url=None):
# 连接到数据库 # 连接到数据库
connection = pymysql.connect(**db_config) connection = pymysql.connect(**db_config)
@@ -17,10 +17,10 @@ def archive_message(timestamp_str, sender, content, message_type, attachment_url
with connection.cursor() as cursor: with connection.cursor() as cursor:
# 插入消息信息 # 插入消息信息
sql = """ sql = """
INSERT INTO messages (timestamp, sender, content, message_type, attachment_url) INSERT INTO messages (group_id,timestamp, sender, content, message_type, attachment_url)
VALUES (%s, %s, %s, %s, %s) VALUES (%s, %s, %s, %s, %s, %s)
""" """
cursor.execute(sql, (timestamp_str, sender, content, message_type, attachment_url)) cursor.execute(sql, (group_id, timestamp_str, sender, content, message_type, attachment_url))
# 提交事务 # 提交事务
connection.commit() connection.commit()
@@ -37,10 +37,11 @@ def archive_message(timestamp_str, sender, content, message_type, attachment_url
# 示例用法 # 示例用法
if __name__ == "__main__": if __name__ == "__main__":
group_id ='XXX@123123'
timestamp_str = "2025-02-06 11:15:28" timestamp_str = "2025-02-06 11:15:28"
sender = "XXX" sender = "XXX"
content = "This is a test message with a string timestamp." content = "This is a test message with a string timestamp."
message_type = "text" message_type = "text"
attachment_url = "http://example.com/attachment.pdf" # 可以为None如果没有附件 attachment_url = "http://example.com/attachment.pdf" # 可以为None如果没有附件
archive_message(timestamp_str, sender, content, message_type, attachment_url) archive_message(group_id,timestamp_str, sender, content, message_type, attachment_url)

View File

@@ -163,7 +163,7 @@ class Robot(Job):
# 聊天记录入库动作: # 聊天记录入库动作:
try: try:
now_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) now_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
archive_message(now_time,msg.sender,msg.content,msg.type,msg.extra) archive_message(msg.roomid,now_time,msg.sender,msg.content,msg.type,msg.extra)
except Exception as e: except Exception as e:
self.LOG.error(f"archive_message error: {e}") self.LOG.error(f"archive_message error: {e}")