aws-sdk#Textract TypeScript Examples

The following examples show how to use aws-sdk#Textract. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: detect.lambda.ts    From cloudstructs with Apache License 2.0 5 votes vote down vote up
export async function handler(event: SlackEvent): Promise<void> {
  console.log('Event: %j', event);

  const slackClient = new WebClient(process.env.SLACK_TOKEN);

  // Get file info
  const info = await slackClient.files.info({
    file: event.file_id,
  }) as FilesInfoResult;
  console.log('File info: %j', info);

  if (!info.file.mimetype.startsWith('image')) {
    console.log('Not an image');
    return;
  }

  // Get file
  const file = await got(info.file.url_private, {
    headers: {
      Authorization: `Bearer ${process.env.SLACK_TOKEN}`,
    },
  }).buffer();

  // Detect text with Textract
  const textract = new Textract({ apiVersion: '2018-06-27' });

  const data = await textract.detectDocumentText({
    Document: { Bytes: file },
  }).promise();

  if (!data.Blocks) {
    console.log('No text detected');
    return;
  }

  // Add detected text in image thread
  const postMessage = await slackClient.chat.postMessage({
    channel: event.channel_id,
    text: data.Blocks.filter((b) => b.BlockType === 'LINE').map((b) => b.Text).join('\n'),
    thread_ts: info.file.shares.public[event.channel_id][0].ts,
  });
  console.log('Post message: %j', postMessage);
}
Example #2
Source File: handler.test.ts    From cloudstructs with Apache License 2.0 5 votes vote down vote up
test('handler', async () => {
  const fileInfo: FilesInfoResult = {
    ok: true,
    file: {
      mimetype: 'image/jpg',
      filetype: 'image',
      url_private: 'url-private',
      shares: {
        public: {
          C12345XYZ: [{
            ts: 'ts',
          }],
        },
      },
    },
  };
  const filesInfoMock = jest.fn().mockResolvedValue(fileInfo);
  const postMessageMock = jest.fn().mockResolvedValue({ ok: true });
  (WebClient as unknown as jest.Mock).mockImplementation(() => {
    return {
      files: { info: filesInfoMock },
      chat: { postMessage: postMessageMock },
    };
  });

  gotMock.mockImplementation(() => {
    return { buffer: () => Buffer.from('image-buffer') };
  });

  const detected: Textract.DetectDocumentTextResponse = {
    Blocks: [
      {
        BlockType: 'LINE',
        Text: 'Hello',
      },
      {
        BlockType: 'ZZZ',
        Text: 'Not',
      },
      {
        BlockType: 'LINE',
        Text: 'World!',
      },
    ],
  };
  const detectDocumentTextMock = jest.fn(() => {
    return { promise: () => detected };
  });
  (Textract as unknown as jest.Mock).mockImplementation(() => {
    return { detectDocumentText: detectDocumentTextMock };
  });

  await handler({
    channel_id: 'C12345XYZ',
    file_id: 'F1234567XYZ',
  });

  expect(filesInfoMock).toHaveBeenCalledWith({
    file: 'F1234567XYZ',
  });

  expect(gotMock).toHaveBeenCalledWith('url-private', {
    headers: {
      Authorization: 'Bearer token',
    },
  });

  expect(detectDocumentTextMock).toHaveBeenLastCalledWith({
    Document: { Bytes: Buffer.from('image-buffer') },
  });

  expect(postMessageMock).toHaveBeenLastCalledWith({
    channel: 'C12345XYZ',
    text: 'Hello\nWorld!',
    thread_ts: 'ts',
  });
});