Skip to content

Commit

Permalink
DEVEXP-295: Refactor channel credentials interface (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Feb 6, 2024
1 parent 40e970d commit b448185
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 34 deletions.
5 changes: 4 additions & 1 deletion examples/simple-examples/src/conversation/app/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import {
if (printFormat === 'pretty') {
console.log(`App updated! New name: '${response.display_name}'.`);
const token = getMessengerTokenFormConfig();
console.log(`Verifying the token (it should be unchanged):\nOLD: '${token}'\nNEW: '${response.channel_credentials?.[0].static_token?.token}'`);
const channelCredentials = response.channel_credentials?.[0];
if(channelCredentials?.channel === 'MESSENGER') {
console.log(`Verifying the token (it should be unchanged):\nOLD: '${token}'\nNEW: '${channelCredentials.static_token?.token}'`);
}
} else {
printFullResponse(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface AppResponse {
callback_settings?: CallbackSettings;
/** Fallback upon no positive delivery report configuration. */
delivery_report_based_fallback?: DeliveryReportBasedFallback | null;
/** TBC: Message retry time configuration. */
/** Message retry time configuration. */
message_retry_settings?: MessageRetrySettings | null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,119 @@ import { AppleBcCredentials } from '../applebc-credentials';
/**
* Enables access to the underlying messaging channel.
*/
export interface ConversationChannelCredential {
export type ConversationChannelCredential =
ChannelCredentialsAppleBC
| ChannelCredentialsInstagram
| ChannelCredentialsKakaoTalk
| ChannelCredentialsKakaoTalkChat
| ChannelCredentialsLine
| ChannelCredentialsMessenger
| ChannelCredentialsMms
| ChannelCredentialsRcs
| ChannelCredentialsSms
| ChannelCredentialsTelegram
| ChannelCredentialsViber
| ChannelCredentialsViberBM
| ChannelCredentialsWeChat
| ChannelCredentialsWhatsApp;

/** The secret used to verify the channel callbacks for channels which support callback verification. The callback verification is not needed for Sinch-managed channels because the callbacks are not leaving Sinch internal networks. Max length is 256 characters. Note: leaving channel_callback_secret empty for channels with callback verification will disable the verification. */
callback_secret?: string;
/** @see ConversationChannel */
channel: ConversationChannel;
/** @see MMSCredentials */
mms_credentials?: MMSCredentials;
/** @see SMSCredentials */
sms_credentials?: SMSCredentials;
/** @see KakaoTalkCredentials */
kakaotalk_credentials?: KakaoTalkCredentials;
/** @see KakaoTalkCredentials */
kakaotalkchat_credentials?: KakaoTalkChatCredentials;
export interface ChannelCredentialsWhatsApp extends ConversationChannelCredentialBase {
channel: 'WHATSAPP';
/** @see StaticBearerCredential */
static_bearer: StaticBearerCredential;
}

export interface ChannelCredentialsRcs extends ConversationChannelCredentialBase {
channel: 'RCS';
/** @see StaticBearerCredential */
static_bearer?: StaticBearerCredential;
static_bearer: StaticBearerCredential;
}

export type ChannelCredentialsSms = ChannelCredentialsSmsWithBearer | ChannelCredentialsSmsWithAppId;

export interface ChannelCredentialsSmsWithBearer extends ConversationChannelCredentialBase {
channel: 'SMS';
/** @see StaticBearerCredential */
static_bearer: StaticBearerCredential;
}

export interface ChannelCredentialsSmsWithAppId extends ConversationChannelCredentialBase {
channel: 'SMS';
/** @see SMSCredentials */
sms_credentials: SMSCredentials;
}

export interface ChannelCredentialsMessenger extends ConversationChannelCredentialBase {
channel: 'MESSENGER';
/** @see StaticTokenCredential */
static_token?: StaticTokenCredential;
static_token: StaticTokenCredential;
}

export interface ChannelCredentialsViber extends ConversationChannelCredentialBase {
channel: 'VIBER';
/** @see StaticTokenCredential */
static_token: StaticTokenCredential;
}

export interface ChannelCredentialsViberBM extends ConversationChannelCredentialBase {
channel: 'VIBERBM';
/** @see StaticBearerCredential */
static_bearer: StaticBearerCredential;
}

export interface ChannelCredentialsMms extends ConversationChannelCredentialBase {
channel: 'MMS';
/** @see MMSCredentials */
mms_credentials: MMSCredentials;
}

export interface ChannelCredentialsInstagram extends ConversationChannelCredentialBase {
channel: 'INSTAGRAM';
/** @see InstagramCredentials */
instagram_credentials: InstagramCredentials;
}

export interface ChannelCredentialsTelegram extends ConversationChannelCredentialBase {
channel: 'TELEGRAM';
/** @see TelegramCredentials */
telegram_credentials?: TelegramCredentials;
telegram_credentials: TelegramCredentials;
}

export interface ChannelCredentialsKakaoTalk extends ConversationChannelCredentialBase {
channel: 'KAKAOTALK';
/** @see KakaoTalkCredentials */
kakaotalk_credentials: KakaoTalkCredentials;
}

export interface ChannelCredentialsKakaoTalkChat extends ConversationChannelCredentialBase {
channel: 'KAKAOTALKCHAT';
/** @see KakaoTalkCredentials */
kakaotalkchat_credentials: KakaoTalkChatCredentials;
}

export interface ChannelCredentialsLine extends ConversationChannelCredentialBase {
channel: 'LINE';
/** @see LineCredentials */
line_credentials?: LineCredentials;
line_credentials: LineCredentials;
}

export interface ChannelCredentialsWeChat extends ConversationChannelCredentialBase {
channel: 'WECHAT';
/** @see WeChatCredentials */
wechat_credentials?: WeChatCredentials;
/** @see InstagramCredentials */
instagram_credentials?: InstagramCredentials;
wechat_credentials: WeChatCredentials;
}

export interface ChannelCredentialsAppleBC extends ConversationChannelCredentialBase {
channel: 'APPLEBC';
/** @see AppleBcCredentials */
applebc_credentials?: AppleBcCredentials;
applebc_credentials: AppleBcCredentials;
}

interface ConversationChannelCredentialBase {
/** @see ConversationChannel */
channel: ConversationChannel;
/** The secret used to verify the channel callbacks for channels which support callback verification. The callback verification is not needed for Sinch-managed channels because the callbacks are not leaving Sinch internal networks. Max length is 256 characters. Note: leaving channel_callback_secret empty for channels with callback verification will disable the verification. */
callback_secret?: string;
/**
* Output only. The state of the channel credentials integration.
* When a channel is activated, the user is prompted for credentials that must be validated and in some cases exchanged by a long-lived token (Instagram).
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
export type { ConversationChannelCredential, ChannelIntegrationState } from './conversation-channel-credential';
export type {
ConversationChannelCredential,
ChannelIntegrationState,
ChannelCredentialsAppleBC,
ChannelCredentialsInstagram,
ChannelCredentialsKakaoTalk,
ChannelCredentialsKakaoTalkChat,
ChannelCredentialsLine,
ChannelCredentialsMessenger,
ChannelCredentialsMms,
ChannelCredentialsRcs,
ChannelCredentialsSms,
ChannelCredentialsTelegram,
ChannelCredentialsViber,
ChannelCredentialsViberBM,
ChannelCredentialsWeChat,
ChannelCredentialsWhatsApp,
ChannelCredentialsSmsWithBearer,
ChannelCredentialsSmsWithAppId,
} from './conversation-channel-credential';
152 changes: 142 additions & 10 deletions packages/conversation/tests/rest/v1/app/app-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ import {
CreateAppRequestData,
DeleteAppRequestData,
GetAppRequestData,
ListAppsRequestData, UpdateAppRequestData,
ListAppsRequestData,
UpdateAppRequestData,
ChannelCredentialsAppleBC,
ChannelCredentialsInstagram,
ChannelCredentialsKakaoTalk,
ChannelCredentialsKakaoTalkChat,
ChannelCredentialsLine,
ChannelCredentialsMessenger,
ChannelCredentialsMms,
ChannelCredentialsRcs,
ChannelCredentialsSms,
ListAppsResponse,
AppApi,
AppApiFixture,
ChannelCredentialsTelegram,
ChannelCredentialsViber,
ChannelCredentialsViberBM, ChannelCredentialsWeChat, ChannelCredentialsWhatsApp,
} from '../../../../src';
import { ListAppsResponse } from '../../../../src';
import { AppApi, AppApiFixture } from '../../../../src';

describe('AppApi', () => {
let appApi: AppApi;
Expand All @@ -28,17 +42,135 @@ describe('AppApi', () => {
describe ('createApp', () => {
it('should make a POST request to create a new Conversation App', async () => {
// Given
const channelCredentialsAppleBC: ChannelCredentialsAppleBC = {
channel: 'APPLEBC',
applebc_credentials: {
business_chat_account_id: 'apple_business_chat_account_id',
merchant_id: 'merchant_id',
apple_pay_certificate_reference: 'apple_pay_certificate_reference',
apple_pay_certificate_password: 'apple_pay_certificate_password',
},
};
const channelCredentialsInstagram: ChannelCredentialsInstagram = {
channel: 'INSTAGRAM',
instagram_credentials: {
token: 'instagram_channel_token',
business_account_id: 'instagram_business_account_id',
},
};
const channelCredentialsKakaoTalk: ChannelCredentialsKakaoTalk = {
channel: 'KAKAOTALK',
kakaotalk_credentials: {
kakaotalk_plus_friend_id: 'kakaotalk_friend_id',
kakaotalk_sender_key: 'kakaotalk_sender_key',
},
};
const channelCredentialsKakaoTalkChat: ChannelCredentialsKakaoTalkChat = {
channel: 'KAKAOTALKCHAT',
kakaotalkchat_credentials: {
kakaotalk_plus_friend_id: 'kakaotalk_friend_id',
api_key: 'info_bank_api_key',
},
};
const channelCredentialsLine: ChannelCredentialsLine = {
channel: 'LINE',
line_credentials: {
token: 'line_token',
secret: 'line_secret',
},
};
const channelCredentialsMms: ChannelCredentialsMms = {
channel: 'MMS',
mms_credentials: {
account_id: 'mms_account_id',
api_key: 'mms_api_key',
basic_auth: {
username: 'username',
password: 'password',
},
default_sender: 'default_sender',
},
};
const channelCredentialsMessenger: ChannelCredentialsMessenger = {
channel: 'MESSENGER',
static_token: {
token: 'messenger_static_token',
},
};
const channelCredentialsRcs: ChannelCredentialsRcs = {
channel: 'RCS',
static_bearer: {
claimed_identity: 'rcs_claimed_identity',
token: 'rcs_token',
},
};
const channelCredentialsSms: ChannelCredentialsSms = {
channel: 'SMS',
static_bearer: {
claimed_identity: 'sms_claimed_identity',
token: 'sms_token',
},
};
// const channelCredentialsSmsWithAppId: ChannelCredentialsSms = {
// channel: 'SMS',
// sms_credentials: {
// sms_app_id: 'sms_app_id',
// },
// };
const channelCredentialsTelegram: ChannelCredentialsTelegram = {
channel: 'TELEGRAM',
telegram_credentials: {
token: 'telegram_token',
},
};
const channelCredentialsViber: ChannelCredentialsViber = {
channel: 'VIBER',
static_token: {
token: 'viber_token',
},
};
const channelCredentialsViberBM: ChannelCredentialsViberBM = {
channel: 'VIBERBM',
static_bearer: {
claimed_identity: 'viberbm_claimed_identity',
token: 'viberbm_token',
},
};
const channelCredentialsWeChat: ChannelCredentialsWeChat = {
channel: 'WECHAT',
wechat_credentials: {
app_id: 'wechat_app_id',
app_secret: 'wechat_app_secret',
token: 'wechat_token',
aes_key: 'wechat_aes_key',
},
};
const channelCredetialsWhatsApp: ChannelCredentialsWhatsApp = {
channel: 'WHATSAPP',
static_bearer: {
claimed_identity: 'whatsapp_claimed_identity',
token: 'whatsapp_token',
},
};
const requestData: CreateAppRequestData = {
appCreateRequestBody: {
display_name: 'Test App',
channel_credentials: [
{
channel: 'WHATSAPP',
static_bearer: {
claimed_identity: 'identity',
token: 'token',
},
},
channelCredentialsAppleBC,
channelCredentialsInstagram,
channelCredentialsKakaoTalk,
channelCredentialsKakaoTalkChat,
channelCredentialsLine,
channelCredentialsMms,
channelCredentialsMessenger,
channelCredentialsRcs,
channelCredentialsSms,
// channelCredentialsSmsWithAppId,
channelCredentialsTelegram,
channelCredentialsViber,
channelCredentialsViberBM,
channelCredentialsWeChat,
channelCredetialsWhatsApp,
],
},
};
Expand Down

0 comments on commit b448185

Please sign in to comment.