Ë
    £| ky&  ã                  óÄ   — d Z ddlm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mZmZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ  ej4                  e«      ZdZdd„Zy)zz/start /help /cancel /export /userbot, plus the routers for free text and inline-button presses (/import is in backup.py).é    )ÚannotationsN)Úeventsé   )ÚCtxÚ	UserErrorÚcmdÚclear_stateÚdiscard_tempÚguardÚsayÚshow)ÚPAGE_CB)Ú	RICH_HELPÚRICH_HELP_HTML)ÚCHEAT_SHEET)Úescu  <b>Channel Post Bot</b>

<b>Posts</b>
/new - create a post (text or media, with buttons)
/posts - your posts and drafts: edit text, caption, media and buttons, or delete them - a published post is deleted in the channel and in My posts (a button post also has â€œMass replace linksâ€�)
/autopost 114 20 - post â€œEpisodes 01 to 20â€�, â€œ21 to 40â€� ... up to 114, each with a download button, then fill in the links
/formatting - bold, italic, links, quotes ...: Telegram's own formatting is kept, or type HTML / Markdown instead
/rich - rich messages: headings, lists, tables, expandable sections, math, maps, pictures, buttons ... typed as HTML / Markdown, or sent as a Telegram rich message
/cancel - stop what you're doing

<b>Channels</b>
/addchannel - register a channel (the bot must be an admin there)
/channels - list your channels, or take one out (the bot asks whether its saved posts in My posts are deleted too)

<b>Owner tools</b>
/broadcast [time] - send one message to all channels; with a time (50m, 1h, 5d) it is deleted again from all channels and My posts afterwards
/replace <post link> - reply to a new media message to replace that post's media and caption (where Telegram refuses the bot, the userbot does it)
/autopublish - the message text and the button name of the posts the bot makes by itself when the autorename bot reports an episode in the communication chat
/swap @old @new - swap the username inside t.me links (button links and hyperlinks) in all channels
/handleswap @old @new - change the username @old written in posts (text, captions, button names) to @new in all channels; links stay
/undo - undo the last /swap or /handleswap
/repost @old @new - copy every post of a channel in order (links swapped, replies, pins and links between posts kept), then delete the old posts
/shift @source @destination [from_id] [to_id] [-c] [-all] [--no-media] - copy posts from one channel into another (buttons kept, listed in My posts); a channel Telegram restricts (copyright strike) is copied from My posts; -c: links to the source's posts in all your channels are changed to the copies; -all: the destination also gets the source's name, description and photo; --no-media: no files, only the text and buttons
/sync - compare every channel with My posts right now (posts deleted, edited or added by others, the older posts from before the bot was added; service messages are deleted)
/userbot - status of the helper account that, by itself and without asking, deletes and edits the posts the bot is not allowed to
/export - download a JSON backup of your channels and posts
/import - put a backup made by /export back (only adds: nothing that exists is changed or deleted)
/admins - list admins (owner)
/addadmin 123456789 - add an admin (owner)
/removeadmin 123456789 - remove an admin (owner)c                óH  ‡ ‡— ‰ j                   ‰ j                  c}Š|j                  t        d«      «      t	        ‰ d¬«      ˆ fd„«       «       }|j                  t        d«      «      t	        ‰ d¬«      ˆ ˆfd„«       «       }|j                  t        d«      «      t	        ‰ d¬«      ˆ ˆfd„«       «       }|j                  t        d	«      «      |j                  t        d
«      «      t	        ‰ «      d„ «       «       «       }|j                  t        d«      «      t	        ‰ «      d„ «       «       }|j                  t        d«      «      t	        ‰ «      d„ «       «       }|j                  t        d«      «      t	        ‰ «      ˆ fd„«       «       }|j                  t        d«      «      t	        ‰ d¬«      ˆfd„«       «       }	|j                  t        d«      «      t	        ‰ d¬«      ˆ fd„«       «       }
|j                  t        j                  dd„ ¬«      «      t	        ‰ «      ˆ fd„«       «       }|j                  t        j                  «       «      t	        ‰ «      ˆ fd„«       «       }d„ }ˆ fd„}ˆ fd„}|‰ j                  d<   |‰ j                  d<   |‰ j                  t        <   y )NÚadminsT)Úownerc           
   “  óì   •K  — t        ‰j                  t        ‰j                  j                  «      z  «      }t	        | d|rdj                  t        t        |«      «      ndz   «      ƒ d {  –—†  y 7 Œ­w)NzAdmins: z, Únone)Úsortedr   ÚsetÚcfgr   ÚjoinÚmapÚstr)ÚeventÚidsÚctxs     €ú/app/app/handlers/basic.pyÚh_adminszregister.<locals>.h_admins5   sQ   øè ø€ ô �S—Z‘Z¤# c§g¡g§n¡nÓ"5Ñ5Ó6ˆÜ�%˜Á3 t§y¡y´´S¸#³Ô'?ÈFÑSÓT×TÒTús   ƒA)A4Á,A2Á-A4Úaddadminc              “  ó   •K  — | j                   xs dj                  «       }t        |«      dk7  s$|d   j                  «       rt	        |d   «      dk  rt        d«      ‚t	        |d   «      }‰j                  j                  |«      rt        d«      ‚‰j                  |«      ƒ d {  –—† st        | |› d�«      ƒ d {  –—†  y ‰j                  j                  |«       t        | d|› d	�«      ƒ d {  –—†  y 7 ŒP7 Œ;7 Œ	­w)
