import { APIClient } from '@wireapp/api-client' ; import type { LoginData } from '@wireapp/api-client/src/auth' ; import { ClientType } from '@wireapp/api-client/src/client' ; import { Text } from '@wireapp/protocol-messaging' ; import { Account } from '../main/Account' ; import { PayloadBundleType } from '../main/conversation' ; import type { ButtonActionContent , ButtonActionConfirmationContent } from '../main/conversation/content' ; import 'dotenv-defaults/config' ; import { MessageBuilder } from '../main/conversation/message/MessageBuilder' ; const args = process . argv . slice ( 2 ) ; const conversationId = args [ 0 ] || process . env . WIRE_CONVERSATION ; if ( ! conversationId ) { console . error ( 'Please provide a conversation ID when starting this script.' ) ; process . exit ( 1 ) ; } const login = { clientType : ClientType . TEMPORARY , email : process . env . WIRE_EMAIL , password : process . env . WIRE_PASSWORD , } ; ( async ( ) => { const backend = process . env . WIRE_BACKEND === 'staging' ? APIClient . BACKEND . STAGING : APIClient . BACKEND . PRODUCTION ; const apiClient = new APIClient ( { urls : backend } ) ; const account = new Account ( apiClient ) ; const { clientId , userId } = await account . login ( login ) ; console . info ( `Hello user "${ userId }" with client "${ clientId }" ...` ) ; await account . listen ( ) ; account . on ( PayloadBundleType . BUTTON_ACTION , async ( buttonActionMessage ) => { const { conversation , from } = buttonActionMessage ; const { content : { buttonId , referenceMessageId } , } = buttonActionMessage ; const buttonActionConfirmationContent = { buttonId , referenceMessageId , } ; const buttonActionConfirmationMessage = MessageBuilder . createButtonActionConfirmationMessage ( { conversationId : conversation , from : clientId , content : buttonActionConfirmationContent , } ) ; await account . service ! . conversation . send ( { payloadBundle : buttonActionConfirmationMessage , userIds : [ from ] } ) ; console . info ( `Confirmed button click on "${ buttonId }" for poll "${ referenceMessageId }" by user "${ from }" in conversation "${ conversation }".` ) ; } ) ; if ( account . service ) { const pollMessage = MessageBuilder . createComposite ( { conversationId , from : '' } ) . addText ( Text . create ( { content : 'Are you a robot?' } ) ) . addButton ( 'Yes' ) . addButton ( 'No' ) . build ( ) ; await account . service . conversation . send ( { payloadBundle : pollMessage } ) ; const buttonItems = pollMessage . content . items ! . filter ( item => typeof item . button === 'object' ) ; const lastButton = buttonItems [ buttonItems . length - 1 ] . button ; const buttonActionContent = { buttonId : lastButton ! . id , referenceMessageId : pollMessage . id , } ; const buttonActionMessage = MessageBuilder . createButtonActionMessage ( { conversationId , from : clientId , content : buttonActionContent , } ) ; await account . service . conversation . send ( { payloadBundle : buttonActionMessage } ) ; } } ) ( ) . catch ( error => console . error ( 'Error' , error . message ) ) ;