拆分消息电报机器人热图
Split message telegram bot pyrogram
我编写了一个代码,使用 pyrogram python 库将消息从源聊天复制到目标聊天。代码工作正常,但它从源聊天中制作消息的绝对副本。
我的代码示例:
#!/usr/bin/env python3
from pyrogram import Client
from pyrogram import filters
# ~~~~~~ CONFIG ~~~~~~~~ #
ACCOUNT = "@account"
PHONE_NR = 'number'
API_ID = APIID
API_HASH = "APIHASH"
app = Client( ACCOUNT, phone_number=PHONE_NR, api_id=API_ID, api_hash=API_HASH )
### CHAT ID
# Variables
SOURCE_CHAT_A = chat_id
TARGET_CHAT_A = chat_id
# ~~~~~~~~~~~~~~~~~~~~~~ #
# Commands
@app.on_message(filters.chat(SOURCE_CHAT_A))
def copy_to_channel(client, message):
message.copy( chat_id=TARGET_CHAT_A )
app.run()
是否可以将消息拆分成多个部分,并将源聊天中的大消息打印为目标聊天中的少量小消息?
例如,如果是来自源聊天的消息:
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
在目标聊天中将出现 3 条单独的消息:
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
我的 python 水平绝对不够高,我已经坐了一个星期解决这个问题,但找不到解决方案。感谢您提供任何类型的帮助:)
假设你想拆分双换行符,你可以这样做:
@app.on_message(filters.chat(SOURCE_CHAT_A))
def copy_to_channel(client, message):
# split message text on double newlines
messages = message.text.split("\n\n")
# send each message to the target chat
for message in messages:
client.send_message(TARGET_CHAT_A, message)
或者要将消息拆分为 300 个字符,您可以使用以 300 递增的 string slicing. Loop on a range 来获取每个批次的起始索引,然后发送切片字符串:
@app.on_message(filters.chat(SOURCE_CHAT_A))
def copy_to_channel(client, message):
# split message into batches of 300 characters
for i in range(0, len(message.text), 300):
client.send_message(
chat_id=SOURCE_CHAT_B,
text=message.text[i:i+300]
)
我编写了一个代码,使用 pyrogram python 库将消息从源聊天复制到目标聊天。代码工作正常,但它从源聊天中制作消息的绝对副本。
我的代码示例:
#!/usr/bin/env python3
from pyrogram import Client
from pyrogram import filters
# ~~~~~~ CONFIG ~~~~~~~~ #
ACCOUNT = "@account"
PHONE_NR = 'number'
API_ID = APIID
API_HASH = "APIHASH"
app = Client( ACCOUNT, phone_number=PHONE_NR, api_id=API_ID, api_hash=API_HASH )
### CHAT ID
# Variables
SOURCE_CHAT_A = chat_id
TARGET_CHAT_A = chat_id
# ~~~~~~~~~~~~~~~~~~~~~~ #
# Commands
@app.on_message(filters.chat(SOURCE_CHAT_A))
def copy_to_channel(client, message):
message.copy( chat_id=TARGET_CHAT_A )
app.run()
是否可以将消息拆分成多个部分,并将源聊天中的大消息打印为目标聊天中的少量小消息?
例如,如果是来自源聊天的消息:
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
在目标聊天中将出现 3 条单独的消息:
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage Testmessage
我的 python 水平绝对不够高,我已经坐了一个星期解决这个问题,但找不到解决方案。感谢您提供任何类型的帮助:)
假设你想拆分双换行符,你可以这样做:
@app.on_message(filters.chat(SOURCE_CHAT_A))
def copy_to_channel(client, message):
# split message text on double newlines
messages = message.text.split("\n\n")
# send each message to the target chat
for message in messages:
client.send_message(TARGET_CHAT_A, message)
或者要将消息拆分为 300 个字符,您可以使用以 300 递增的 string slicing. Loop on a range 来获取每个批次的起始索引,然后发送切片字符串:
@app.on_message(filters.chat(SOURCE_CHAT_A))
def copy_to_channel(client, message):
# split message into batches of 300 characters
for i in range(0, len(message.text), 300):
client.send_message(
chat_id=SOURCE_CHAT_B,
text=message.text[i:i+300]
)