Ë
    £| k   ã                  óÜ   — d Z ddlmZ ddlZddlZddl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mZmZmZ dd	lmZmZ dd
lmZ  ej<                  e«      Z dZ!dZ"dd„Z#dd„Z$y)z-Channel registration: /addchannel, /channels.é    )ÚannotationsN)ÚButtonÚeventsÚtypesé   )Úpicker)Úparse_channel_ref)ÚCtxÚ	UserErrorÚclear_stateÚcmdÚguardÚon_cbÚon_textÚrequire_ownerÚsayÚ	set_stateÚshow)ÚescÚ
get_rights)ÚCANCELu‹  ðŸ“¢ <b>Add a channel</b>
1. Make the bot an admin of the channel with the rights <i>Post messages</i>, <i>Edit messages of others</i> and <i>Delete messages</i>.
2. Then <b>forward any post from that channel to me</b>, or send its @username (public channels).

Tip for private channels: if forwarding doesn't work, post something in the channel while the bot is running, then forward that post.zÅI can see the channel, but Telegram hasn't given me its access details yet. Post something in the channel while the bot is running (or send the @username if it's public), then forward a post again.c                óž   — | €y| j                   syd„ }d || j                  «      › d || j                  «      › d || j                  «      › �S )Nzrights unknownu   âš ï¸� not an admin therec                ó   — | rdS dS )Nu   âœ…u   â�Œ© )Úvs    ú/app/app/handlers/channels.pyÚ<lambda>zrights_line.<locals>.<lambda>$   s   € ™A�5Ð( 5Ð(ó    zpost u    Â· edit others' posts u    Â· delete )ÚadminÚpostÚeditÚdelete)ÚrÚoks     r   Úrights_liner%      sN   € Ø€yØØ�7Š7Ø*Ù	(€BØ‘2�a—f‘f“:�,Ð5±b¸¿¹³j°\ÀÉRÐPQ×PXÑPXË\ÈNÐ[Ð[r   c                ó  ‡ ‡‡‡	— ‰ j                   ‰ j                  cŠŠ‰j                  t        j                  d„ ¬«      «      ˆ ˆfd„«       }‰j                  t        d«      «      t        ‰ d¬«      ˆ fd„«       «       }t        ‰ d«      ˆˆ ˆfd	„«       }ddˆ ˆfd
„Š	‰	‰ j                  d<   ‰j                  t        d«      «      t        ‰ «      ˆ	fd„«       «       }t        ‰ d«      ˆ ˆˆ	fd„«       }t        ‰ d«      ˆ ˆfd„«       }y )Nc                óJ   — t        | j                  «      xr | j                   S ©N)ÚboolÚ
is_channelÚis_group)Úes    r   r   zregister.<locals>.<lambda>,   s   € ´°Q·\±\Ó0BÒ0UÈ1Ï:É:À~Ð0Ur   )Úfuncc              “  ó  •K  — 	 | j                  «       ƒ d {  –—† }t        |t        j                  «      r¤|j                  r—t        |dd«      s‰‰j                  j                  |j                  «      }|‰j                  |j                  <   |�|j                  |j                  k7  r/‰j                  |j                  |j                  «      ƒ d {  –—†  y y y y y 7 ŒÃ7 Œ# t        $ r t        j                  dd¬«       Y y w xY w­w)NÚminFzchannel watcher failedT)Úexc_info)Úget_chatÚ
isinstancer   ÚChannelÚaccess_hashÚgetattrÚseen_channelsÚgetÚidÚtouch_channel_hashÚ	ExceptionÚlogÚdebug)ÚeventÚchatÚprevÚctxÚdbs      €€r   Úwatch_channelsz register.<locals>.watch_channels,   sã   øè ø€ ð	?ØŸ™Ó)×)ˆDÜ˜$¤§¡Ô.°4×3CÒ3CÌGÐTXÐZ_ÐafÔLgØ×(Ñ(×,Ñ,¨T¯W©WÓ5�Ø-1�×!Ñ! $§'¡'Ñ*Ø�< 4×#3Ñ#3°t×7GÑ7GÒ#GØ×/Ñ/°·±¸×9IÑ9IÓJ×JÑJð $Hð MhÐ3CÐ.ð *øð
 KùÜò 	?Ü�I‰IÐ.¸ˆIÖ>ð	?üsK   ƒD…C  ˜C™B9C  ÃCÃC  ÃDÃC  ÃC  Ã  DÄ DÄDÄDÚ
addchannelT)Úownerc              “  óz   •K  — t        ‰| j                  d«       t        | t        t        «      ƒ d {  –—†  y 7 Œ­w)NÚadd_channel)r   Ú	sender_idr   ÚADD_HELPr   )r=   r@   s    €r   Úh_addchannelzregister.<locals>.h_addchannel8   s+   øè ø€ ô 	�#�u—‘¨Ô6Ü�%œ¤6Ó*×*Ò*ús   ƒ0;³9´;rF   c           	   “  ó€  •K  — | j                   }d }|j                  }|�rt        t        |dd «      t        j
                  «      rM|j                  }t        |dd«      st        |dd «      s¹‰	j                  j                  |j                  |«      }n’t        |j                  xs d«      }|€t        d«      ‚t        |t        «      r‰	j                  j                  |«      }|€?	 ‰j                  t        |t        «      r|nt	        j                   |«      «      ƒ d {  –—† }t        |t        j
                  «      s't        dt%        j&                  ddt(        «      z   «      ‚|j*                  st        d	«      ‚|j,                  rt        |dd«      rt        t.        «      ‚t1        ‰|«      ƒ d {  –—† }|�|j2                  st        d
