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'
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
.