Spaces:
Runtime error
Runtime error
| import { | |
| Body, | |
| Controller, | |
| Delete, | |
| Get, | |
| HttpCode, | |
| HttpStatus, | |
| Param, | |
| Post, | |
| Query, | |
| UsePipes, | |
| } from '@nestjs/common'; | |
| import { ApiOperation, ApiParam, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { ChannelsInfoServiceCore } from '@waha/core/services/ChannelsInfoServiceCore'; | |
| import { isJidNewsletter } from '@waha/core/utils/jids'; | |
| import { | |
| SessionApiParam, | |
| WorkingSessionParam, | |
| } from '@waha/nestjs/params/SessionApiParam'; | |
| import { WAHAValidationPipe } from '@waha/nestjs/pipes/WAHAValidationPipe'; | |
| import { | |
| Channel, | |
| ChannelCategory, | |
| ChannelCountry, | |
| ChannelListResult, | |
| ChannelMessage, | |
| ChannelSearchByText, | |
| ChannelSearchByView, | |
| ChannelView, | |
| CreateChannelRequest, | |
| ListChannelsQuery, | |
| NewsletterIdApiParam, | |
| NewsletterIdOrInviteCodeApiParam, | |
| PreviewChannelMessages, | |
| } from '@waha/structures/channels.dto'; | |
| import { SessionManager } from '../core/abc/manager.abc'; | |
| import { | |
| parseChannelInviteLink, | |
| WhatsappSession, | |
| } from '../core/abc/session.abc'; | |
| ('api_key') | |
| ('api/:session/channels') | |
| ('📢 Channels') | |
| export class ChannelsController { | |
| constructor( | |
| private manager: SessionManager, | |
| private channelsInfoService: ChannelsInfoServiceCore, | |
| ) {} | |
| ('') | |
| ({ summary: 'Get list of know channels' }) | |
| async list( | |
| session: WhatsappSession, | |
| () query: ListChannelsQuery, | |
| ): Promise<Channel[]> { | |
| return session.channelsList(query); | |
| } | |
| ('') | |
| ({ summary: 'Create a new channel.' }) | |
| create( | |
| session: WhatsappSession, | |
| () request: CreateChannelRequest, | |
| ): Promise<Channel> { | |
| return session.channelsCreateChannel(request); | |
| } | |
| (':id') | |
| ({ summary: 'Delete the channel.' }) | |
| delete( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ) { | |
| return session.channelsDeleteChannel(id); | |
| } | |
| (':id') | |
| ({ | |
| summary: 'Get the channel info', | |
| description: | |
| 'You can use either id (123@newsletter) ' + | |
| 'OR invite code (https://www.whatsapp.com/channel/123)', | |
| }) | |
| get( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<Channel> { | |
| if (isJidNewsletter(id)) { | |
| return session.channelsGetChannel(id); | |
| } else { | |
| const inviteCode = parseChannelInviteLink(id); | |
| return session.channelsGetChannelByInviteCode(inviteCode); | |
| } | |
| } | |
| (':id/messages/preview') | |
| (new WAHAValidationPipe()) | |
| ({ | |
| name: 'id', | |
| description: 'Channel id or invite code', | |
| required: true, | |
| type: 'string', | |
| schema: { | |
| default: '0029Va4K0PZ5a245NkngBA2M', | |
| }, | |
| }) | |
| ({ | |
| summary: 'Preview channel messages', | |
| description: | |
| 'You can use either ' + | |
| 'invite code (https://www.whatsapp.com/channel/123) or (123)' + | |
| 'OR' + | |
| 'Channel ID (123@newsletter).', | |
| }) | |
| async previewChannelMessages( | |
| session: WhatsappSession, | |
| ('id') code: string, | |
| () query: PreviewChannelMessages, | |
| ): Promise<ChannelMessage[]> { | |
| if (isJidNewsletter(code)) { | |
| const channel = await session.channelsGetChannel(code); | |
| code = parseChannelInviteLink(channel.invite); | |
| } | |
| const inviteCode = parseChannelInviteLink(code); | |
| return session.previewChannelMessages(inviteCode, query); | |
| } | |
| (':id/follow') | |
| ({ summary: 'Follow the channel.' }) | |
| follow( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<void> { | |
| return session.channelsFollowChannel(id); | |
| } | |
| (':id/unfollow') | |
| ({ summary: 'Unfollow the channel.' }) | |
| unfollow( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<void> { | |
| return session.channelsUnfollowChannel(id); | |
| } | |
| (':id/mute') | |
| ({ summary: 'Mute the channel.' }) | |
| mute( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<void> { | |
| return session.channelsMuteChannel(id); | |
| } | |
| (':id/unmute') | |
| ({ summary: 'Unmute the channel.' }) | |
| unmute( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<void> { | |
| return session.channelsUnmuteChannel(id); | |
| } | |
| ('/search/by-view') | |
| (HttpStatus.OK) | |
| (new WAHAValidationPipe()) | |
| ({ summary: 'Search for channels (by view)' }) | |
| async searchByView( | |
| session: WhatsappSession, | |
| () request: ChannelSearchByView, | |
| ): Promise<ChannelListResult> { | |
| return session.searchChannelsByView(request); | |
| } | |
| ('/search/by-text') | |
| (HttpStatus.OK) | |
| (new WAHAValidationPipe()) | |
| ({ summary: 'Search for channels (by text)' }) | |
| async searchByText( | |
| session: WhatsappSession, | |
| () request: ChannelSearchByText, | |
| ): Promise<ChannelListResult> { | |
| return session.searchChannelsByText(request); | |
| } | |
| ('/search/views') | |
| ({ summary: 'Get list of views for channel search' }) | |
| getSearchViews(): Promise<ChannelView[]> { | |
| return this.channelsInfoService.getViews(); | |
| } | |
| ('/search/countries') | |
| ({ summary: 'Get list of countries for channel search' }) | |
| getSearchCountries(): Promise<ChannelCountry[]> { | |
| return this.channelsInfoService.getCountries(); | |
| } | |
| ('/search/categories') | |
| ({ summary: 'Get list of categories for channel search' }) | |
| getSearchCategories(): Promise<ChannelCategory[]> { | |
| return this.channelsInfoService.getCategories(); | |
| } | |
| } | |