«      ‚‰
j5                  |j                  |j,                  |j6                  xs d|j8                  | j:                  «      ƒ d {  –—†  t=        ‰	| j:                  «       d}|�&|j>                  r|j@                  r|jB                  sd}tE        | dtG        |j6                  xs d«      › dtI        |«      › |› d�«      ƒ d {  –—†  y 7 �Œƒ# t"        $ r d }Y �Œ�w xY w7 �Œ7 Œš7 Œ­w)Nr>   r/   Fr4   Ú z=Forward a post from the channel to me, or send its @username.zI couldn't find that channel.

z<[^>]+>z<That is a group, not a channel. Only channels are supported.zPThe bot is not an admin of that channel yet. Add it as an admin, then try again.ux   

âš ï¸� Missing rights: give the bot <i>Post</i>, <i>Edit messages of others</i> and <i>Delete</i> so everything works.u   âœ… Added <b>z</b>
z

Create a post with /new.)%ÚmessageÚforwardr2   r5   r   r3   r>   r6   r7   r8   r	   Úraw_textr   ÚintÚ
get_entityÚstrÚPeerChannelr:   ÚreÚsubrH   Ú	broadcastr4   ÚNEEDS_ACCESSr   r   Úsave_channelÚtitleÚusernamerG   r   r    r!   r"   r   r   r%   )r=   ÚstÚmÚentÚfwdÚrefÚrightsÚwarnÚclientr@   rA   s           €€€r   Úh_add_channelzregister.<locals>.h_add_channel>   sI  øè ø€ à�M‰MˆØˆØ�i‰iˆØˆ?œz¬'°#°v¸tÓ*DÄeÇmÁmÔTØ—(‘(ˆCÜ�s˜E 5Ô)´¸¸mÈTÔ1RØ×'Ñ'×+Ñ+¨C¯F©F°CÓ8‘ä# A§J¡JÒ$4°"Ó5ˆCØˆ{ÜÐ _Ó`Ð`Ü˜#œsÔ#Ø×'Ñ'×+Ñ+¨CÓ0�Øˆ{ðØ &× 1Ñ 1¼ÀCÌÔ9M±#ÔSX×SdÑSdÐehÓSiÓ j×j�Cô ˜#œuŸ}™}Ô-ÜÐ?Ä"Ç&Á&ÈÐUWÔYaÓBbÑbÓcÐcØ�}Š}ÜÐZÓ[Ð[Ø�Š¤'¨#¨u°eÔ"<ÜœLÓ)Ð)Ü! &¨#Ó.×.ˆØÐ f§l¢lÜÐnÓoÐoØ�o‰o˜cŸf™f c§o¡o°s·y±y²ÀBÈÏÉÐV[×VeÑVeÓf×fÐfÜ�C˜Ÿ™Ô)ØˆØÐ v§{¢{°v·{²{ÀvÇ}Â}ð PˆDÜØØœC §	¡	¢¨RÓ0Ð1°¼ÀFÓ8KÐ7LÈTÈFÐRnÐoó
÷ 	
ñ 	
ð# kúÜ ò Ø“Cðúð /ùð 	gøð
	
úso   ƒC"J>Ã&9J% ÄJ"Ä J% Ä$BJ>Æ3J7Æ4A+J>ÈJ:È A<J>ÊJ<ÊJ>Ê"J% Ê%J4Ê0J>Ê3J4Ê4J>Ê:J>Ê<J>c           	   “  ó$  •K  — ‰j                  «       ƒ d{  –—† }|st        d«      ‚‰j                  j                  | j                  «      }t        j                  ||dd|rd„ nddd¬«      \  }}t        | ||xs d«      ƒ d{  –—†  y7 Œt7 Œ­w)	zFThe list of channels; the owner's number buttons choose one to remove.Nz!No channels yet. Use /addchannel.u   ðŸ“¢ <b>Your channels</b>Úchc                ó    — d| j                   › �S )Nzchd:)r8   )Úcs    r   r   z1register.<locals>.show_channels.<locals>.<lambda>p   s   €   Q§T¡T F™mr   Tz1To take a channel out of the bot, tap its number.)ÚheadÚkindÚchooseÚwith_usernameÚhint)Úlist_channelsr   ÚcfgÚis_ownerrG   r   Úrenderr   )	r=   ÚargÚpageÚchansrD   ÚtextÚkbr@   rA   s	          €€r   Úshow_channelszregister.<locals>.show_channelse   sŽ   øè ø€ à×&Ñ&Ó(×(ˆÙÜÐ?Ó@Ð@Ø—‘× Ñ  §¡Ó1ˆÜ—=‘=ØØØ,ØÙ05Ò+¸4ØØDô
‰ˆˆbô �5˜$ ¢
 dÓ+×+Ñ+ð )øð 	,ús"   ƒB—B˜A.BÂBÂBÂBrd   Úchannelsc              “  ó2   •K  —  ‰| «      ƒ d {  –—†  y 7 Œ­wr(   r   )r=   ru   s    €r   Ú
