-
Notifications
You must be signed in to change notification settings - Fork 1
DEVEXP-1276: (Conversation) OAS sync - Sprint 2 #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asein-sinch
wants to merge
2
commits into
feature/number-lookup
Choose a base branch
from
DEVEXP-1276_Conversation_OAS_sync_Sprint-2
base: feature/number-lookup
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
docs/snippets/conversation/messages/listLastMessagesByChannelIdentity.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * Sinch Node.js Snippet | ||
| * See: https://github.com/sinch/sinch-sdk-node/docs/snippets | ||
| */ | ||
| import { SinchClient } from '@sinch/sdk-core'; | ||
| import * as dotenv from 'dotenv'; | ||
| dotenv.config(); | ||
|
|
||
| async function main() { | ||
| const projectId = process.env.SINCH_PROJECT_ID ?? 'MY_PROJECT_ID'; | ||
| const keyId = process.env.SINCH_KEY_ID ?? 'MY_KEY_ID'; | ||
| const keySecret = process.env.SINCH_KEY_SECRET ?? 'MY_KEY_SECRET'; | ||
| const conversationRegion = process.env.SINCH_CONVERSATION_REGION ?? 'MY_CONVERSATION_REGION'; | ||
|
|
||
| // Channel identities to fetch the last message (can be phone numbers, social media IDs, etc. depending on the channel) | ||
| const channelIdentities = ['CHANNEL_IDENTITY_1', 'CHANNEL_IDENTITY_2']; | ||
|
|
||
| const sinch = new SinchClient({ projectId, keyId, keySecret, conversationRegion }); | ||
|
|
||
| try { | ||
| const response = await sinch.conversation.messages.listLastMessagesByChannelIdentity({ | ||
| listLastMessagesByChannelIdentityRequestBody: { | ||
| channel_identities: channelIdentities, | ||
| messages_source: 'DISPATCH_SOURCE', | ||
| }, | ||
| }); | ||
| if (response.data.length === 0) { | ||
| console.log('No Messages found for the specified identities.'); | ||
| return; | ||
| } | ||
| console.log(`✅ Found ${response.data.length} Messages.`); | ||
| response.data.forEach((message) => { | ||
| console.log(message); | ||
| }); | ||
| } catch (err) { | ||
| console.error('❌ Failed to list the Messages for the specified identities:'); | ||
| console.error(err); | ||
| } | ||
| } | ||
|
|
||
| main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
examples/simple-examples/src/conversation/messages/listLastMessagesByIdentities.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { Conversation, PageResult } from '@sinch/sdk-core'; | ||
| import { | ||
| getAppIdFromConfig, | ||
| getContactIdFromConfig, getConversationIdFromConfig, getPhoneNumberFromConfig, | ||
| getPrintFormat, | ||
| initConversationService, | ||
| printFullResponse, | ||
| } from '../../config'; | ||
|
|
||
| const populateMessagesList = ( | ||
| conversationPage: PageResult<Conversation.ConversationMessage>, | ||
| conversationList: Conversation.ConversationMessage[], | ||
| conversationDetailsList: string[], | ||
| ) => { | ||
| conversationPage.data.map((message: Conversation.ConversationMessage) => { | ||
| conversationList.push(message); | ||
| conversationDetailsList.push(`${message.id} - ${message.accept_time}`); | ||
| }); | ||
| }; | ||
|
|
||
| (async () => { | ||
| console.log('******************************************'); | ||
| console.log('* Messages_ListMessagesByChannelIdentity *'); | ||
| console.log('******************************************'); | ||
|
|
||
| const phoneNumber = getPhoneNumberFromConfig().substring(1); | ||
|
|
||
| const requestData: Conversation.ListLastMessagesByChannelIdentityRequestData = { | ||
| listLastMessagesByChannelIdentityRequestBody: { | ||
| channel_identities: [phoneNumber], | ||
| messages_source: 'DISPATCH_SOURCE', | ||
| }, | ||
| }; | ||
|
|
||
| const conversationService = initConversationService(); | ||
|
|
||
| // ---------------------------------------------- | ||
| // Method 1: Fetch the data page by page manually | ||
| // ---------------------------------------------- | ||
| let response = await conversationService.messages.listLastMessagesByChannelIdentity(requestData); | ||
|
|
||
| const messageList: Conversation.ConversationMessage[] = []; | ||
| const messagesDetailsList: string[] = []; | ||
|
|
||
| // Loop on all the pages to get all the active numbers | ||
| let reachedEndOfPages = false; | ||
| while (!reachedEndOfPages) { | ||
| populateMessagesList(response, messageList, messagesDetailsList); | ||
| if (response.hasNextPage) { | ||
| response = await response.nextPage(); | ||
| } else { | ||
| reachedEndOfPages = true; | ||
| } | ||
| } | ||
|
|
||
| const printFormat = getPrintFormat(process.argv); | ||
|
|
||
| if (printFormat === 'pretty') { | ||
| console.log(messagesDetailsList.length > 0 | ||
| ? 'List of messages:\n' + messagesDetailsList.join('\n') | ||
| : 'Sorry, no messages were found.'); | ||
| } else { | ||
| printFullResponse(messageList); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // Method 2: Use the iterator and fetch data on more pages automatically | ||
| // --------------------------------------------------------------------- | ||
| for await (const message of conversationService.messages.listLastMessagesByChannelIdentity(requestData)) { | ||
| if (printFormat === 'pretty') { | ||
| console.log(`${message.id} - ${message.accept_time}`); | ||
| } else { | ||
| console.log(message); | ||
| } | ||
| } | ||
|
|
||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/conversation/src/models/v1/list-last-messages-by-channel-identity-request/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type { ListLastMessagesByChannelIdentityRequest } from './list-last-messages-by-channel-identity-request'; |
30 changes: 30 additions & 0 deletions
30
...st-messages-by-channel-identity-request/list-last-messages-by-channel-identity-request.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { ConversationChannel } from '../conversation-channel'; | ||
| import { ConversationDirection, ConversationMessagesView, MessageSource } from '../enums'; | ||
|
|
||
| /** | ||
| * Request body for listing messages by channel identity. NOTE: You can use either contact_ids OR channel_identities, but not both in the same request. | ||
| */ | ||
| export interface ListLastMessagesByChannelIdentityRequest { | ||
| /** Optional. Filter messages by `channel_identity`. */ | ||
| channel_identities?: string[]; | ||
| /** Optional. Resource name (id) of the contact. In case the messages source is set to `CONVERSATION_SOURCE`: Can list last messages by contact_id. In case the messages source is set to `DISPATCH_SOURCE`: The field is unsupported and cannot be set. */ | ||
| contact_ids?: string[]; | ||
| /** Optional. Resource name (id) of the app. */ | ||
| app_id?: string; | ||
| /** Optional. Specifies the message source for which the request will be processed. Default is `DISPATCH_SOURCE`. */ | ||
| messages_source?: MessageSource; | ||
| /** Optional. Maximum number of messages to fetch. Defaults to 10 and the maximum is 1000. */ | ||
| page_size?: number; | ||
| /** Optional. Next page token previously returned if any. */ | ||
| page_token?: string; | ||
| /** Optional. Specifies the representation in which messages should be returned. Default to `WITH_METADATA`. */ | ||
| view?: ConversationMessagesView; | ||
| /** Optional. Only fetch messages with `accept_time` after this date. */ | ||
| start_time?: Date; | ||
| /** Optional. Only fetch messages with `accept_time` before this date. */ | ||
| end_time?: Date; | ||
| /** Optional. Only fetch messages from the `channel`. */ | ||
| channel?: ConversationChannel; | ||
| /** Optional. Only fetch messages with the specified `direction`. If direction is not specified, it will list both TO_APP and TO_CONTACT messages. */ | ||
| direction?: ConversationDirection; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add a comment to explain "why"
.substring(1)or having a dedicated helper function with naming helping to understand