U
    «ëflk  ã                   @   sš   d dl Z d dlZd dl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 e dd¡Ze dd¡ZdZd	ZG d
d„ deƒZdd„ ZG dd„ dƒZdS )é    N)Úprompt)ÚInMemoryHistoryZWIT_URLzhttps://api.wit.aiÚWIT_API_VERSIONZ20200513z> z,Learn more at https://wit.ai/docs/quickstartc                   @   s   e Zd ZdS )ÚWitErrorN)Ú__name__Ú
__module__Ú__qualname__© r	   r	   ú+/tmp/pip-unpacked-wheel-2ezanbwc/wit/wit.pyr      s   r   c           
      K   s¸   t | }|  d|||¡ d| dt d dœ}| | di ¡¡ tj||f||dœ|—Ž}|jdkr„td	t	|jƒ d
 |j
 d ƒ‚| ¡ }	d|	kr¤td|	d  ƒ‚|  d|||	¡ |	S )Nz%s %s %szBearer zapplication/vnd.wit.z+json)ÚauthorizationÚacceptÚheaders)r   ÚparamséÈ   zWit responded with status: z (ú)ÚerrorzWit responded with an error: )ÚWIT_API_HOSTÚdebugr   ÚupdateÚpopÚrequestsÚrequestÚstatus_coder   ÚstrÚreasonÚjson)
ÚloggerÚaccess_tokenÚmethÚpathr   ÚkwargsÚfull_urlr   Zrspr   r	   r	   r
   Úreq   s0    