NÚ r   é   r   z5Use /addadmin followed by a numeric Telegram user ID.zThat user is already an owner.z is already an admin.zAdded ú as an admin.)Úraw_textÚsplitÚlenÚisdigitÚintr   r   Úis_ownerÚ	add_adminr   r   Úadd©r   ÚpartsÚuidr    Údbs      €€r!   Ú
h_addadminzregister.<locals>.h_addadmin;   så   øè ø€ ð —‘Ò% 2×,Ñ,Ó.ˆÜˆu‹:˜Š? %¨¡(×"2Ñ"2Ô"4¼¸EÀ!¹H»ÈÒ8JÜÐSÓTÐTÜ�%˜‘(‹mˆØ�7‰7×Ñ˜CÔ ÜÐ<Ó=Ð=Ø—\‘\ #Ó&×&Ð&Ü�e ˜uÐ$9Ð:Ó;×;Ð;ØØ�
‰
�‰�sÔÜ�%˜6 #  mÐ4Ó5×5Ñ5ð	 'øØ;øð 	6ús6   ƒB$C>Â'C8Â(C>Â>C:Â?3C>Ã2C<Ã3C>Ã:C>Ã<C>Úremoveadminc              “  ó´  •K  — | j                   xs dj                  «       }t        |«      dk7  s$|d   j                  «       rt	        |d   «      dk  rt        d«      ‚t	        |d   «      }‰j                  |«      ƒ d {  –—† st        | |› d�«      ƒ d {  –—†  y ‰j                  j                  |«       t        | d|› d�«      ƒ d {  –—†  y 7 ŒP7 Œ;7 Œ	­w)	Nr%   r   r&   r   z8Use /removeadmin followed by a numeric Telegram user ID.z is not an admin.zRemoved r'   )
r(   r)   r*   r+   r,   r   Úremove_adminr   r   Údiscardr0   s      €€r!   Úh_removeadminzregister.<locals>.h_removeadminJ   sÉ   øè ø€ ð —‘Ò% 2×,Ñ,Ó.ˆÜˆu‹:˜Š? %¨¡(×"2Ñ"2Ô"4¼¸EÀ!¹H»ÈÒ8JÜÐVÓWÐWÜ�%˜‘(‹mˆØ—_‘_ SÓ)×)Ð)Ü�e ˜uÐ$5Ð6Ó7×7Ð7ØØ�
‰
×Ñ˜3ÔÜ�%˜8 C 5¨Ð6Ó7×7Ñ7ð	 *øØ7øð 	8ús6   ƒA>CÂCÂCÂCÂ3CÃCÃCÃCÃCÚstartÚhelpc              “  ó@   K  — t        | t        «      ƒ d {  –—†  y 7 Œ­w©N)r   ÚHELP©r   s    r!   Úh_helpzregister.<locals>.h_helpW   s   è ø€ ô �%œÓ×Òúó   ‚–—Ú
formattingc              “  ó@   K  — t        | t        «      ƒ d {  –—†  y 7 Œ­wr=   )r   r   r?   s    r!   Úh_formattingzregister.<locals>.h_formatting]   s   è ø€ ô �%œÓ%×%Ò%úrA   Úrichc              “  ót   K  — t        | t        «      ƒ d {  –—†  t        | t        «      ƒ d {  –—†  y 7 Œ7 Œ­wr=   )r   r   r   r?   s    r!   Úh_richzregister.<locals>.h_richb   s1   è ø€ ô �%œÓ#×#Ð#Ü�%œÓ(×(Ñ(ð 	$øØ(ús   ‚8–4—8®6¯8¶8Úcancelc              “  ó¦   •K  — t        ‰| j                  «      ƒ d {  –—†  t        ‰| j                  «       t        | d«      ƒ d {  –—†  y 7 Œ/7 Œ­w©Nz
Cancelled.)r
   Ú	sender_idr	   r   )r   r    s    €r!   Úh_cancelzregister.<locals>.h_cancelh   sD   øè ø€ ô ˜3 §¡Ó0×0Ð0Ü�C˜Ÿ™Ô)Ü�%˜Ó&×&Ñ&ð 	1øà&ús!   ƒA�Až)AÁAÁAÁAÚexportc           	   “  ó<  •K  — ‰j                  «       ƒ d {  –—† }t        j                  t        j                  |dd¬«      j                  d«      «      }d|_        | j                  dt        |d   «      › dt        |d	   «      › d
