Ë
    £| k½…  ã                  óÚ  — d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZmZmZmZ ddlmZ ddlmZ dd	lmZmZmZmZmZmZmZmZ  ej8                  e«      Z ej>                  d
«      Z  G d„ de!«      Z" G d„ de"«      Z#h d£Z$h d£Z%dZ&dZ'd*d„Z(d+d„Z) G d„ d«      Z*d,d„Z+ G d„ d«      Z, G d„ de,«      Z- G d„ de,«      Z.dddd d!d"œZ/ G d#„ d$e,«      Z0ddd%d&œd-d'„Z1dd(œd.d)„Z2y)/aã  A userbot - a normal Telegram account - that does what the bot is not allowed to do in a channel.

Deleting: Telegram's documentation says bots can only delete messages younger than 48 hours. The bot always tries
first; whatever is left goes to the userbot.
Editing: Telegram refuses the bot an edit of a post that belongs to another bot (its buttons were added by that bot,
or it was posted through it). The bot always tries first; when Telegram refuses, the userbot makes the edit (text,
caption or a new photo / file). A user account can't set inline buttons, so Telegram may drop them with that edit -
the bot puts them back where it can.
A new photo / file goes to the userbot through the userbot's own chat with the bot: the bot sends it there (Telegram
just points to the file it already has - nothing is uploaded), the userbot finds it in that chat and edits the post
with that copy, again by reference. So a big file is as quick as a small one and keeps its thumbnail. Only when that
chat does not work (the userbot account never started the bot) is the file downloaded and uploaded again.

How it gets into the channel:
  1. the bot makes a one-time invite link (or reads the channel's own link) and the userbot joins with it
     (public channels without a usable link: the userbot joins by name);
  2. if the userbot is not allowed to do the job there yet and the bot has the "Add new admins" right, the bot
     makes the userbot an admin with that one right ("Delete messages" / "Edit messages of others");
  3. the userbot does the job, and afterwards the bot gives the admin rights back as they were
     (USERBOT_KEEP_ADMIN=true keeps the new right).
The session lives in USERBOT_SESSION (made on your own computer with make_userbot_session.py).
é    )ÚannotationsN)ÚOptional)ÚTelegramClientÚerrorsÚ	functionsÚtypesÚutils)ÚStringSessioné   ©Ú	with_rich)Ú	MediaFileÚedit_rawÚflood_retryÚ
get_rightsÚmedia_from_refÚ	media_keyÚpeer_ofÚ
with_mediaz€(?i)(?:https?://)?(?:t\.me|telegram\.me|telegram\.dog)/(?:\+|joinchat/)([A-Za-z0-9_-]{8,})|tg://join\?invite=([A-Za-z0-9_-]{8,})c                  ó   — e Zd ZdZy)ÚUserbotErrorz;Something the owner can fix; the text is meant to be shown.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú/app/app/userbot.pyr   r   -   s   „ ÚEr   r   c                  ó8   ‡ — e Zd ZdZddˆ fd„Zedd„«       Zˆ xZS )ÚUserbotRefusedz_Telegram refused what the userbot tried; `name` is the Telegram error (e.g. MediaInvalidError).c                ó2   •— t         ‰| �  |«       || _        y ©N)ÚsuperÚ__init__Úname)ÚselfÚtextr%   Ú	__class__s      €r   r$   zUserbotRefused.__init__4   s   ø€ Ü‰Ñ˜ÔØˆ�	r   c                ó&   — | j                   t        v S )z[The refusal is about the photo / file the userbot used (so trying it another way may help).)r%   Ú_MEDIA_REFUSED©r&   s    r   Úabout_mediazUserbotRefused.about_media8   s   € ð �y‰yœNÐ*Ð*r   )Ú )r'   Ústrr%   r.   ©ÚreturnÚbool)r   r   r   r   r$   Úpropertyr,   Ú__classcell__©r(   s   @r   r    r    1   s   ø„ Ùiöð ò+ó ô+r   r    >   ÚMediaEmptyErrorÚMediaInvalidErrorÚPhotoInvalidErrorÚDocumentInvalidErrorÚFileReferenceEmptyErrorÚFileReferenceExpiredErrorÚFileReferenceInvalidError>   ÚPeerIdInvalidErrorÚUserIdInvalidErrorÚUserIsBlockedErrorÚUserDeactivatedErrorÚInputUserDeactivatedErroré
   ç      à?c                ó„   — t         j                  | xs d«      }|r$|j                  d«      xs |j                  d«      S d S )Nr-   r   é   )Ú
_INVITE_RXÚsearchÚgroup)ÚlinkÚms     r   Úparse_invite_hashrJ   K   s8   € Ü×Ñ˜$š* "Ó%€AÙ)*ˆA�G‰G�A‹JÒ$˜!Ÿ'™' !›*Ð4°Ð4r   c                ón   — t        t        | j                  «      | j                  | j                  d¬«      S )NF)Úreceive_updates)r   r
   Úuserbot_sessionÚapi_idÚapi_hash)Úcfgs    r   Ú_default_factoryrQ   P   s'   € Üœ-¨×(;Ñ(;Ó<¸c¿j¹jÈ#Ï,É,ÐhmÔnÐnr   c                  óˆ   — e Zd Zdd„Zedd„«       Zedd„«       Zedd„«       Zdd„Zdd„Z	dd„Z
dd	„Zdd
„Zd„ Zdd„Zdd„Zy)ÚUserbotNc                óš   — || _         || _        |xs t        | _        d | _        d | _        d | _        t        j                  «       | _	        y r"   )
ÚbotrP   rQ   Ú_factoryÚclientÚmeÚ
last_errorÚasyncioÚLockÚlock)r&   Ú
bot_clientrP   Úclient_factorys       r   r$   zUserbot.__init__U   s?   € ØˆŒØˆŒØ&Ò:Ô*:ˆŒØˆŒØˆŒØ)-ˆŒÜ—L‘L“Nˆ�	r   c                óB   — t        t        | j                  dd«      «      S )NrM   r-   ©r1   ÚgetattrrP   r+   s    r   ÚenabledzUserbot.enabled^   s   € ä”G˜DŸH™HÐ&7¸Ó<Ó=Ð=r   c                ó   — | j                   d uS r"   )rW   r+   s    r   ÚreadyzUserbot.readyb   s   € à�{‰{ $Ð&Ð&r   c                óB   — t        t        | j                  dd«      «      S )NÚuserbot_keep_adminFr`   r+   s    r   Ú
keep_adminzUserbot.keep_adminf   s   € ä”G˜DŸH™HÐ&:¸EÓBÓCÐCr   c              ƒ  ó`  K  — | j                   sd| _        y| j                  �yd}	 | j                  | j                  «      }|j                  «       ƒ d{  –—†  |j                  «       ƒ d{  –—† st        d«      ‚|j                  «       ƒ d{  –—† }t        |dd«      rt        d«      ‚	 ||dc| _        | _        | _        t        j'                  dt        |dd«      xs t        |dd«      |j(                  «       y7 Œª7 Œ”7 Œs# t        $ rœ}t        |t        «      rt        |«      n%t        |«      j                  › dt        |«      dd	 › �| _        t        j!                  d
| j                  «       |�*	 |j#                  «       ƒ d{  –—†7   n# t        $ r Y nw xY wY d}~yd}~ww xY w­w)zTLog the userbot in. True when it is usable; otherwise the reason is in `last_error`.zUSERBOT_SESSION is not set.FNTzoThe userbot session is not logged in any more (or it was revoked). Make a new one with make_userbot_session.py.rU   zNUSERBOT_SESSION belongs to a bot. The userbot has to be a normal user account.z: éÈ   z the userbot is not available: %szuserbot logged in as %s (id %s)ÚusernameÚ
first_namer-   )rb   rY   rW   rV   rP   ÚconnectÚis_user_authorizedr   Úget_mera   Ú	ExceptionÚ
isinstancer.   Útyper   ÚlogÚwarningÚ
disconnectrX   ÚinfoÚid)r&   rW   rX   Úes       r   rl   zUserbot.connectj   s”  è ø€ à�|Š|Ø;ˆDŒOØØ�;‰;Ð"ØØˆð	Ø—]‘] 4§8¡8Ó,ˆFØ—.‘.Ó"×"Ð"Ø×2Ñ2Ó4×4Ð4Ü"ðCóð ð —}‘}“×&ˆBÜ�r˜5 %Ô(Ü"Ð#sÓtÐtð )ð 17¸¸DÐ-ˆŒ�T”W˜dœoÜ�‰Ð2´G¸BÀ
ÈDÓ4QÒ4rÔU\Ð]_ÐamÐoqÓUrÐtv×tyÑtyÔzØð) #øØ4øð
 'ùô ò 	Ü(2°1´lÔ(Cœc !œfÌDÐQRËG×L\ÑL\ÐK]Ð]_Ô`cÐdeÓ`fÐgkÐhkÐ`lÐ_mÐInˆDŒOÜ�K‰KÐ:¸D¿O¹OÔLØÐ!ðØ ×+Ñ+Ó-×-Ò-øÜ ò Ùðúäûð	üs–   ‚$F.§.D ÁD ÁD Á-DÁ."D ÂDÂD Â-AF.Ä D ÄD ÄD Ä	F+ÄA(F&Å8FÆFÆFÆF&Æ	FÆF&ÆFÆF&Æ!F.Æ&F+Æ+F.c              ƒ  óŽ   K  — | j                   d c}| _         |�	 |j                  «       ƒ d {  –—†  y y 7 Œ# t        $ r Y y w xY w­wr"   )rW   rt   ro   )r&   rW   s     r   ÚclosezUserbot.closeŠ   sN   è ø€ Ø"Ÿk™k¨4Ðˆ�”ØÐðØ×'Ñ'Ó)×)Ñ)ð à)ùÜò Ùðüs0   ‚Aš6 ­4®6 ²A´6 ¶	A¿AÁAÁAc                ó  — | j                   sy| j                  rVt        | j                  dd «      }|rd|› �nt        | j                  dd«      xs d}d|› d| j                  j                  › d	�S d