h_channelszregister.<locals>.h_channelsx   s   øè ø€ ñ ˜EÓ"×"Ò"ús   ƒ��Úchdc              “  óò  •K  — t        ‰| «       ‰j                  t        |d   «      «      ƒ d{  –—† }|�|j                  s ‰| «      ƒ d{  –—†  yt	        j
                  dt        j                  › d�«      }‰j                  |j                  «      ƒ d{  –—† }|sRt        | dt        |j                  «      › d�t	        j
                  dd|j                  › d	�«      |gg«      ƒ d{  –—†  yt        | dt        |j                  «      › d
|› d�t	        j
                  dd|j                  › d�«      t	        j
                  dd|j                  › d	�«      g|gg«      ƒ d{  –—†  y7 �Œ;7 �Œ 7 Œ×7 Œ†7 Œ­w)zeA number was tapped: ask what becomes of the channel's saved posts before it is taken out of the bot.r   Nu	   ðŸ”™ Backz:ch::0u   âž– Take <b>zR</b> out of the bot?
It has no saved posts. You can add it again with /addchannel.u   âœ… Yes, remove itzchy:z:0zG</b> out of the bot.

Do you also want to delete its saved posts - the zS post(s) My posts holds for this channel? Nothing is deleted in the channel itself.u   ðŸ—‘ Yes, delete them tooz:1u   ðŸ“¦ No, keep them)r   Úget_channelrO   Úactiver   Úinliner   ÚPAGE_CBÚcount_channel_postsr8   r   r   rX   )r=   Úpartsrd   ÚbackÚnr@   rA   ru   s        €€€r   Úcb_remove_channelz#register.<locals>.cb_remove_channel}   s‹  øè ø€ ô 	�c˜5Ô!Ø—>‘>¤# e¨A¡h£-Ó0×0ˆØˆ:˜RŸYšYÙ Ó&×&Ð&ØÜ�}‰}˜[¬V¯^©^Ð,<¸FÐ*CÓDˆØ×(Ñ(¨¯©Ó/×/ˆÙÜØØœs 2§8¡8›}˜oð  .Að  BÜ—-‘-Ð 4¸¸R¿U¹U¸GÀ2Ð6FÓGÈÐNÐOó÷ ð ð
 ÜØØœ3˜rŸx™x›=˜/ð *@Ø@A¸sð C8ð8ô —‘Ð:¸dÀ2Ç5Á5À'ÈÐ<LÓMÌvÏ}É}Ð]qÐuyÐz|×zÑzð  zAð  ACð  tDó  PEð  FØ�ðó	
÷ 		
ñ 		
ð 1ùà&ùð 0øðøð		
úsZ   ƒ-E7°E+±E7ÁE.ÁA
E7ÂE1ÂAE7Ã,E3Ã-A8E7Å%E5Å&E7Å.E7Å1E7Å3E7Å5E7Úchyc              “  óZ  •K  — t        ‰
| «       t        |d   «      }t        |«      dkD  xr |d   dk(  }‰j                  |«      ƒ d{  –—† }|�dt	        |j
                  «      › d�nd}‰j                  |d«      ƒ d{  –—†  d}‰