�|¬«      ƒ d {  –—†  y 7 Œ€7 Œ­w)NFr&   )Úensure_asciiÚindentúutf-8zchannel-posts-export.jsonu   ðŸ“¦ Backup: Úchannelsz channel(s), ÚpostszU post(s). To put it back (on this bot or a new one), send /import and then this file.)Úfile)	Ú
export_allÚioÚBytesIOÚjsonÚdumpsÚencodeÚnameÚrespondr*   )r   ÚdataÚbufr3   s      €r!   Úh_exportzregister.<locals>.h_exporto   sœ   øè ø€ ð —]‘]“_×$ˆÜ�j‰jœŸ™ D°uÀQÔG×NÑNÈwÓWÓXˆØ.ˆŒØ�m‰mØœC  ZÑ 0Ó1Ð2°-ÄÀDÈÁMÓ@RÐ?Sð TZð Zàð ó 
÷ 	
ñ 	
ð %øð	
ús"   ƒB—B˜A:BÂBÂBÂBÚuserbotc              “  óD  •K  — ‰j                   }|�|j                  st        | d«      ƒ d {  –—†  y |j                  s|j	                  «       ƒ d {  –—†  |j
                  rdnd}t        | dt        |j                  «       «      › d|› d�«      ƒ d {  –—†  y 7 Œl7 ŒI7 Œ	­w)Nur  ðŸ¤– <b>Userbot: not set up.</b>
The bot does its work itself first. If Telegram refuses - deleting posts older than 48 hours, or editing a post that belongs to another bot (/replace, text and caption edits) - a helper account, the userbot, takes over by itself, without asking you each time: it joins the channel through an invite link the bot makes, the bot gives it the â€œDelete messagesâ€� or â€œEdit messages of othersâ€� right if it has the â€œAdd new adminsâ€� right, and gives the rights back afterwards.