| j
                  xs d› �S )Nz%not set up (USERBOT_SESSION is empty)rj   Ú@rk   r-   zthe accountzconnected as z (id Ú)zset up, but not connected: úunknown reason)rb   rd   ra   rX   rv   rY   )r&   r%   Úwhos      r   ÚdescribezUserbot.describe’   s|   € Ø�|Š|Ø:Ø�:Š:Ü˜4Ÿ7™7 J°Ó5ˆDÙ $�A�d�V‘*¬7°4·7±7¸LÈ"Ó+MÒ+^ÐQ^ˆCØ" 3 % u¨T¯W©W¯Z©Z¨L¸Ð:Ð:Ø,¨T¯_©_Ò-PÐ@PÐ,QÐRÐRr   c                ó   — t        | |«      S r"   )ÚUserbotDeleter©r&   Úchs     r   ÚdeleterzUserbot.deleter›   s   € Ü˜d BÓ'Ð'r   c                ó   — t        | |«      S r"   )ÚUserbotEditorr‚   s     r   ÚeditorzUserbot.editorž   s   € Ü˜T 2Ó&Ð&r   c              ƒ  óÈ   K  — | j                   sy| j                  s| j                  «       ƒ d{  –—†  | j                  sy| j                  |«      }|j                  |fS 7 Œ0­w)a  (delete function, deleter) to hand to the delete step, or (None, None) when no userbot is usable.

        Nothing happens in the channel until the bot has failed to delete something and the function is called;
        the caller must always `await deleter.close()` afterwards.
        )NNN)rb   rd   rl   r„   Údelete)r&   rƒ   Úds      r   Úfallback_forzUserbot.fallback_for¡   sS   è ø€ ð �|Š|ØØ�zŠzØ—,‘,“.× Ð Ø�zŠzØØ�L‰L˜ÓˆØ�x‰x˜ˆ{Ðð	 !ús   ‚-A"¯A °1A"c              ƒ  ó®   K  — | j                   sy| j                  s| j                  «       ƒ d{  –—†  | j                  r| j                  |«      S dS 7 Œ#­w)znAn editor for posts in `ch`, or None when no userbot is usable. The caller must always `await editor.close()`.N)rb   rd   rl   r‡   r‚   s     r   Ú
editor_forzUserbot.editor_for°   sD   è ø€ à�|Š|ØØ�zŠzØ—,‘,“.× Ð Ø"&§*¢*ˆt�{‰{˜2‹Ð6°$Ð6ð !ús   ‚-A¯A°$Ac              ƒ  ó¤   K  — | j                   sy| j                  s| j                  «       ƒ d{  –—†  | j                  rt        | |«      S dS 7 Œ­w)zmA reader for posts in `ch`, or None when no userbot is usable. The caller must always `await reader.close()`.N)rb   rd   rl   ÚUserbotReaderr‚   s     r   Ú
reader_forzUserbot.reader_for¸   sC   è ø€ à�|Š|ØØ�zŠzØ—,‘,“.× Ð Ø*.¯*ª*Œ}˜T 2Ó&Ð>¸$Ð>ð !ús   ‚-A¯A°Ar"   r/   ©r0   ÚNone)r0   r.   )r0   z'UserbotDeleter')r0   z'UserbotEditor')r0   zOptional['UserbotEditor'])r0   zOptional['UserbotReader'])r   r   r   r$   r2   rb   rd   rg   rl   ry   r   r„   r‡   r‹   r�   r�   r   r   r   rS   rS   T   so   „ ó#ð ò>ó ð>ð ò'ó ð'ð òDó ðDóó@óSó(ó'òó7ô?r   rS   c                ó†   — | �8| j                  «       j                  «       D ��ci c]  \  }}|dk7  sŒ||“Œ c}}S i S c c}}w )NÚ_)Úto_dictÚitems)ÚrightsÚkÚvs      r   Ú_rights_dictrš   Á   sE   € ØFLÐFX˜VŸ^™^Ó-×3Ñ3Ô5ÔBÑ5‘T�Q˜¸¸c»ˆAˆq‰DÐ5ÒBÐ`Ð^`Ð`ùÓBs   ¤=²=c                  óŽ   — e Zd ZdZdZdZdZdZdd„Zdd„Z	dd„Z
dd	„Zd
„ Zdd„Zd„ Zdd„Zd„ Zdd„Zdd„Zd„ Zdd„Zdd„Zd„ Zy)Ú_ChannelJobzêEverything needed to do ONE job in ONE channel with the userbot. Call close() when done, whatever happened.

    What the job needs is named by RIGHT (the field of ChatAdminRights) and RIGHT_NAME (what the Telegram apps call it).
    Údelete_messageszDelete messageszdelete messagesr‰   c                ó�   — || _         || _        d | _        g | _        d| _        d| _        d| _        d | _        d | _        d | _	        y ©NF)
