-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
167 additions
and
891 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import { readLines } from 'https://deno.land/[email protected]/io/mod.ts' | |
import { fromFileUrl } from 'https://deno.land/[email protected]/path/mod.ts' | ||
import { start } from '../src/hosting/http.ts' | ||
|
||
start(fromFileUrl(import.meta.resolve('./src'))) | ||
start(fromFileUrl(import.meta.resolve('./src')), 3000) | ||
|
||
const sendMessage = async (message: string) => { | ||
await fetch('http://localhost:3000/webhook', { | ||
|
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,33 +1,18 @@ | ||
import { Configuration } from '../../sdk/config.ts' | ||
import { Configuration } from '../../mod.ts' | ||
import { load } from 'https://deno.land/[email protected]/dotenv/mod.ts' | ||
import { fromFileUrl } from 'https://deno.land/[email protected]/path/mod.ts' | ||
import { MemoryStorage } from '../../src/storage/memory.ts' | ||
import { WebhookChannel } from '../../src/channel/webhook.ts' | ||
|
||
await load({ | ||
export: true, | ||
envPath: fromFileUrl(import.meta.resolve('./.env')), | ||
}) | ||
|
||
const config: Configuration = { | ||
hosting: { | ||
http: { | ||
port: Number(Deno.env.get('PORT')), | ||
}, | ||
}, | ||
logLevel: 'WARNING', | ||
channels: { | ||
webhook: { | ||
url: 'http://localhost:3001', | ||
}, | ||
whatsapp: { | ||
token: Deno.env.get('WHATSAPP_TOKEN') as string, | ||
numberId: Deno.env.get('WHATSAPP_NUMBER_ID') as string, | ||
verifyToken: Deno.env.get('WHATSAPP_VERIFY_TOKEN') as string, | ||
}, | ||
web: {}, | ||
}, | ||
storage: { | ||
type: 'memory', | ||
}, | ||
channels: [new WebhookChannel({ url: 'http://localhost:3001' })], | ||
storage: new MemoryStorage(), | ||
} | ||
|
||
export default config |
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
export * from './sdk/config.ts' | ||
export * from './sdk/storage.ts' | ||
export * from './sdk/channel.ts' | ||
export * from './sdk/hooks.ts' | ||
export * from './sdk/message.ts' | ||
export * from './sdk/state.ts' | ||
export * from './sdk/user.ts' | ||
export * from './sdk/options.ts' | ||
export * from './sdk/log.ts' |
This file contains 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 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 |
---|---|---|
@@ -1,52 +1,18 @@ | ||
export type HttpHostingConfiguration = { | ||
port: number | ||
} | ||
|
||
export type HostingConfiguration = { | ||
http?: HttpHostingConfiguration | ||
} | ||
|
||
export type WebhookChannelConfiguration = { | ||
url: string | ||
authorizationToken?: string | ||
} | ||
|
||
export type WhatsappChannelConfiguration = { | ||
numberId: string | ||
token: string | ||
verifyToken: string | ||
} | ||
|
||
export type WebChannelConfiguration = { | ||
allowedOrigins?: (string | RegExp)[] | ||
authorizationToken?: string | ||
} | ||
|
||
export type ChannelsConfiguration = { | ||
webhook?: WebhookChannelConfiguration | ||
whatsapp?: WhatsappChannelConfiguration | ||
web?: WebChannelConfiguration | ||
} | ||
import { Channel } from './channel.ts' | ||
import { Storage } from './storage.ts' | ||
|
||
export type BotConfiguration = Record<string, unknown> | ||
|
||
export type MongoDbConfiguration = { | ||
url: string | ||
} | ||
|
||
export type StorageConfiguration = | ||
| { | ||
type: 'memory' | ||
} | ||
| { | ||
type: 'mongodb' | ||
config: MongoDbConfiguration | ||
} | ||
|
||
export type Configuration = { | ||
hosting: HostingConfiguration | ||
channels: ChannelsConfiguration | ||
storage: StorageConfiguration | ||
channels: Channel[] | ||
storage: Storage | ||
logLevel?: 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR' | ||
config?: BotConfiguration | ||
} | ||
|
||
let currentConfiguration: Configuration | ||
export const setConfiguration = (configuration: Configuration) => { | ||
currentConfiguration = configuration | ||
} | ||
|
||
export const config = () => currentConfiguration |
This file contains 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import { getLogger, handlers, LevelName } from 'https://deno.land/[email protected]/lo | |
|
||
export const logger = getLogger('mawa') | ||
|
||
export const setup = (levelName: LevelName) => { | ||
export const setupLogger = (levelName: LevelName) => { | ||
logger.levelName = levelName | ||
logger.handlers = [ | ||
new handlers.ConsoleHandler('NOTSET', { | ||
|
This file contains 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 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,9 @@ | ||
import { User, UserId } from './user.ts' | ||
|
||
export interface Storage { | ||
track(userId: UserId, event: string, properties?: Record<string, unknown>): Promise<void> | ||
mergeUser(userId: UserId, user: Partial<Omit<User, 'id'>>): Promise<User> | ||
getUser(id: UserId): Promise<User | undefined> | ||
setKv<T>(userId: UserId, key: string, value: T): Promise<void> | ||
getKv<T>(userId: UserId, key: string): Promise<T | undefined> | ||
} |
This file contains 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,25 @@ | ||
import { MessageHandler, config } from '../../mod.ts' | ||
|
||
export const resolveChannel = async (request: Request, onMessage: MessageHandler): Promise<Response> => { | ||
const channelPatterns = config().channels.map((channel) => ({ | ||
pattern: new URLPattern({ pathname: `/${channel.sourceId}` }), | ||
channel, | ||
})) | ||
|
||
const channel = channelPatterns.find((channel) => channel.pattern.test(request.url))?.channel | ||
if (!channel) { | ||
throw new Error('No channel found for ' + request.url) | ||
} | ||
|
||
const sourceMessage = await channel.receive(request) | ||
if (sourceMessage instanceof Response) { | ||
return sourceMessage | ||
} else if (sourceMessage) { | ||
await onMessage(sourceMessage.sourceAuthorId, sourceMessage.message, channel) | ||
return new Response() | ||
} else if (channel.handle) { | ||
return await channel.handle(request, onMessage) | ||
} else { | ||
throw new Error('Channel ' + channel.sourceId + ' did not return a response') | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import { MawaConfiguration } from '../sdk/config.ts' | ||
import { logger } from './log.ts' | ||
import { config, setConfiguration, logger } from '../mod.ts' | ||
import { resolve } from 'https://deno.land/[email protected]/path/mod.ts' | ||
|
||
let configuration: MawaConfiguration | ||
const configurationWatchers: (() => void | Promise<void>)[] = [] | ||
|
||
const loadConfiguration = async (configFile: string, terminateIfError = false) => { | ||
try { | ||
logger.debug('Loading configuration file', configFile) | ||
|
||
const config = await import(`file:///${configFile}?${Date.now()}`) | ||
configuration = config.default | ||
const configuration = config.default | ||
|
||
setConfiguration(configuration) | ||
logger.debug('Configuration loaded', configuration) | ||
|
||
configurationWatchers.forEach((watcher) => watcher()) | ||
|
@@ -33,8 +32,8 @@ const initializeConfigurationChangesWatcher = async (configFile: string) => { | |
} | ||
} | ||
|
||
export const onConfigurationsLoaded = (callback: typeof configurationWatchers[0]) => { | ||
if (configuration) { | ||
export const onConfigurationsLoaded = (callback: (typeof configurationWatchers)[0]) => { | ||
if (config()) { | ||
callback() | ||
} | ||
|
||
|
@@ -50,5 +49,3 @@ export const initializeConfiguration = async (directory: string, watchForChanges | |
initializeConfigurationChangesWatcher(configFile) | ||
} | ||
} | ||
|
||
export const config = () => configuration |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.