þ
ÿþýüÿr"   c                   @   s¤  e Zd ZdZi ZdRdd„ZdSdd„ZdTdd„ZdUdd	„ZdVd
d„Z	dWdd„Z
dXdd„ZdYdd„ZdZdd„Zd[dd„Zd\dd„Zd]dd„Zd^dd„Zd_dd„Zd`dd„Zdad d!„Zdbd"d#„Zdcd$d%„Zddd&d'„Zded(d)„Zdfd*d+„Zdgd,d-„Zdhd.d/„Zdid0d1„Zdjd2d3„Zdkd4d5„Zdld6d7„Zdmd8d9„Z dnd:d;„Z!dod<d=„Z"dpd>d?„Z#dqd@dA„Z$drdBdC„Z%dsdDdE„Z&dtdFdG„Z'dudHdI„Z(dvdJdK„Z)dwdLdM„Z*dxdNdO„Z+dydPdQ„Z,dS )zÚWitNc                 C   s   || _ |pt t¡| _d S )N)r   ÚloggingÚ	getLoggerr   r   )Úselfr   r   r	   r	   r
   Ú__init__5   s    zWit.__init__c                 C   sV   i }|d k	r||d< |r ||d< |r2t  |¡|d< |r>||d< t| j| jdd|ƒ}|S )NÚnÚqÚcontextÚverboseÚGETz/message)r   Údumpsr"   r   r   )r&   Úmsgr*   r(   r+   r   Úrespr	   r	   r
   Úmessage9   s    zWit.messagec              	   C   s6   i }|p
i }|rd|d< t | j| jdd|||d}|S )aE  Sends an audio file to the /speech API.
        Uses the streaming feature of requests (see `req`), so opening the file
        in binary mode is strongly recommended (see
        http://docs.python-requests.org/en/master/user/advanced/#streaming-uploads).
        Add Content-Type header as specified here: https://wit.ai/docs/http/20200513#post--speech-link

        :param audio_file: an open handler to an audio file
        :param headers: an optional dictionary with request headers
        :param verbose: for legacy versions, get extra information
        :return:
        Tr+   ÚPOSTz/speech)Údatar   ©r"   r   r   )r&   Z
audio_filer   r+   r   r/   r	   r	   r
   ÚspeechF   s    ù	z
Wit.speechc              	   C   sx   |dkri }t ƒ }ztt|dd ¡ }W n ttfk
rB   Y dS X |dkr^t|  ||¡ƒ qt||  ||¡ƒƒ qdS )a
  Runs interactive command line chat between user and bot. Runs
        indefinitely until EOF is entered to the prompt.

        handle_message -- optional function to customize your response.
        context -- optional initial context. Set to {} if omitted
        NT)ÚhistoryZmouse_support)r   r   ÚINTERACTIVE_PROMPTÚrstripÚKeyboardInterruptÚEOFErrorÚprintr0   )r&   Zhandle_messager*   r5   r0   r	   r	   r
   Úinteractivea   s      ÿzWit.interactivec                 C   s4   i }|p
i }|rd|d< t | j| jdd||d}|S )zH
        Returns names of all intents associated with your app.
        Tr+   r,   ú/intents©r   r3   ©r&   r   r+   r   r/   r	   r	   r
   Úintent_listx   s         ÿzWit.intent_listc                 C   sP   i }|p
i }|r||d< |r$d|d< |dk	r4||d< t | j| jdd||d}|S )	zT
        Returns the list of the top detected locales for the text message.
        r)   Tr+   Nr(   r,   z	/languager=   r3   )r&   r.   r(   r   r+   r   r/   r	   r	   r
   Údetect_language…   s"         ÿzWit.detect_languagec                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )zy
        Returns all available information about an intent.

        :param intent_name: name of existing intent
        Tr+   ú	/intents/r,   r=   ©ÚurllibÚparseÚ
quote_plusr"   r   r   ©r&   Úintent_namer   r+   r   Úendpointr/   r	   r	   r
   Úintent_info–   s         ÿzWit.intent_infoc                 C   s4   i }|p
i }|rd|d< t | j| jdd||d}|S )zH
        Returns list of all entities associated with your app.
        Tr+   r,   ú	/entitiesr=   r3   r>   r	   r	   r
   Úentity_list¦   s         ÿzWit.entity_listc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )zy
        Returns all available information about an entity.

        :param entity_name: name of existing entity
        Tr+   ú
/entities/r,   r=   rB   ©r&   Úentity_namer   r+   r   rH   r/   r	   r	   r
   Úentity_info³   s         ÿzWit.entity_infoc                 C   s4   i }|p
i }|rd|d< t | j| jdd||d}|S )zF
        Returns list of all traits associated with your app.
        Tr+   r,   ú/traitsr=   r3   r>   r	   r	   r
   Ú
trait_listÃ   s         ÿzWit.trait_listc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )zu
        Returns all available information about a trait.

        :param trait_name: name of existing trait
        Tr+   ú/traits/r,   r=   rB   ©r&   Ú
trait_namer   r+   r   rH   r/   r	   r	   r
   Ú
trait_infoÐ   s         ÿzWit.trait_infoc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )zv
        Delete an intent associated with your app.

        :param intent_name: name of intent to be deleted
        Tr+   rA   ÚDELETEr=   rB   rF   r	   r	   r
   Údelete_intentà   s         ÿzWit.delete_intentc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )zv
        Delete an entity associated with your app.

        :param entity_name: name of entity to be deleted
        Tr+   rL   rV   r=   rB   rM   r	   r	   r
   Údelete_entityð   s         ÿzWit.delete_entityc                 C   sT   i }|p
i }|rd|d< dt j |¡ d t j |¡ }t| j| jd|||d}|S )zÌ
        Deletes a role associated with the entity.

                :param entity_name: name of entity whose particular role is to be deleted
        :param role_name: name of role to be deleted
        Tr+   rL   ú:rV   r=   rB   )r&   rN   Z	role_namer   r+   r   rH   r/   r	   r	   r
   Údelete_role   s*    
ÿþ
ýÿ     ÿzWit.delete_rolec                 C   sT   i }|p
i }|rd|d< dt j |¡ d t j |¡ }t| j| jd|||d}|S )zØ
        Deletes a keyword associated with the entity.

                :param entity_name: name of entity whose particular keyword is to be deleted
        :param keyword_name: name of keyword to be deleted
        Tr+   rL   ú
/keywords/rV   r=   rB   )r&   rN   Úkeyword_namer   r+   r   rH   r/   r	   r	   r
   Údelete_keyword  s*    
ÿþ
ýÿ     ÿzWit.delete_keywordc           	      C   sd   i }|p
i }|rd|d< dt j |¡ d t j |¡ d t j |¡ }t| j| jd|||d}|S )zÙ
        Delete a synonym of the keyword of the entity.

                :param entity_name: name of entity whose particular keyword is to be deleted
        :param keyword_name: name of keyword to be deleted
        Tr+   rL   r[   z
/synonyms/rV   r=   rB   )	r&   rN   r\   Zsynonym_namer   r+   r   rH   r/   r	   r	   r
   Údelete_synonym,  s2    	
ÿþ
ýü
ûÿ     ÿzWit.delete_synonymc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )zt
        Delete a trait associated with your app.

        :param intent_name: name of intent to be deleted
        Tr+   rR   rV   r=   rB   rS   r	   r	   r
   Údelete_traitF  s         ÿzWit.delete_traitc                 C   sT   i }|p
i }|rd|d< dt j |¡ d t j |¡ }t| j| jd|||d}|S )zÍ
        Deletes a value associated with the trait.

                :param trait_name: name of trait whose particular value is to be deleted
        :param value_name: name of value to be deleted
        Tr+   rR   z/values/rV   r=   rB   )r&   rT   Z
value_namer   r+   r   rH   r/   r	   r	   r
   Údelete_trait_valueV  s*    
ÿþ
ýÿ     ÿzWit.delete_trait_valuec                 C   sX   i }|p
i }|dk	r||d< |r(||d< |r4||d< |r@||d< t | j| jdd|ƒ}|S )zè
        Returns a JSON array of utterances.

                :param limit: number of utterances to return
        :param offset: number of utterances to skip
        :param intents: list of intents to filter the utterances
        NÚlimitÚoffsetÚintentsr+   r,   ú/utterancesr3   )r&   ra   rb   rc   r   r+   r   r/   r	   r	   r
   Úget_utterancesl  s    
zWit.get_utterancesc              	   C   sR   i }|p
i }g }|D ]}|  d|i¡ q|r4||d< t| j| jdd|||d}|S )zw
        Delete utterances from your app.

                :param utterances: list of utterances to be deleted
        Útextr+   rV   rd   ©r   r   )Úappendr"   r   r   )r&   Z
utterancesr   r+   r   r2   Z	utterancer/   r	   r	   r
   Údelete_utterancesƒ  s"    ù	zWit.delete_utterancesc                 C   sL   i }|p
i }|dk	r||d< |r(||d< |r4||d< t | j| jdd|ƒ}|S )z 
        Returns an array of all your apps.

                :param limit: number of apps to return
        :param offset: number of utterances to skip
        Nra   rb   r+   r,   ú/appsr3   )r&   ra   rb   r   r+   r   r/   r	   r	   r
   Úget_apps›  s    zWit.get_appsc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )ús
        Returns an object representation of the specified app.

        :param app_id: ID of existing app
        Tr+   ú/apps/r,   r=   rB   ©r&   Úapp_idr   r+   r   rH   r/   r	   r	   r
   Úapp_info­  s         ÿzWit.app_infoc                 C   sD   i }|p
i }|rd|d< dt j |¡ }t| j| jd|||d}|S )rl   Tr+   rm   rV   r=   rB   rn   r	   r	   r
   Ú
delete_app½  s         ÿzWit.delete_appc                 C   sH   i }|p
i }|rd|d< dt j |¡ d }t| j| jd|||d}|S )zk
        Returns an array of all tag groups for an app.

        :param app_id: ID of existing app
        Tr+   rm   z/tagsr,   r=   rB   rn   r	   r	   r
   Úapp_versionsÍ  s         ÿzWit.app_versionsc                 C   sT   i }|p
i }|rd|d< dt j |¡ d t j |¡ }t| j| jd|||d}|S )z–
        Returns an object representation of the specified app.

        :param app_id: ID of existing app
        :param tag_id: name of tag
        Tr+   rm   ú/tags/r,   r=   rB   )r&   ro   Ztag_idr   r+   r   rH   r/   r	   r	   r
   Úapp_version_infoÝ  s    ÿ     ÿzWit.app_version_infoc           	   	   C   sJ   i }|p
i }d|i}d| d }|r,||d< t | j| jd||||d}|S )z“
        Create a new version of your app.

                :param app_id: ID of existing app
                :param tag_name: name of tag
        Útagrm   rs   r+   r1   rg   r3   )	r&   ro   Útag_namer   r+   r   r2   rH   r/   r	   r	   r
   Úcreate_app_versionð  s     ù	zWit.create_app_versionc                 C   sT   i }|p
i }dt j |¡ d t j |¡ }|r8||d< t| j| jd|||d}|S )z˜
        Delete a specific version of your app.

                :param app_id: ID of existing app
                :param tag_name: name of tag
        rm   rs   r+   rV   r=   rB   )r&   ro   rv   r   r+   r   rH   r/   r	   r	   r
   Údelete_app_version  s*    
ÿþ
ýÿ     ÿzWit.delete_app_versionc                 C   s4   i }|p
i }|rd|d< t | j| jdd||d}|S )z^
        Get a URL where you can download a ZIP file containing all of your app data.
        Tr+   r,   z/exportr=   r3   r>   r	   r	   r
   Úexport  s         ÿz
Wit.exportc                 C   sP   i }|p
i }|dk	r||d< |r(||d< |r4||d< t | j| jdd||d}|S )z©
        Create a new app with all the app data from the exported app.

                :param name: name of the new app
        :param private: private if true
        NÚnameÚprivater+   r1   z/import)r2   r3   )r&   rz   r{   Úzip_filer   r+   r   r/   r	   r	   r
   Ú
import_app+  s"         ÿzWit.import_appc              	   C   sB   i }|p
i }d|i}d}|r$||d< t | j| jd||||d}|S )zƒ
        Creates a new intent with the given attributes.

                :param intent_name: name of intent to be created
        rz   r<   r+   r1   rg   r3   )r&   rG   r   r+   r   r2   rH   r/   r	   r	   r
   Úcreate_intent?  s     ù	zWit.create_intentc           
   	   C   sP   i }|p
i }||dœ}d}|r&||d< |r2||d< t | j| jd||||d}	|	S )a  
        Creates a new intent with the given attributes.

                :param entity_name: name of entity to be created
                :param roles: list of roles you want to create for the entity
                :param lookups:  list of lookup strategies
        ©rz   ÚrolesrJ   Úlookupsr+   r1   rg   r3   )
r&   rN   r€   r   r   r+   r   r2   rH   r/   r	   r	   r
   Úcreate_entityV  s$    

ù	zWit.create_entityc              	   C   s\   i }|p
i }||dœ}dt j |¡ }	|r2||d< |r>||d< t| j| jd|	|||d}
|
S )zï
        Updates the attributes of an entity.

                :param entity_name: name of entity to be updated
                :param roles: updated list of roles
                :param lookups:  updated list of lookup strategies
        r   rL   r   r+   ÚPUTrg   rB   )r&   Zcurrent_entity_nameZnew_entity_namer€   r   r   r+   r   r2   rH   r/   r	   r	   r
   Úupdate_entitys  s$    
ù	zWit.update_entityc              	   C   sJ   i }|p
i }dt j |¡ d }|r,||d< t| j| jd||||d}|S )z­
        Add a possible value into the list of keywords for the keywords entity.

                :param entity_name: name of entity to which keyword is to be added
        rL   z	/keywordsr+   r1   rg   rB   )r&   rN   r2   r   r+   r   rH   r/   r	   r	   r
   Úadd_keyword_value–  s    ù	zWit.add_keyword_valuec           
   	   C   sb   i }|p
i }dt j |¡ d t j |¡ d }d|i}|rD||d< t| j| jd||||d}	|	S )a<  
        Create a new synonym of the canonical value of the keywords entity.

                :param entity_name: name of entity to which synonym is to be added
                :param keyword_name: name of keyword to which synonym is to be added
                :param synonym: name of synonym to be created
        rL   r[   z	/synonymsÚsynonymr+   r1   rg   rB   )
r&   rN   r\   r†   r   r+   r   rH   r2   r/   r	   r	   r
   Úcreate_synonym¬  s2    

ÿþ
ýüÿù	zWit.create_synonymc           	   	   C   sD   i }|p
i }||dœ}d}|r&||d< t | j| jd||||d}|S )z¼
        Creates a new trait with the given attributes.

                :param trait_name: name of trait to be created
                :param values: list of values for the trait
        )rz   ÚvaluesrP   r+   r1   rg   r3   )	r&   rT   rˆ   r   r+   r   r2   rH   r/   r	   r	   r
   Úcreate_traitÍ  s     
ù	zWit.create_traitc           	   	   C   sR   i }|p
i }d|i}dt j |¡ d }|r4||d< t| j| jd||||d}|S )zÎ
        Creates a new trait with the given attributes.

                :param trait_name: name of trait to which new value is to be added
                :param new_value: name of new trait value
        ÚvaluerR   z/valuesr+   r1   rg   rB   )	r&   rT   Ú	new_valuer   r+   r   r2   rH   r/   r	   r	   r
   Úcreate_trait_valueå  s     ù	zWit.create_trait_valuec              	   C   s:   i }|p
i }d}|r||d< t | j| jd||||d}|S )zr
        Train your utterances.

                :param data: array of utterances with required arguments
        rd   r+   r1   rg   r3   )r&   r2   r   r+   r   rH   r/   r	   r	   r
   Útrainý  s    ù	z	Wit.trainc              	   C   sR   i }|p
i }|||dœ}d}	|r(||d< |r4||d< t | j| jd|	|||d}
|
S )a  
        Creates a new app for an existing user.

                :param app_name: name of new app
                :param lang: language code in ISO 639-1 format
                :param private: private if true
                :param timezone: default timezone of the app
        )rz   Úlangr{   rj   Útimezoner+   r1   rg   r3   )r&   Úapp_namerŽ   r{   r   r   r+   r   r2   rH   r/   r	   r	   r
   Ú
create_app  s$    ù	zWit.create_appc              	   C   sz   i }|p
i }i }	dt j |¡ }
|r,||	d< |r8||	d< |rD||	d< |rP||	d< |r\||d< t| j| jd|
||	|d}|S )	a  
        Updates existing app with given attributes.

                :param app_name: new_name
                :param lang: language code in ISO 639-1 format
                :param private: private if true
                :param timezone: default timezone of the app
        rm   rz   rŽ   r{   r   r+   rƒ   rg   rB   )r&   ro   r   rŽ   r{   r   r   r+   r   r2   rH   r/   r	   r	   r
   Ú
update_app1  s0    ù	zWit.update_appc              	   C   s~   i }|p
i }i }	dt j |¡ d t j |¡ }
|r<||	d< |rH||	d< |rT||	d< |r`||d< t| j| jd|
||	|d}|S )	a^  
        Update the tag's name or description, or move the tag to point to another tag.

                :param app_id: ID of existing app
                :param tag_name: name of existing tag
                :param new_name: name of new tag
                :param desc: new description of tag
                :param move_to: new name of tag
        rm   rs   ru   ÚdescÚmove_tor+   rƒ   rg   rB   )r&   ro   rv   Únew_namer“   r”   r   r+   r   r2   rH   r/   r	   r	   r
   Úupdate_app_version\  s:    
ÿþ
ýÿù	zWit.update_app_version)N)NNN)NN)NN)NN)NNN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NNNN)NN)NNN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NNN)NNN)NN)NN)NN)NN)NN)NNN)NNNNNN)NNNNN)-r   r   r   r   Z	_sessionsr'   r0   r4   r;   r?   r@   rI   rK   rO   rQ   rU   rW   rX   rZ   r]   r^   r_   r`   re   ri   rk   rp   rq   rr   rt   rw   rx   ry   r}   r~   r‚   r„   r…   r‡   r‰   rŒ   r   r‘   r’   r–   r	   r	   r	   r
   r#   1   sŒ   














   ÿ


       ÿ











     ÿ
"   ù
#
   ÿ
!


     ÿ
!      ø
/     ør#   )r   r$   ÚosÚurllib.requestrC   Úurllib.parseÚurllib.errorr   Zprompt_toolkitr   Zprompt_toolkit.historyr   Úgetenvr   r   r6   Z
LEARN_MOREÚ	Exceptionr   r"   r#   r	   r	   r	   r
   Ú<module>   s   