Úubrƒ   ÚpeerÚnotesÚ_readyÚ_holds_lockÚ	_promotedÚ_beforeÚ	_bot_userÚ_link)r&   r    rƒ   s      r   r$   z_ChannelJob.__init__Ð   sI   € ØˆŒØˆŒØˆŒ	ØˆŒ
ØˆŒØ ˆÔØˆŒØˆŒØˆŒØ$(ˆ�
r   c              ƒ  ób  K  — | j                   j                  s&t        d| j                   j                  xs d› �«      ‚| j                  s_| j
                  s3| j                   j                  j                  «       ƒ d{  –—†  d| _        | j                  «       ƒ d{  –—†  d| _        yy7 Œ,7 Œ­w)zYGet the channel ready (joined, with the right). The userbot's lock is kept until close().úThe userbot is not connected: r}   NT)	r    rd   r   rY   r£   r¤   r\   ÚacquireÚ_preparer+   s    r   Ú_beginz_ChannelJob._beginÝ   s‰   è ø€ à�w‰w�}Š}ÜÐ!?ÀÇÁ×@RÑ@RÒ@fÐVfÐ?gÐhÓiÐiØ�{Š{Ø×#Ò#Ø—g‘g—l‘l×*Ñ*Ó,×,Ð,Ø#'�Ô Ø—-‘-“/×!Ð!ØˆD�Kð à,øà!ús$   ‚A<B/Á>B+Á?B/ÂB-ÂB/Â-B/c              ƒ  óî  K  — | j                   j                  | j                  }}	 | j                  r·| j                  �«| j                   j
                  s•| j                  xs t        j                  «       df\  }}	  |t        j                  j                  t        |«      | j                  ||¬«      «      ƒ d{  –—†  | j                  j                  | j                  €dnd«       | j*                  rD	  |t        j,                  j/                  t        |«      | j*                  d¬	«      «      ƒ d{  –—†  d\  | _        | _        | j2                  r,d| _        | j                   j4                  j7                  «        yy7 ŒÅ# t        $ rY}t         j#                  d|j$                  t'        |«      j(                  «       | j                  j                  d«       Y d}~Œ÷d}~ww xY w7 Œ³# t        $ r3}t         j1                  d
t'        |«      j(                  «       Y d}~Œèd}~ww xY w# d\  | _        | _        | j2                  r,d| _        | j                   j4                  j7                  «        w w xY w­w)z_Undo what this job changed: give the admin rights back as they were, revoke the temporary link.Nr-   ©ÚchannelÚuser_idÚadmin_rightsÚrankz0took the admin right away from the userbot againz0put the userbot's admin rights back as they werez&could not demote the userbot in %s: %szKcould not take the admin right away from the userbot - please do it by handT)r¡   rH   Úrevokedz.could not revoke the temporary invite link: %s)FNF)r    rU   rƒ   r¥   r§   rg   r¦   r   ÚChatAdminRightsr   ÚchannelsÚEditAdminRequestr   r¢   Úappendro   rr   rs   rv   rq   r   r¨   ÚmessagesÚEditExportedChatInviteRequestru   r¤   r\   Úrelease)r&   rU   rƒ   r—   r³   rw   s         r   ry   z_ChannelJob.closeè   s  è ø€ à—'‘'—+‘+˜tŸw™wˆRˆð	'Ø�~Š~ $§.¡.Ð"<ÀTÇWÁW×EWÒEWØ#Ÿ|™|ÒL´×0EÑ0EÓ0GÈÐ/L‘�˜ðuÙÜ!×*Ñ*×;Ñ;Ü$+¨B£K¸¿¹ÐV\Ðcgð <ó ó÷ ð ð
 —J‘J×%Ñ%àŸ<™<Ð/ñ KàOôð �zŠzðaÙÜ!×*Ñ*×HÑHÌgÐVXËkÐ`d×`jÑ`jÐtxÐHÓyó÷ ð ð *5Ñ&ˆDŒN˜DœJØ×ÒØ#(�Ô Ø—‘—‘×$Ñ$Õ&ð  ð-ùô !ò uÜ—K‘KÐ HÈ"Ï%É%ÔQUÐVWÓQX×QaÑQaÔbØ—J‘J×%Ñ%Ð&s×tÑtûðuúð
ùô !ò aÜ—H‘HÐMÌtÐTUËw×O_ÑO_×`Ñ`ûðaûð *5Ñ&ˆDŒN˜DœJØ×ÒØ#(�Ô Ø—‘—‘×$Ñ$Õ&ð  üs’   ‚#I5¦AH) Á<?F Â;FÂ<-F Ã)H) Ã6>G* Ä4G(Ä5G* Ä9AI5ÆF Æ	G%ÆAG ÇH) Ç G%Ç%H) Ç(G* Ç*	H&Ç3)H!ÈH) È!H&È&H) È)A	I2É2I5c              ƒ  óº   K  — | j                  «       ƒ d {  –—† }|€| j                  «       ƒ d {  –—† }|| _        | j                  «       ƒ d {  –—†  y 7 Œ>7 Œ&7 Œ	­wr"   )Ú_member_peerÚ_joinr¡   Ú_ensure_right)r&   r¡   s     r   r¬   z_ChannelJob._prepare
  sP   è ø€ Ø×&Ñ&Ó(×(ˆØˆ<ØŸ™›×%ˆDØˆŒ	Ø× Ñ Ó"×"Ñ"ð	 )øà%øà"ús1   ‚A–A—A°A±AÁAÁAÁAÁAc              ƒ  óÂ  K  — | j                   j                  }	 |j                  t        j                  | j
                  j                  «      «      ƒ d{  –—† }t        |t        j                  «      sy |t        j                  j                  |t        j                  «       «      «      ƒ d{  –—†  |S 7 Œa7 Œ# t        j                  t        t         f$ r Y yw xY w­w)zDThe channel as the userbot sees it, if the userbot is in it already.N)r    rW   Úget_input_entityr   ÚPeerChannelrƒ   rv   rp   ÚInputPeerChannelr   r¶   ÚGetParticipantRequestÚInputPeerSelfr   ÚRPCErrorÚ
ValueErrorÚ	TypeError)r&   r    r¡   s      r   r½   z_ChannelJob._member_peer  s«   è ø€ à�W‰W�^‰^ˆð	Ø×,Ñ,¬U×->Ñ->¸t¿w¹w¿z¹zÓ-JÓK×KˆDÜ˜d¤E×$:Ñ$:Ô;ØÙ”Y×'Ñ'×=Ñ=¸dÄE×DWÑDWÓDYÓZÓ[×[Ð[ð ˆð Løð \ùÜ—‘¤¬YÐ7ò 	Ùð	üsX   ‚Cš;B; ÁB7ÁB; Á4CÁ5;B; Â0B9Â1B; Â5CÂ7B; Â9B; Â;CÃCÃCÃCc              ƒ  ó*  K  — | j                   j                  t        | j                  «      }}	 t        j                  j                  t        j                  j                  «      t	        j                  d¬«      z   } |t        j                  j                  ||dd¬«      «      ƒ d {  –—† }t        |dd «      }|r$|| _        | j                  j                  d«       |S 	 	  |t        j.                  j1                  |«      «      ƒ d {  –—† }t        t        |j2                  dd «      dd «      }|r| j                  j                  d	«       |S 	 y 7 Œ©# t         j"                  $ rH}t$        j'                  d| j                  j(                  t+        |«      j,                  «       Y d }~ŒÉd }~ww xY w7 Œ§# t         j"                  $ rH}t$        j'                  d
| j                  j(                  t+        |«      j,                  «       Y d }~y d }~ww xY w­w)Nr   )ÚhourszCleanup helper)r¡   Úexpire_dateÚusage_limitÚtitlerH   z+made a one-time invite link for the userbotz)could not create an invite link in %s: %sÚexported_invitez"used the channel's own invite linkz(could not read the invite link of %s: %s)r    rU   r   rƒ   ÚdatetimeÚnowÚtimezoneÚutcÚ	timedeltar   r¹   ÚExportChatInviteRequestra   r¨   r¢   r¸   r   rÆ   rr   ru   rv   rq   r   r¶   ÚGetFullChannelRequestÚ	full_chat)r&   rU   r¡   ÚexpiresÚresrH   rw   Úfulls           r   Ú_invite_linkz_ChannelJob._invite_link  s·  è ø€ Ø—G‘G—K‘K¤¨¯©Ó!1ˆTˆð	`Ü×'Ñ'×+Ñ+¬H×,=Ñ,=×,AÑ,AÓBÄX×EWÑEWÐ^_ÔE`Ñ`ˆGÙÜ×"Ñ"×:Ñ:ÀÐRYÐghð  qAÐ:ó  Bó÷ ˆCô ˜3 ¨Ó-ˆDÙØ!�”
Ø—
‘
×!Ñ!Ð"OÔPØ�ð ð	_ÙœY×/Ñ/×EÑEÀdÓKÓL×LˆDÜœ7 4§>¡>Ð3DÀdÓKÈVÐUYÓZˆDÙØ—
‘
×!Ñ!Ð"FÔGØ�ð ð
 ð%ùô �‰ò 	`Ü�H‰HÐ@À$Ç'Á'Ç*Á*ÌdÐSTËg×N^ÑN^×_Ñ_ûð	`úð Mùô
 �‰ò 	_Ü�H‰HÐ?ÀÇÁÇÁÌTÐRSËW×M]ÑM]×^Ð^Øûð	_üs„   ‚,H¯A:E Â)EÂ*6E Ã HÃ#(F5 ÄF3ÄAF5 ÅHÅHÅE ÅF0Å(>F+Æ&HÆ+F0Æ0HÆ3F5 Æ5HÇ>HÈHÈHÈHc              ƒ  óÊ  K  — | j                  «       ƒ d {  –—† }|rt        |«      nd }|r8| j                  |«      ƒ d {  –—† }|�| j                  j	                  d«       |S | j
                  j                  rJ| j                  | j
                  j                  «      ƒ d {  –—† }| j                  j	                  d«       |S t        d| j                  › d�«      ‚7 ŒÆ7 Œž7 Œ>­w)Nzthe userbot joined the channelz1the userbot joined the channel by its public nameuÊ   I could not get the userbot into this private channel: Telegram gave me no invite link. Give the bot the â€œInvite users via linkâ€� admin right, or add the userbot to the channel yourself (with the â€œu   â€� admin right).)