To set it up: on your own computer run <code>python make_userbot_session.py</code> with a <b>separate</b> Telegram account, and put the printed text into the bot's secrets as <code>USERBOT_SESSION</code>. Then send /start to this bot from that account (new media reaches the userbot through its chat with the bot - that is what makes a replace quick). See the README.zstays an adminz,is made admin only while it deletes or editsu   ðŸ¤– <b>Userbot:</b> z
It zÂ (USERBOT_KEEP_ADMIN).
It must have started this bot (/start from that account): new media for an edit goes through its chat with the bot, which is what makes a big file as quick as a small one.)r`   Úenabledr   ÚreadyÚconnectÚ
keep_adminr   Údescribe)r   ÚubÚkeptr    s      €r!   Ú	h_userbotzregister.<locals>.h_userbot{   s®   øè ø€ ð �[‰[ˆØˆ:˜RŸZšZÜØð	oó÷ ð ð Ø�xŠxØ—*‘*“,×ÐØ#%§=¢=ÑÐ6dˆÜØØ#¤C¨¯©«Ó$6Ð#7°u¸T¸Fð CTð Tó
÷ 	
ñ 	
ð#øð øð	
ús4   ƒ*B ­B®$B ÁBÁAB ÂBÂB ÂB ÂB c                ó^   — | j                   xr  | j                  xs dj                  d«       S )Nr%   Ú/)Ú
is_privater(   Ú
startswith)Úes    r!   Ú<lambda>zregister.<locals>.<lambda>™   s)   € ¸¿¹Ò8oÈaÏjÉjÒN^Ð\^×MjÑMjÐknÓMoÐIoÐ8oó    )ÚincomingÚfuncc              “  óJ  •K  — ‰j                   j                  | j                  «      }|st        | d«      ƒ d {  –—†  y ‰j                  j                  |d   «      }|€+t        ‰| j                  «       t        | d«      ƒ d {  –—†  y  || |«      ƒ d {  –—†  y 7 Œb7 Œ7 Œ	­w)NzQUse /new to create a post or /posts to manage your posts. /help lists everything.Úmodez5That step is no longer active. Start again with /new.)ÚstateÚgetrK   r   Útext_handlersr	   )r   ÚstÚfnr    s      €r!   Úh_free_textzregister.<locals>.h_free_text˜   s—   øè ø€ ð
 �Y‰Y�]‰]˜5Ÿ?™?Ó+ˆÙÜ�eÐpÓq×qÐqØØ×Ñ×"Ñ" 2 f¡:Ó.ˆØˆ:Ü˜˜UŸ_™_Ô-Ü�eÐTÓU×UÐUØÙ�˜‹m×Ñð røð
 Vøàús4   ƒ7B#ºB»A
B#ÂBÂB#ÂB!ÂB#ÂB#Â!B#c              “  óX  •K  — 	 | j                  «       ƒ d {  –—†  | j                  j                  dd«      }|j	                  d«      ^}}‰j
                  j                  |«      }|€| j                  d«      ƒ d {  –—†  y  || |«      ƒ d {  –—†  y 7 Œ|# t        $ r Y Œ…w xY w7 Œ(7 Œ­w)NrQ   ÚreplaceÚ:z4This button is no longer active. Use /new or /posts.)ÚanswerÚ	Exceptionr]   Údecoder)   Ú	callbacksrv   r\   )r   r]   Úprefixr1   ry   r    s        €r!   Ú
h_callbackzregister.<locals>.h_callback¨   s¤   øè ø€ ð	Ø—,‘,“.× Ð ð �z‰z× Ñ  ¨)Ó4ˆØŸ™ C›ˆˆ�Ø�]‰]×Ñ˜vÓ&ˆØˆ:Ø—-‘-Ð VÓW×WÐWØÙ�˜Ó×Ñð !ùÜò 	Ùð	úð XøàúsV   ƒB*…B ˜B™B �A B*Á=B&Á>B*ÂB(ÂB*ÂB Â	B#Â B*Â"B#Â#B*Â(B*c              “  ó@   K  — | j                  «       ƒ d {  –—†  y 7 Œ­wr=   )r~   )r   r1   s     r!   Úcb_noopzregister.<locals>.cb_noop·   s   è ø€ Ø�l‰l‹n×ÒúrA   c              “  ó–   •K  — | j                   }t        ‰|«      ƒ d {  –—†  t        ‰|«       t        | d«      ƒ d {  –—†  y 7 Œ%7 Œ­wrJ   )rK   r
   r	   r   )r   r1   r2   r    s      €r!   Ú	cb_cancelzregister.<locals>.cb_cancelº   sC   øè ø€ Ø�o‰oˆÜ˜3 Ó$×$Ð$Ü�C˜ÔÜ�5˜,Ó'×'Ñ'ð 	%øà'ús    ƒA	ŸA A	¿AÁ A	ÁA	c              “  ó¶   •K  — |d   |d   t        |d   «      }}}‰j                  j                  |«      }|€t        d«      ‚ || ||«      ƒ d{  –—†  y7 Œ­w)uM   â—€ â–¶ under a channel chooser: draw the same chooser again on another page.r   r&   r   Nz7This list is no longer active. Start the command again.)r,   Úpickersrv   r   )r   r1   ÚkindÚargÚpageÚdrawr    s         €r!   Úcb_channel_pagez!register.<locals>.cb_channel_pageÀ   sY   øè ø€ à ™( E¨!¡H¬c°%¸±(«m�4ˆcˆØ�{‰{�‰˜tÓ$ˆØˆ<ÜÐUÓVÐVÙ�5˜#˜tÓ$×$Ò$ús   ƒAAÁAÁAÚnoopÚcx)
Úclientr3   Úonr   r   r   Ú
NewMessageÚCallbackQueryr�   r   )r    r‘   r"   r4   r9   r@   rD   rG   rL   r_   ri   rz   rƒ   r…   r‡   rŽ   r3   s   `               @r!   Úregisterr•   2   sz  ù€ Ø—‘˜SŸV™V€J€FˆBà‡Y�YŒs�8‹}ÓÜ
ˆ3�dÔóUó ó ðUð ‡Y�YŒs�:‹ÓÜ
ˆ3�dÔô6ó ó  ð6ð ‡Y�YŒs�=Ó!Ó"Ü
ˆ3�dÔô	8ó ó #ð	8ð ‡Y�YŒs�7‹|ÓØ‡Y�YŒs�6‹{ÓÜ
ˆ3ƒZñó ó ó ðð ‡Y�YŒs�<Ó Ó!Ü
ˆ3ƒZñ&ó ó "ð&ð ‡Y�YŒs�6‹{ÓÜ
ˆ3ƒZñ)ó ó ð)ð ‡Y�YŒs�8‹}ÓÜ
ˆ3ƒZó'ó ó ð'ð
 ‡Y�YŒs�8‹}ÓÜ
ˆ3�dÔó
ó ó ð
ð ‡Y�YŒs�9‹~ÓÜ
ˆ3�dÔó
ó ó ð
ð6 ‡Y�YÜ×Ñ 4Ñ.oÔpóô ˆ3ƒZó
ó óð
ð ‡Y�YŒv×#Ñ#Ó%Ó&Ü
ˆ3ƒZóó ó 'ðòô(ô%ð $€C‡M�M�&ÑØ#€C‡M�M�$ÑØ,€C‡M�M”'Òrp   )r    r   ÚreturnÚNone)Ú__doc__Ú
__future__r   rV   rX   ÚloggingÚtelethonr   Úcommonr   r   r   r	   r
   r   r   r   Úpickerr   Úrichmsgr   r   Úrichtextr   Útgutilr   Ú	getLoggerÚ__name__Úlogr>   r•   © rp   r!   Ú<module>r¥      sS   ðñ AÝ "ã 	Û Û å ç U× UÓ UÝ ß /Ý "Ý à€g×Ñ˜Ó!€ðDð ô@X-rp   