npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-messenger-platform

v0.3.1

Published

Client/Server for FB messenger platform written in TypeScript

Downloads

9

Readme

typed-messenger-platform

Toolkit for developing applications for the Messenger platform written in TypeScript.

State

In active development. Feel free to open an issue if you would like to contribute or check out existing issues.

Principles

  • follow the original API as closely as possible and provide type information
  • de-couple server (for receiving webhooks), client (for sending messages etc) and message builder parts (POJO)

Goals

Setup

To interact with the messenger platform you require a fb app connected to a page. Set up a webhook connecting the page with your server. To install the module:

npm i typed-messenger-platform

Usage

Echo bot:

import http = require('http');
import * as platform from 'typed-messenger-platform';

const PAGE_TOKEN = 'TOKEN';

const { sendMessage, messageTo } = platform;

const fbServer = platform
  // initial configuration is passed via createServer
  .createServer({
    verificationToken: 'YOUR TOKEN',
  })
  // handle messages of specific types with .onTextMessage, .onLocationMessage etc
  .onTextMessage(
    async (testMessage: platform.webhookApi.MessageReceivedEvent) => {
      // decide
      // - when to construct a message (with the builder interface `messageTo`)
      // - when to actually send a message (with `sendMessage` call)
      // - whether to process synchronously or concurrently (with await)
      await sendMessage(
        PAGE_TOKEN,
        messageTo(testMessage.sender).text(
          testMessage.message.text || 'No message',
        ),
      );
    },
  );

// call .done to get node http compatible handler
http.createServer(fbServer.done()).listen(3000);

// monitor errors asynchronously
fbServer.on('error', (err: Error) => {
  console.log('error', err);
})

Message builder interface allows chaining calls to build a message:

messageTo(recipient)
  .text('Answer the question: Where is Berlin?')
  .quickTextReply('Germany', 'GER')
  .quickLocationReply();

Also see examples.

The following message handlers (on[MessageType]Message) are available:

  • onTextMessage - fired if the message contains text
  • onLocationMessage - fired if the message contains a location attachment
  • onAudioMessage - fired if the message contains an audio attachment
  • onVideoMessage - fired if the message contains a video attachment
  • onFileMessage - fired if the message contains a file attachment
  • onImageMessage - fired if the message contains a image attachment
  • onUnknownMessage - fired for every message of unknown type
  • onAnyMesage - fired for every message of known type

The messages are evaluated in order and only one of the handlers is called for any given message, the one that matches first. Exceptions are onAnyMesage which is fired always for any message of known type (even if a more specific handlers were already called) and onUnknownMessage which is called for every message of unknown type.

LICENCE

MIT