rÚ   rJ   Ú_import_inviter¢   r¸   rƒ   rj   Ú_join_publicr   Ú
RIGHT_NAME)r&   rH   Úhr¡   s       r   r¾   z_ChannelJob._join5  sÑ   è ø€ Ø×&Ñ&Ó(×(ˆÙ'+Ô˜dÔ#°ˆÙØ×,Ñ,¨QÓ/×/ˆDØÐØ—
‘
×!Ñ!Ð"BÔCØ�Ø�7‰7×ÒØ×*Ñ*¨4¯7©7×+;Ñ+;Ó<×<ˆDØ�J‰J×ÑÐQÔRØˆKÜðà ŸO™OÐ,Ð,=ð?ó
ð 	
ð )øð 0øð
 =ús4   ‚C#–C—)C#Á CÁA!C#Â"C!Â#;C#ÃC#Ã!C#c              ƒ  ó*  ‡‡K  — | j                   j                  Š	 t        ˆˆfd„«      ƒ d {  –—† }| j                  |«      S 7 Œ# t        j                  $ r | j                  ‰«      ƒ d {  –—†7  cY S t        j                  $ r t        d«      ‚t        j                  t        j                  t        j                  f$ r Y y t        j                  t        j                  f$ r t        d«      ‚t        j                  $ r t        d«      ‚w xY w­w)Nc                 óN   •—  ‰t         j                  j                  ‰ «      «      S r"   )r   r¹   ÚImportChatInviteRequest)rß   r    s   €€r   Ú<lambda>z,_ChannelJob._import_invite.<locals>.<lambda>J  s   ø€ ©B¬y×/AÑ/A×/YÑ/YÐZ[Ó/\Ô,]r   z‘This channel's invite link needs an admin to approve new members. Approve the userbot's join request (or add the userbot yourself) and try again.úYThe userbot account is in too many channels. Leave a few with that account and try again.ú>The userbot account is banned in this channel. Unban it first.)r    rW   r   r   ÚUserAlreadyParticipantErrorÚ_peer_when_already_inÚInviteRequestSentErrorr   ÚInviteHashExpiredErrorÚInviteHashInvalidErrorÚInviteHashEmptyErrorÚChannelsTooMuchErrorÚUserChannelsTooMuchErrorÚUserBannedInChannelErrorÚ_peer_from_updates)r&   rß   rØ   r    s    ` @r   rÜ   z_ChannelJob._import_inviteG  sô   ùè ø€ Ø�W‰W�^‰^ˆð	aÜ#Ô$]Ó^×^ˆCð ×&Ñ& sÓ+Ð+ð _ùÜ×1Ñ1ò 	7Ø×3Ñ3°AÓ6×6Ð6Ò6Ü×,Ñ,ò 	Üð?óð ô ×-Ñ-¬v×/LÑ/LÌf×NiÑNiÐjò 	ÙÜ×+Ñ+¬V×-LÑ-LÐMò 	|ÜÐzÓ{Ð{Ü×.Ñ.ò 	aÜÐ_Ó`Ð`ð	aüsQ   „DœA ®A¯A ³DÁA Á'DÁ-A0Á.DÁ3DÁ5ADÃDÃADÄDc                ób  — t        |dd «      xs g D �cg c]  }t        |t        j                  «      sŒ|‘Œ! }}|D ]<  }|j                  | j
                  j                  k(  sŒ't        j                  |«      c S  t        |«      dk(  rt        j                  |d   «      S t        d«      ‚c c}w )NÚchatsr   r   zPThe userbot joined, but Telegram did not tell me which channel it is. Try again.)
ra   rp   r   ÚChannelrv   rƒ   r	   Úget_input_peerÚlenr   )r&   rØ   Úcrñ   s       r   rï   z_ChannelJob._peer_from_updatesZ  sœ   € Ü$ S¨'°4Ó8Ò>¸BÐ>Ó`Ñ>�qÄ:ÈaÔQV×Q^ÑQ^ÕC_’Ð>ˆÐ`ÛˆAØ�t‰t�t—w‘w—z‘zÓ!Ü×+Ñ+¨AÓ.Ò.ð ô ˆu‹:˜Š?Ü×'Ñ'¨¨a©Ó1Ð1ÜÐmÓnÐnùò as
   ”B,´B,c              ƒ  ót  K  — | j                   j                  }	  |t        j                  j	                  |«      «      ƒ d {  –—† }t        |t        j                  «      rt        j                  |j                  «      S 	 |j                  «       2 3 d {  –—† }t        |j                  dd «      | j                   j"                  k(  sŒ8t        |j                  t        j$                  «      sŒ]t        j                  |j                  «      c S 7 ŒÌ# t        j                  $ r Y Œ¥w xY w7 Œ”6 t'        d«      ‚­w)Nrv   zHThe userbot is in the channel, but I could not find it in its chat list.)r    rW   r   r¹   ÚCheckChatInviteRequestrp   r   ÚChatInviteAlreadyr	   ró   Úchatr   rÆ   Úiter_dialogsra   Úentityrƒ   rv   rò   r   )r&   rß   r    ru   rŠ   s        r   rç   z!_ChannelJob._peer_when_already_inc  sî   è ø€ Ø�W‰W�^‰^ˆð	ÙœI×.Ñ.×EÑEÀaÓHÓI×IˆDÜ˜$¤× 7Ñ 7Ô8Ü×+Ñ+¨D¯I©IÓ6Ð6ð 9ð —‘Ô(÷ 	6�!Ü�q—x‘x  tÓ,°·±·
