英文:
What code does TelegramClient use to send stickers to groups?
问题 {#heading}
我正在为Telegram制作一个软件,它将通过Telegram聊天数据库以贴纸形式发送一定数量的消息,循环结束后等待24小时并重复循环。但是代码本身出现了某种原因而无法工作。
以下是代码本身:
from telethon.sync import TelegramClient
import time
import datetime
API_ID = 25133720
API_HASH = '08af2f4d9e4c65cb242d220e533338e8'
messages_sent = 0
def main():
global messages_sent
chat_usernames = ['@forexjobinkiev', '@spb_work_choogl', '@temshchik_chat', '@work_online_today', '@freelancerchat1', '@chat_sportbetwiner', '@SravkiS', '@dropshiping_tovarka_ua', '@Kiev_Work_Job682', '@robota_com5']
with TelegramClient('anon', API_ID, API_HASH) as client:
while True:
current_time = datetime.datetime.now().time()
if current_time.hour == 0 and current_time.minute == 0:
messages_sent = 0
if messages_sent < 35:
for chat_username in chat_usernames:
chat_entity = client.get_entity(chat_username)
for sticker_id in ['CAACAgEAAxkBAAK2RmTUSWd8kMnKSh4vNz_YpKaRGL2tAAIuAwAC4WihRo38v4W03s7TMAQ', 'CAACAgEAAxkBAAK2SGTUSYInvjbUPyaP3mpG22JsVhjiAAIYBAACwHKgRlJDWLOjrJKVMAQ']:
client.send_sticker(chat_entity, sticker=sticker_id)
messages_sent += 1
if messages_sent >= 35:
break
time.sleep(20)
next_day = datetime.datetime.now() + datetime.timedelta(days=1)
time_to_wait = datetime.datetime.combine(next_day.date(), datetime.time.min) - datetime.datetime.now()
time.sleep(time_to_wait.total_seconds())
if __name__ == "__main__":
main()
运行代码后,默认情况下不会检测到任何错误,除了PIP,但运行代码后,它显示以下内容:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\pythonProject4\main.py", line 37, in <module>
main()
File "C:\Users\User\PycharmProjects\pythonProject4\main.py", line 26, in main
client.send_sticker(chat_entity, sticker=sticker_id)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'TelegramClient' object has no attribute 'send_sticker'
英文:
I am making a software for telegram, which will send a certain number of messages in the form of stickers through the telegram chat database, after the end of the cycle wait 24 hours and repeat the cycle. but the code itself does not work for some reason.
Here is the code itself:
from telethon.sync import TelegramClient
import time
import datetime
API_ID = 25133720
API_HASH = '08af2f4d9e4c65cb242d220e533338e8'
messages_sent = 0
def main():
global messages_sent
chat_usernames = ['@forexjobinkiev', '@spb_work_choogl', '@temshchik_chat', '@work_online_today', '@freelancerchat1', '@chat_sportbetwiner', '@SravkiS', '@dropshiping_tovarka_ua', '@Kiev_Work_Job682', '@robota_com5']
with TelegramClient('anon', API_ID, API_HASH) as client:
while True:
current_time = datetime.datetime.now().time()
if current_time.hour == 0 and current_time.minute == 0:
messages_sent = 0
if messages_sent < 35:
for chat_username in chat_usernames:
chat_entity = client.get_entity(chat_username)
for sticker_id in ['CAACAgEAAxkBAAK2RmTUSWd8kMnKSh4vNz_YpKaRGL2tAAIuAwAC4WihRo38v4W03s7TMAQ', 'CAACAgEAAxkBAAK2SGTUSYInvjbUPyaP3mpG22JsVhjiAAIYBAACwHKgRlJDWLOjrJKVMAQ']:
client.send_sticker(chat_entity, sticker=sticker_id)
messages_sent += 1
if messages_sent >= 35:
break
time.sleep(20)
next_day = datetime.datetime.now() + datetime.timedelta(days=1)
time_to_wait = datetime.datetime.combine(next_day.date(), datetime.time.min) - datetime.datetime.now()
time.sleep(time_to_wait.total_seconds())
if __name__ == "__main__":
main()
It does not detect any errors by default except PIP, but after running the code it shows the following:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\pythonProject4\main.py", line 37, in <module>
main()
File "C:\Users\User\PycharmProjects\pythonProject4\main.py", line 26, in main
client.send_sticker(chat_entity, sticker=sticker_id)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'TelegramClient' object has no attribute 'send_sticker'
答案1 {#1}
得分: 0
Telethon库在官方版本中从未内置send_sticker
方法。
贴纸是具有特殊属性的文档。
您可以使用client.send_file(chat, 'your-sticker.webp')
来将同一工作目录中的your-sticker.webp
文件发送到chat
。
在v1中,文件ID已经有一段时间没有维护了,将来的版本中可能也不会直接支持。
您还可以将现有消息的message.sticker
作为send_file
的参数进行重用。
英文:
The Telethon library has never had a send_sticker
method built-in in the official version.
Stickers are documents with a special attribute.
You can client.send_file(chat, 'your-sticker.webp')
to send the your-sticker.webp
file from the same working directory to chat
.
File IDs have not been maintained for quite some time in v1 and likely won't work out-of-the-box in future versions either.
You can also reuse the message.sticker
of existing messages as the parameter for send_file
.