j                  }|�|j                  «        |r¼t        | d	|› d
�«      ƒ d{  –—†  |�C|j                  «       4 ƒd{  –—†  ‰j                  |«      ƒ d{  –—† }ddd«      ƒd{  –—†  n‰j                  |«      ƒ d{  –—† }‰
j                  j                  ‰
j                  D �ch c]  }|d   |k(  sŒ|’Œ c}«       d|› d|› d�}	nd|› d�}	t        | |	«      ƒ d{  –—†  y7 �Œ57 �Œ7 ŒÉ7 Œ°7 Œ™7 Œ‹# 1 ƒd{  –—†7  sw Y   Œ�xY w7 Œˆc c}w 7 Œ/­w)z¬chy:<channel>:1 removes the channel and deletes its saved posts, chy:<channel>:0 (or an old button without the
        second part) removes only the channel and keeps them.r   é   Ú1Nz<b>z</b>zThe channelFu    â�³ Deleting the saved posts of u   â€¦u   âœ… z# was removed from the bot, and its z‘ saved post(s) were deleted from My posts. Nothing was deleted in the channel. If you add it again with /addchannel, its posts are read in again.zl was removed from the bot. Its saved posts are kept; add it again with /addchannel and they are there again.)r   rO   Úlenr{   r   rX   Úset_channel_activeÚsyncerÚforget_cacher   Ú	exclusiveÚdelete_channel_dataÚsync_ignoreÚdifference_update)r=   r€   ÚcidÚwiperd   ÚnameÚgoneÚsyÚkrs   r@   rA   s             €€r   Úcb_remove_channel_yesz'register.<locals>.cb_remove_channel_yes™   s¤  øè ø€ ô 	�c˜5Ô!Ü�%˜‘(‹mˆÜ�5‹z˜A‰~Ò1 %¨¡(¨c¡/ˆØ—>‘> #Ó&×&ˆØ,.¨N�”S˜Ÿ™“]�O 4Ñ(ÀˆØ×#Ñ# C¨Ó/×/Ð/ØˆØ�Z‰ZˆØˆ>Ø�O‰OÔÙÜ�uÐ @ÀÀÀcÐJÓK×KÐKàˆ~ØŸ<™<Ÿ>�>Ø!#×!7Ñ!7¸Ó!<×<�D÷ *Ÿ>˜>ð  ×3Ñ3°CÓ8×8�Ø�O‰O×-Ñ-¸#¿/º/Ó.Y¹/°QÈQÈqÉTÐUXË[ªq¸/Ñ.YÔZà�t�fÐ?À¸vð Feð eñ ð
 ˜$˜ð   Lð  MˆDÜ�5˜$Ó×Ñð- 'ùà/ùð Løð *øØ<øð *øŸ>Ÿ>™>úð 9úÚ.Yð 	 úsÀ   ƒAF+Á
E?Á6F+ÂFÂ9F+Â;FÂ<F+ÃFÃF+ÃFÃ/F	Ã0FÃ4F+Ã?FÄ F+ÄF"Ä'F+ÅF$ÅF$Å&F+Å9F)Å:F+ÆF+ÆF+ÆF+Æ	FÆF+ÆFÆFÆFÆF+Æ$F+)rK   r   )rp   rQ   rq   rO   ÚreturnÚNone)
ra   rA   Úonr   Ú
NewMessager   r   r   Úpickersr   )
r@   rB   rI   rb   rx   rƒ   r–   ra   rA   ru   s
   `      @@@r   Úregisterrœ   (   s  û€ Ø—‘˜SŸV™V€J€FˆBð ‡Y�YŒv× Ñ Ñ&UÔVÓWô	?ó Xð	?ð ‡Y�YŒs�<Ó Ó!Ü
ˆ3�dÔó+ó ó "ð+ô ˆS�-Ó õ$
ó !ð$
÷L,ð" &€C‡K�K�Ñà‡Y�YŒs�:‹ÓÜ
ˆ3ƒZó#ó ó  ð#ô ˆ3�Óõ
ó ð
ô6 ˆ3�Óô ó ñ r   )r—   rQ   )r@   r
   r—   r˜   )%Ú__doc__Ú
__future__r   ÚloggingrS   Útelethonr   r   r   rK   r   Ú
channelrefr	   Úcommonr
   r   r   r   r   r   r   r   r   r   r   Útgutilr   r   Úuir   Ú	getLoggerÚ__name__r;   rH   rV   r%   rœ   r   r   r   Ú<module>r§      sb   ðÙ 3Ý "ã Û 	ç *Ñ *å Ý *ß q× q× qÑ qß $Ý à€g×Ñ˜Ó!€ð'ð 	ðjð ó\ôN r   