±
Ó:¼zÈ!Ï(É(ÔTY×TaÑTaÕ?bÜ×+Ñ+¨A¯H©HÓ5Ò5ð Jùô �‰ò 	Ùð	úð	6øÐ(ô ÐeÓfÐfùsj   ‚D8š(D ÁDÁ<D Á?D8ÂD,ÂD*ÂD,Â/D8Ã	$D8Ã.!D8ÄD ÄD'Ä$D8Ä&D'Ä'D8Ä*D,Ä,D8c              ƒ  óü  ‡‡K  — | j                   j                  Š	 ‰j                  |«      ƒ d {  –—† Š	 t        ˆˆfd„«      ƒ d {  –—†  ‰S 7 Œ7 Œ# t        j
                  $ r Y ‰S w xY w# t        j                  t        j                  f$ r t        d«      ‚t        j                  $ r t        d«      ‚t        j                  t        j                  t        j                  f$ r t        d|› d�«      ‚w xY w­w)Nc                 óN   •—  ‰t         j                  j                  ‰ «      «      S r"   )r   r¶   ÚJoinChannelRequest)r¡   r    s   €€r   rã   z*_ChannelJob._join_public.<locals>.<lambda>u  s   ø€ ©"¬Y×-?Ñ-?×-RÑ-RÐSWÓ-XÔ*Yr   rä   rå   zThe userbot could not open @z!. Has the channel's name changed?)r    rW   rÁ   r   r   ræ   rì   rí   r   rî   ÚUsernameNotOccupiedErrorÚUsernameInvalidErrorÚChannelPrivateError)r&   rj   r¡   r    s     @@r   rÝ   z_ChannelJob._join_publicp  sï   ùè ø€ Ø�W‰W�^‰^ˆð	kØ×,Ñ,¨XÓ6×6ˆDðÜ!Ô"YÓZ×ZÐZð ˆð 7øàZùÜ×5Ñ5ò Øð ˆðûä×+Ñ+¬V×-LÑ-LÐMò 	|ÜÐzÓ{Ð{Ü×.Ñ.ò 	aÜÐ_Ó`Ð`Ü×/Ñ/´×1LÑ1LÌf×NhÑNhÐiò 	kÜÐ!=¸h¸ZÐGhÐiÓjÐjð	küsb   „C<œA- °A±A- ¶A ÁAÁ	A ÁC<ÁA- ÁA ÁA*Á&A- Á'C<Á)A*Á*A- Á-BC9Ã9C<c              ƒ  ó¼  K  — 	 | j                   j                  t        j                  j	                  | j
                  t        j                  «       «      «      ƒ d{  –—† }|j                  }t        |t        j                  «      ryt        |t        j                  «      rd|j                  t        |dd«      xs dfS y7 Œd# t        j                  $ r Y yw xY w­w)zg(is the creator, its admin rights or None, its rank) as the channel shows the userbot; None if unknown.N)TNr-   Fr³   r-   )FNr-   )r    rW   r   r¶   rÄ   r¡   r   rÅ   r   rÆ   Úparticipantrp   ÚChannelParticipantCreatorÚChannelParticipantAdminr²   ra   )r&   rØ   Úps      r   Ú_userbot_statusz_ChannelJob._userbot_status�  s¯   è ø€ ð	ØŸ™Ÿ™¤y×'9Ñ'9×'OÑ'OÐPT×PYÑPYÔ[`×[nÑ[nÓ[pÓ'qÓr×rˆCð �O‰OˆÜ�aœ×8Ñ8Ô9Ø!Ü�aœ×6Ñ6Ô7Ø˜!Ÿ.™.¬'°!°V¸TÓ*BÒ*HÀbÐHÐHØð sùÜ�‰ò 	Ùð	üs<   ‚C„AC ÁCÁC Á!A CÃC ÃCÃCÃCÃCc              ƒ  ó˜   K  — | j                  «       ƒ d {  –—† }|€y|\  }}}|xs  t        t        || j                  d«      «      S 7 Œ1­wrŸ   )r  r1   ra   ÚRIGHT)r&   ÚstatusÚcreatorr—   r”   s        r   Ú_userbot_has_rightz_ChannelJob._userbot_has_rightŽ  sM   è ø€ Ø×+Ñ+Ó-×-ˆØˆ>ØØ#Ñˆ�˜ØÒBœ$œw v¨t¯z©z¸5ÓAÓBÐBð	 .ús   ‚A
–A—2A
c              ƒ  óf  K  — | j                  «       ƒ d {  –—† ry t        | j                  j                  | j                  «      ƒ d {  –—† }|�|j
                  r|j                  s&t        d| j                  › d| j                  › d�«      ‚| j                  «       ƒ d {  –—† }| j                  «       ƒ d {  –—† }|rt        |d   «      ni }|r|d   nd}	 | j                  j                  t        j                  j                  t!        | j                  «      |t#        j$                  di i |¥| j&                  di¥¤Ž|¬«      «      ƒ d {  –—†  d|c| _        | _        t9        |j;                  «       «      r6|d   |f| _        | j>                  jA                  d| j                  › d�«       n)| j>                  jA                  d| j                  › d�«       tC        d«      D ]9  }	| j                  «       ƒ d {  –—† r y tE        jF                  d«      ƒ d {  –—†  Œ; t        d| jH                  › d�«      ‚7 �Œ%7 �Œõ7 �Œ 7 �Œ‹7 Œ÷# t(        j*                  $ rN}t-        |«      j.                  }d	d
dddœj1                  |t3        |«      d d «      }t        d|› d|› �«      ‚d }~ww xY w7 Œ°7 Œ“­w)Nz*The userbot is in the channel but may not u£    there, and the bot may not add admins. Give the bot the â€œAdd new adminsâ€� right (it then promotes the userbot by itself), or make the userbot an admin with â€œõ   â€� yourself.r   rD   r-   Tr¯   z The bot may not give that right.z0The bot may not change that user's admin status.z5The channel already has the maximum number of admins.u-   The bot needs the â€œAdd new adminsâ€� right.)ÚRightForbiddenErrorÚUserAdminInvalidErrorÚAdminsTooMuchErrorÚChatAdminRequiredErroré–   z'I could not make the userbot an admin (ú). u   gave the userbot the â€œu#   â€� right on top of the ones it hadu'   made the userbot an admin with only â€œu   â€�é   rB   z@I made the userbot an admin, but Telegram still does not let it z there. Check its rights.r   )%r  r   r    rU   rƒ   ÚadminÚ
add_adminsr   ÚWHATrÞ   Ú_bot_view_of_userbotr  rš   r   r¶   r·   r   r   rµ   r	  r   rÆ   rq   r   Úgetr.   r¥   r§   ÚanyÚvaluesr¦   r¢   r¸   ÚrangerZ   ÚsleepÚVERB)
r&   r—   Úuserr
  Úhadr³   rw   r%   Úhintr”   s
             r   r¿   z_ChannelJob._ensure_right•  s‡  è ø€ Ø×(Ñ(Ó*×*Ð*ØÜ! $§'¡'§+¡+¨t¯w©wÓ7×7ˆØˆ> &§,¢,°6×3DÒ3DÜØ<¸T¿Y¹Y¸Kð H$à$(§O¡OÐ#4°MðCóð ð
 ×.Ñ.Ó0×0ˆØ×+Ñ+Ó-×-ˆÙ)/Œl˜6 !™9Ô%°RˆÙ"ˆv�aŠy¨ˆð	ZØ—'‘'—+‘+Ü×"Ñ"×3Ñ3Ü# D§G¡GÓ,Ø Ü!&×!6Ñ!6Ñ!SÐ9R¸CÐ9RÀÇÁÈTÑ9RÑ!SØð	 4ó ó÷ ð ð" *.¨tÐ&ˆŒ˜œÜˆs�z‰z‹|ÔØ" 1™I tÐ,ˆDŒLØ�J‰J×ÑÐ 8¸¿¹Ð8IÐIlÐmÕnà�J‰J×ÑÐ GÈÏÉÐGXÐX[Ð\Ô]Ü�r–ˆAØ×,Ñ,Ó.×.Ð.ÙÜ—-‘- Ó$×$Ñ$ð ô ØNÈtÏyÉyÈkÐYrÐsó
ð 	
ðS +ùà7ùð 1ùØ-ùðùô �‰ò 	ZÜ˜“7×#Ñ#ˆDà'IØ)[Ø&]Ø*Yñ	÷
 ‰c�$œ˜A›˜t ˜Ó%ð ô Ð!HÈÈÈcÐRVÐQWÐXÓYÐYûð	Zúð  /øà$ús�   ‚J1–H;—2J1Á	H>Á
AJ1Â!IÂ"J1Â9IÂ:J1ÃA5I	 ÅIÅI	 ÅB(J1Ç<J-Ç=J1ÈJ/È J1È>J1ÉJ1ÉJ1ÉI	 É	J*ÉA	J%Ê%J*Ê*J1Ê/J1c           
   ƒ  óì  K  — | j                   j                  | j                   j                  }}|j                  }	 |j	                  |«      ƒ d{  –—† }t        |t        j                  «      r*t        j                  |j                  |j                  «      S 	 t        j                  «       g}t        |dd«      t        |dd«      fD ])  }|sŒ|j                  t        j                  |«      «       Œ+ |D ]ª  }	  |t         j"                  j%                  t'        | j(                  «      |ddd¬«      «      ƒ d{  –—† }t        |dd«      xs g D ]N  }	t        |	dd«      |k(  sŒt        |	d	d«      €Œ"t        j                  |	j                  |	j                  «      c c S  Œ¬ t        |dd«      ri	 |j	                  |j.                  «      ƒ d{  –—† }t        |t        j                  «      r*t        j                  |j                  |j                  «      S 	 t1        d
| j2                  › d�«      ‚7 �Œã# t        $ r Y �Œ¨w xY w7 �Œ# t*        j,                  $ r Y �Œhw xY w7 Œ’# t        $ r Y ŒVw xY w­w)zdThe userbot account as an InputUser the BOT can use (needs the bot's own access hash for that user).Nrj   rk   r   ri   )r°   ÚfilterÚoffsetÚlimitÚhashÚusersrv   Úaccess_hashu’   The userbot joined, but the bot cannot find that account in the channel's member list, so it cannot promote it. Make the userbot an admin with â€œr  )r    rU   rX   rv   rÁ   rp   r   ÚInputPeerUserÚ	InputUserr±   r)  ro   ÚChannelParticipantsRecentra   r¸   ÚChannelParticipantsSearchr   r¶   ÚGetParticipantsRequestr   rƒ   r   rÆ   rj   r   rÞ   )
r&   rU   rX   ÚuidÚentÚqueriesÚqÚfltrØ   Úus
             r   r  z _ChannelJob._bot_view_of_userbotÃ  s-  è ø€ à—'‘'—+‘+˜tŸw™wŸz™zˆRˆØ�e‰eˆð	Ø×,Ñ,¨SÓ1×1ˆCÜ˜#œu×2Ñ2Ô3Ü—‘ s§{¡{°C·O±OÓDÐDð 4ô ×2Ñ2Ó4Ð5ˆÜ˜"˜j¨$Ó/´¸¸\È4Ó1PÓQˆAÚØ—‘œu×>Ñ>¸qÓAÕBð Ró ˆCðÙÜ×&Ñ&×=Ñ=ÄgÈdÏgÉgÓFVÐ_bÐklÐtwÐ~Ð=ó  Aó÷ �ô
 ˜S '¨4Ó0Ò6°BÒ6�Ü˜1˜d DÓ)¨SÓ0´W¸QÀÈtÓ5TÑ5`Ü Ÿ?™?¨1¯4©4°·±Ó?Ô?ñ 7ð ô �2�z 4Ô(ðØ×0Ñ0°·±Ó=×=�Ü˜c¤5×#6Ñ#6Ô7Ü Ÿ?™?¨3¯;©;¸¿¹ÓHÐHð 8ô ð=Ø=A¿_¹_Ð<MÈ]ð\ó
ð 	
ð5 2úô ò 	Úð	úðúô —?‘?ò Úðúð >ùô ò Ùðüs»   ‚9I4¼H6 ÁH3ÁAH6 Â6I4Ã+I4Ã;A I	Ä;IÄ<I	Å $I4Å%I4Å3>I4Æ2I% ÇI#ÇAI% ÈI4È3H6 È6	IÈ?I4ÉIÉI4ÉI	É	I ÉI4ÉI É I4É#I% É%	I1É.I4É0I1É1I4N©r    rS   r‘   )r0   úOptional[str])rß   r.   )rj   r.   r/   )r   r   r   r   r	  rÞ   r  r  r$   r­   ry   r¬   r½   rÚ   r¾   rÜ   rï   rç   rÝ   r  r  r¿   r  r   r   r   rœ   rœ   Å   sm   „ ñð
 €EØ"€JØ€DØ€Dó
)ó	ó'óD#ò
óò0
ó$,ò&oógóò"óCó,
ó\"
r   rœ   c                  ó   — e Zd ZdZdd„Zy)r�   z\Deletes messages in ONE channel with the userbot. Call close() when done, whatever happened.c              ƒ  óþ   ‡ ‡K  — ‰ j                  «       ƒ d {  –—†  ‰ j                  j                  Št        dt	        |«      d«      D ],  }t        |||dz    «      }t        |fˆˆ fd„	«      ƒ d {  –—†  Œ. y 7 Œ`7 Œ	­w)Nr   éd   c                óf   •—  ‰t         j                  j                  ‰j                  | ¬«      «      S )N)r°   rv   )r   r¶   ÚDeleteMessagesRequestr¡   )ÚpartrW   r&   s    €€r   rã   z'UserbotDeleter.delete.<locals>.<lambda>ñ  s(   ø€ ¡&¬×);Ñ);×)QÑ)QÐZ^×ZcÑZcÐhlÐ)QÓ)mÔ"nr   )r­   r    rW   r  rô   Úlistr   )r&   ÚidsÚir<  rW   s   `   @r   r‰   zUserbotDeleter.deleteë  sr   ùè ø€ Ø�k‰k‹m×ÐØ—‘—‘ˆÜ�qœ#˜c›( CÖ(ˆAÜ˜˜A  C¡Ð(Ó)ˆDÜØ Õnó÷ ñ ñ )ð 	øðús"   „A=˜A9™AA=Á1A;Á2A=Á;A=N)r>  r=  r0   r’   )r   r   r   r   r‰   r   r   r   r�   r�   è  s
   „ Ùfôr   r�   c                  ó<   ‡ — e Zd ZdZdZdZdd„Zˆ fd„Zdd	d„Zˆ xZ	S )
r�   zàReads posts of ONE channel as the userbot: what the bot cannot see (a post of another bot, the buttons it carries).
    Being a member of the channel is enough - no right is needed. Call close() when done, whatever happened.z
read postsÚreadc              ƒ  ó   K  — y ­wr"   r   r+   s    r   r¿   zUserbotReader._ensure_rightü  s	   è ø€ Øùs   ‚c              ƒ  óÆ  •K  — t         ‰| �  «       ƒ d {  –—† }|�|S 	 | j                  j                  j	                  «       2 3 d {  –—† }t        |j                  dd «      | j                  j                  k(  sŒ8t        |j                  t        j                  «      sŒ]t        j                  |j                  «      c S 7 Œ«7 Œ{6 	 y # t        j                  $ r Y y w xY w­w)Nrv   )r#   r½   r    rW   rú   ra   rû   rƒ   rv   rp   r   rò   r	   ró   r   rÆ   )r&   r¡   rŠ   r(   s      €r   r½   zUserbotReader._member_peerÿ  s¶   øè ø€ Ü‘WÑ)Ó+×+ˆØÐØˆKð	ØŸ7™7Ÿ>™>×6Ñ6Ô8÷ :�aÜ˜1Ÿ8™8 T¨4Ó0°D·G±G·J±JÓ>Ä:ÈaÏhÉhÔX]×XeÑXeÕCfÜ ×/Ñ/°·±Ó9Ò9ð ,øð:øÑ8ð
 øô �‰ò 	ØØð	üsh   ƒC!•C–C!Ÿ$C ÁCÁCÁCÁ/C Á;$C Â  C Ã C!ÃCÃC ÃC!ÃCÃC!ÃCÃC!c              ƒ  óæ  ‡ ‡‡‡K  — ‰ j                  «       ƒ d{  –—†  ‰ j                  j                  Š	 ‰rt        ˆˆˆ fd„«      ƒ d{  –—† }nt        ˆˆˆ fd„«      ƒ d{  –—† }t        |t        t        f«      r|n|gD �cg c]  }|€Œ|‘Œ	 c}S 7 Œz7 ŒK7 Œ4# t        j
                  $ r;}t        dt        |«      j                  › d�t        |«      j                  «      ‚d}~ww xY wc c}w ­w)zlThe post `message_id` (a list of one), or the newest `limit` posts, newest first - as the userbot sees them.Nc                 ó@   •— ‰ j                  ‰j                  ‰g¬«      S )N)r>  ©Úget_messagesr¡   )rW   Ú
message_idr&   s   €€€r   rã   z$UserbotReader.read.<locals>.<lambda>  s   ø€ °×0CÑ0CÀDÇIÁIÐT^ÐS_Ð0CÔ0`r   c                 ó>   •— ‰ j                  ‰j                  ‰¬«      S )N©r&  rF  )rW   r&  r&   s   €€€r   rã   z$UserbotReader.read.<locals>.<lambda>  s   ø€ °×0CÑ0CÀDÇIÁIÐUZÐ0CÔ0[r   zTelegram refused the userbot (ú).)r­   r    rW   r   r   rÆ   r    rq   r   rp   r=  Útuple)r&   rH  r&  rØ   rw   rI   rW   s   ```   @r   rA  zUserbotReader.read  sÓ   ûè ø€ à�k‰k‹m×ÐØ—‘—‘ˆð	jÙÜ'Õ(`Óa×a‘ä'Õ([Ó\×\�ô $.¨c´D¼%°=Ô#A™CÈÀuÐLÓ_ÑL�aÐQRÑQ^’ÐLÑ_Ð_ð 	øð bøà\ùÜ�‰ò 	jÜ Ð#AÄ$ÀqÃ'×BRÑBRÐASÐSUÐ!VÔX\Ð]^ÓX_×XhÑXhÓiÐiûð	jüâ_ùsh   †C1šB›C1¶B ÁBÁB Á$BÁ%B Á)C1ÂC,ÂC,ÂC1ÂB ÂB ÂC)Â.6C$Ã$C)Ã)C1r‘   )Né   )rH  zOptional[int]r&  Úintr0   r=  )
r   r   r   r   r  r  r¿   r½   rA  r3   r4   s   @r   r�   r�   õ  s+   ø„ ñpð €DØ€Dóô
÷`ð `r   r�   uG   It needs the â€œEdit messages of othersâ€� admin right in that channel.z>The post may be gone, or Telegram does not let anyone edit it.zSThat post was sent through another bot's inline mode, so only that bot can edit it.z/Telegram no longer allows editing that message.)r  ÚMessageAuthorRequiredErrorÚMessageIdInvalidErrorÚInlineBotRequiredErrorÚMessageEditTimeExpiredErrorc                  óp   ‡ — e Zd ZdZdZdZdZdZdˆ fd„Zdˆ fd„Z	dddd	œdd
„Z
dd„Zdd„Zd„ Zdd„Zˆ xZS )r†   a  Edits posts in ONE channel as the userbot. Call close() when done, whatever happened.

    The userbot is a user account: it can't set inline buttons, so its edit carries none and Telegram may drop the ones
    the post had. The caller puts them back afterwards.Úedit_messageszEdit messages of otherszedit other people's postsÚeditc                óB   •— t         ‰| �  ||«       g | _        d | _        y r"   )r#   r$   Ú_handedÚ
dm_problem)r&   r    rƒ   r(   s      €r   r$   zUserbotEditor.__init__-  s    ø€ Ü‰Ñ˜˜RÔ ØˆŒØ)-ˆ�r   c              ƒ  ó\  •K  — | j                   g c}| _         |D ]5  \  }}	 | j                  j                  j                  ||g«      ƒ d{  –—†  Œ7 t        ‰| �)  «       ƒ d{  –—†  y7 Œ# t        $ r3}t
        j                  dt        |«      j                  «       Y d}~Œˆd}~ww xY w7 ŒF­w)zcClean up the channel (rights, link) and the files the bot put into the userbot's chat with the bot.Nz=could not delete the helper message in the userbot's chat: %s)rW  r    rU   r�   ro   rr   ru   rq   r   r#   ry   )r&   Úhandedr¡   rH  rw   r(   s        €r   ry   zUserbotEditor.close2  s™   øè ø€ à#Ÿ|™|¨RÐˆ�”Û &ÑˆD�*ðlØ—g‘g—k‘k×1Ñ1°$¸¸ÓE×EÑEð !'ô
 ‰g‰m‹o×Ñð FùÜò lÜ—‘ÐXÔZ^Ð_`ÓZa×ZjÑZj×kÑkûðlúàúsL   ƒB,¡*A+ÁA)ÁA+ÁB,Á#B*Á$B,Á)A+Á+	B'Á4)B"ÂB,Â"B'Â'B,N)ÚmediaÚpreviewÚrichc          	   ƒ  ó>  ‡ ‡‡‡‡‡‡‡	K  — ‰ j                  «       ƒ d{  –—†  ‰ j                  j                  Š		 t        ‰t        «      r‰ j                  ‰«      ƒ d{  –—† Š‰�t        ˆ	ˆˆˆ fd„«      ƒ d{  –—†  yt        ˆ	ˆˆˆˆˆ ˆfd„«      ƒ d{  –—†  y7 Œ}7 Œ?7 Œ&7 Œ# t        j                  $ r Y yt        j                  $ rO}t        |«      j                  }t        d|› dt        j                  |«      xs t        |«      dd › �|«      ‚d}~ww xY w­w)zåMake the post show `text` (with `entities`) and, when given, `media`: what media_by_reference() returned (the
        file does not move at all) or a MediaFile (uploaded again). `rich` (a richmsg.Rich) makes it that rich message.Nc                 ó&   •— t        ‰ˆ ˆˆfd„«      S )Nc                ó6   •— t        ‰‰j                  ‰| ¬«      S )N)Úrich_message©r   r¡   )ÚinprW   rH  r&   s    €€€r   rã   z6UserbotEditor.edit.<locals>.<lambda>.<locals>.<lambda>F  s   ø€ ¼ÀÈÏÉÐT^ÐmpÕ8qr   r   )rW   rH  r]  r&   s   €€€€r   rã   z$UserbotEditor.edit.<locals>.<lambda>F  s   ø€ œI dÕ,qÔrr   c            	     ó<   •— t        ‰ ‰j                  ‰‰‰‰‰¬«      S )N)r'   Úentitiesr\  r[  rb  )rW   re  r[  rH  r\  r&   r'   s   €€€€€€€r   rã   z$UserbotEditor.edit.<locals>.<lambda>J  s   ø€ œHØ §	¡	¨:¸DÈ8Ð]dÐlqõr   z&Telegram refused the userbot as well (r  ri   )r­   r    rW   rp   r   Ú_uploadr   r   ÚMessageNotModifiedErrorrÆ   rq   r   r    Ú_EDIT_HINTSr  r.   )
r&   rH  r'   re  r[  r\  r]  rw   r%   rW   s
   ```````  @r   rU  zUserbotEditor.edit<  s
  ÿè ø€ ð �k‰k‹m×ÐØ—‘—‘ˆð	Ü˜%¤Ô+Ø"Ÿl™l¨5Ó1×1�ØÐÜ!Þró÷ ñ ô "÷ñ ó÷ ñ ð 	øð 2øðøðùô
 ×-Ñ-ò 	ÙÜ�‰ò 	Ü˜“7×#Ñ#ˆDÜ Ø8¸¸¸cÄ+Ç/Á/ÐRVÓBWÒBgÔ[^Ð_`Ó[aÐbfÐcfÐ[gÐAhÐiÐkoóð ûð	üs�   ŠDžBŸDº$B$ ÁBÁB$ Á9B Á:B$ Á>DÁ?B$ ÂB"ÂB$ ÂDÂB$ Â B$ Â"B$ Â$DÂ7DÂ9DÃA
DÄDÄDc              ƒ  ót  K  — | j                  «       ƒ d{  –—†  d| _        	 | j                  |«      ƒ d{  –—† S 7 Œ%7 Œ# t        j                  t
        t        t        f$ rR}dt        |«      j                  › �| _        t        j                  dt        |«      j                  d¬«       Y d}~yd}~ww xY w­w)a   The new photo / file as the userbot's own account holds it, ready to edit the post with - or None.

        The bot sends it to the userbot's chat with the bot (Telegram reuses the file it already has, nothing is
        downloaded or uploaded) and the userbot picks the message up there. The access data of a file belongs to one
        account, so the bot's own reference would be useless to the userbot: this is how the userbot gets its own.
        None when it does not work; `dm_problem` says why, and the caller uploads the file instead.Nzit failed with z*could not hand the file to the userbot: %sT)Úexc_info)r­   rX  Ú
_hand_overr   rÆ   rÇ   rÈ   ÚOSErrorrq   r   rr   ru   )r&   Úrefrw   s      r   Úmedia_by_referencez UserbotEditor.media_by_referenceW  s–   è ø€ ð �k‰k‹m×ÐØˆŒð	ØŸ™¨Ó-×-Ð-ð 	øð .ùÜ—‘¤¬Y¼Ð@ò 	Ø /´°Q³×0@Ñ0@Ð/AÐBˆDŒOÜ�H‰HÐAÄ4ÈÃ7×CSÑCSÐ^bˆHÔcÜûð	üsE   ‚B8–<—B8£A  ·>¸A  »B8¾A  Á #B5Á#AB0Â+B8Â0B5Â5B8c           	   ƒ  ó¾  ‡
‡‡‡K  — | j                   j                  | j                   j                  cŠ
Št        t	        |«      «      }|€t        d«      ‚	 | j                  «       ƒ d {  –—† }	 | j                  «       ƒ d {  –—† Št        j                  |j                  |j                  «      Š	 t        |ˆ
ˆfd„«      ƒ d {  –—† }| j*                  j-                  ‰|j.                  f«       t1        t2        «      D ]€  }|r!t5        j6                  t8        «      ƒ d {  –—†  	 t;        ˆˆfd„«      ƒ d {  –—† }|D ]>  }	t        t=        |	d	d «      «      |k(  sŒt?        j@                  |	jB                  «      c c S  Œ‚ d
| _        y 7 �Œ"# t        $ r
 d| _        Y y w xY w7 �Œ"# t        $ r}t        |«      | _        Y d }~y d }~ww xY w7 �Œ# t         j"                  $ r4}t%        |«      j&                  }|t(        v rdnd|› d�| _        Y d }~y d }~ww xY w7 Œø7 Œâ# t         j"                  $ r)}dt%        |«      j&                  › d�| _        Y d }~ y d }~ww xY w­w)Nznot a photo or a filez'the bot cannot find the userbot accountc                ó&   •‡ — t        ˆˆ ˆfd„«      S )Nc                 ó,   •— ‰ j                  ‰‰d¬«      S )NT)Úsilent)Ú	send_file)rU   r[  r¡   s   €€€r   rã   z<UserbotEditor._hand_over.<locals>.<lambda>.<locals>.<lambda>x  s   ø€ È3Ï=É=ÐY]Ð_dÐmqÈ=ÔKrr   )r   )r[  rU   r¡   s   `€€r   rã   z*UserbotEditor._hand_over.<locals>.<lambda>x  s   ù€ ´{ÕCrÔ7sr   zZthe userbot account has not started the bot yet (send /start to the bot from that account)z=Telegram would not let the bot send the file to the userbot (r|   c                 ó*   •— ‰j                  ‰ d¬«      S )NrA   rJ  )rG  )Úbot_peerrW   s   €€r   rã   z*UserbotEditor._hand_over.<locals>.<lambda>†  s   ø€ °×1DÑ1DÀXÐUWÐ1DÔ1Xr   z2the userbot could not read its chat with the bot (r[  z;the file did not show up in the userbot's chat with the bot)"r    rU   rW   r   r   rÇ   r  r   rX  Ú_userbot_view_of_botr.   r   r*  r±   r)  r   r   rÆ   rq   r   Ú_NOT_STARTEDrW  r¸   rv   r  Ú	_DM_POLLSrZ   r  Ú_DM_WAITr   ra   r	   Úget_input_mediar[  )r&   rm  Úwantr   rw   Úsentr%   r?  ÚmsgsrI   rU   ru  rW   r¡   s             @@@@r   rk  zUserbotEditor._hand_overg  s  ûè ø€ Ø—g‘g—k‘k 4§7¡7§>¡>ˆˆˆVÜœ¨Ó,Ó-ˆØˆ<ÜÐ4Ó5Ð5ð	Ø×2Ñ2Ó4×4ˆDð	Ø!×6Ñ6Ó8×8ˆHô ×"Ñ" 4§<¡<°×1AÑ1AÓBˆð		Ü# CÔ)sÓt×tˆDð 	�‰×Ñ˜T 4§7¡7˜OÔ,Ü”yÖ!ˆAÙÜ—m‘m¤HÓ-×-Ð-ðÜ(Ô)XÓY×Y�ó �ÜœW Q¨°Ó6Ó7¸4Ó?Ü ×0Ñ0°·±Ó9Ô9ñ ð "ð XˆŒØðC 5úÜò 	ØGˆDŒOÙð	úð 9úÜò 	Ü! !›fˆDŒOÜûð	úð
 uúÜ�‰ò 	Ü˜“7×#Ñ#ˆDð œ<Ñ'ñ màTÐUYÐTZÐZ[Ð\ð ŒOô
 ûð	úð .øàYùÜ—?‘?ò Ø$VÔW[Ð\]ÓW^×WgÑWgÐVhÐhiÐ"j�”Ýûðüsý   †AIÁF Á)FÁ*F Á/F' ÂF$ÂF' Â*IÂ2G ÃGÃG Ã
AIÄ!HÄ"IÄ'HÄ9HÄ:HÄ>IÅ.IÆF ÆF!ÆIÆ F!Æ!IÆ$F' Æ'	G
Æ0GÇ IÇG
Ç
IÇG ÇHÇ#*HÈIÈHÈIÈHÈIÈ1IÉIÉIÉIc              ƒ  óJ  K  — | j                   j                  j                  «       ƒ d{  –—† }t        |dd«      }|st	        d«      ‚	 | j                   j
                  j                  |«      ƒ d{  –—† S 7 ŒL7 Œ# t        j                  t        f$ r t	        d«      ‚w xY w­w)zJThe bot as the userbot account addresses it (found by the bot's username).Nrj   zDthe bot has no username, so the userbot cannot find its chat with itz2the userbot could not find the bot by its username)
r    rU   rn   ra   r   rW   rÁ   r   rÆ   rÇ   )r&   rX   r%   s      r   rv  z"UserbotEditor._userbot_view_of_bot�  sŽ   è ø€ à—7‘7—;‘;×%Ñ%Ó'×'ˆÜ�r˜: tÓ,ˆÙÜÐeÓfÐfð	UØŸ™Ÿ™×8Ñ8¸Ó>×>Ð>ð (øð
 ?ùÜ—‘¤Ð,ò 	UÜÐSÓTÐTð	Uüs9   ‚(B#ªA7«B#Á
(A; Á2A9Á3A; Á6B#Á9A; Á;%B Â B#c              ƒ  óF  ‡‡K  — | j                   j                  Št        ˆˆfd„«      ƒ d{  –—† }‰j                  dk(  rt	        j
                  |¬«      S d}‰j                  r	 t        ˆˆfd„«      ƒ d{  –—† }t	        j                  |‰j                  xs dt!        ‰j"                  «      |‰j                  dk(  rd	¬
«      S d¬
«      S 7 Œ 7 ŒW# t        j                  t        f$ r3}t        j                  dt        |«      j                  «       Y d}~Œœd}~ww xY w­w)zSUpload a downloaded photo / file (with its thumbnail) as the new media of the post.c                 óR   •— ‰ j                  ‰j                  ‰j                  ¬«      S )N)Ú	file_name)Úupload_fileÚpathr%   ©rW   Úfs   €€r   rã   z'UserbotEditor._upload.<locals>.<lambda>Ÿ  s    ø€ ¨6×+=Ñ+=¸a¿f¹fÐPQ×PVÑPVÐ+=Ô+Wr   NÚphoto)Úfilec                 ó:   •— ‰ j                  ‰j                  «      S r"   )r‚  Ú
thumb_pathr„  s   €€r   rã   z'UserbotEditor._upload.<locals>.<lambda>¥  s   ø€ °&×2DÑ2DÀQÇ\Á\Ô2Rr   z"could not upload the thumbnail: %szapplication/octet-streamÚdocumentT)r‡  Ú	mime_typeÚ
attributesÚthumbÚ
force_file)r    rW   r   Úkindr   ÚInputMediaUploadedPhotor‰  r   rÆ   rl  rr   ru   rq   r   ÚInputMediaUploadedDocumentr‹  r=  rŒ  )r&   r…  Úhandler�  rw   rW   s    `   @r   rf  zUserbotEditor._uploadœ  só   ùè ø€ à—‘—‘ˆÜ"Ô#WÓX×XˆØ�6‰6�WÒÜ×0Ñ0°fÔ=Ð=ØˆØ�<Š<ðQÜ)Ô*RÓS×S�ô ×/Ñ/ØØ—k‘kÒ?Ð%?Ü˜AŸL™LÓ)ØØ Ÿv™v¨Ò3�tô
ð 	
ð
 :>ô
ð 	
ð Yøð TùÜ—O‘O¤WÐ-ò QÜ—‘Ð=¼tÀA»w×?OÑ?O×PÑPûðQüsL   „)D!­C®7D!Á&C Á8CÁ9C Á=AD!ÃC ÃDÃ+)DÄD!ÄDÄD!r5  r‘   )rH  rN  r'   r.   re  r=  r0   r’   )rm  r.   )r…  r   )r   r   r   r   r	  rÞ   r  r  r$   ry   rU  rn  rk  rv  rf  r3   r4   s   @r   r†   r†   "  sP   ø„ ñ;ð
 €EØ*€JØ&€DØ€Dõ.õ
ð OSÐ\`Ðgkõ ó6ó 'òR	U÷
r   r†   F)Ú	bot_errorÚfallback_errorÚtriedc               óR  — g }|r|j                  d|› d�«       |r|j                  d|› �«       nc|r|j                  d«       nO| �t        | dd«      s|j                  d«       n.| j                  s"|j                  d| j                  xs d	› �«       d
j	                  |«      xs dS )z?Why some messages could not be deleted, in words for the owner.zTelegram refused the bot (rK  zThe userbot could not help: z9The userbot tried as well, but the posts are still there.rb   Fz<Posts older than 48 hours may need a userbot - see /userbot.rª   r}   Ú uc   Telegram did not delete them (the bot's â€œDelete messagesâ€� right, or an age limit on old posts).)r¸   ra   rd   rY   Újoin)r    r“  r”  r•  Úpartss        r   Údelete_problem_textrš  ±  s¨   € à€EÙØ�‰Ð1°)°¸BÐ?Ô@ÙØ�‰Ð3°NÐ3CÐDÕEÙ	Ø�‰ÐPÕQØ	ˆœ7 2 y°%Ô8Ø�‰ÐSÕTØ�XŠXØ�‰Ð5°b·m±mÒ6WÐGWÐ5XÐYÔZØ�8‰8�E‹?ò  Dð  Dð  Dr   )r”  c               óp   — |rd|› �S | �t        | dd«      sy| j                  sd| j                  xs d› �S y)zmWhat to add when Telegram refused the bot's edit and the userbot could not take over (empty: nothing to add).z$The userbot could not do it either: rb   Fz5A userbot can edit such posts for you - see /userbot.rª   r}   r-   )ra   rd   rY   )r    r”  s     r   Úedit_problem_textrœ  Á  sH   € áØ5°nÐ5EÐFÐFØ	€zœ  Y°Ô6ØFØ�8Š8Ø/°·±Ò0QÐAQÐ/RÐSÐSØr   )rH   r.   r0   r6  )r0   r   )r0   Údict)r“  r6  r”  r6  r•  r1   r0   r.   )r”  r6  r0   r.   )3r   Ú
__future__r   rZ   rÏ   ÚloggingÚreÚtypingr   Útelethonr   r   r   r   r	   Útelethon.sessionsr
   Úrichmsgr   Útgutilr   r   r   r   r   r   r   r   Ú	getLoggerr   rr   ÚcompilerE   ro   r   r    r*   rw  rx  ry  rJ   rQ   rS   rš   rœ   r�   r�   rh  r†   rš  rœ  r   r   r   Ú<module>r¨     s  ðñõ, #ã Û Û Û 	Ý ç DÕ DÝ +å ß p× pÓ pà€g×Ñ˜Ó!€àˆR�Z‰Zð.ó€
ôF�9ô Fô
+�\ô 
+ò€ò
€ð €	Ø€ó5ó
o÷j?ñ j?óZa÷`
ñ `
ôF	
�[ô 
ô!`�Kô !`ðJ hØ"kØ]ØsØ#Tñ€ôL
�Kô L
ð^ ;?Ð`dÐtyõ Dð  >Bö r   