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

@effect-line/messaging-api

v0.1.0

Published

The LINE Messaging API integration

Downloads

66

Readme

@effect-line/messaging-api

Effect-based wrapper for LINE Messaging API with automatic error handling and retries

Features

  • Type-safe LINE Messaging API integration
  • Automatic error handling and retries
  • Effect-based API for better composability
  • Full TypeScript support
  • Modern ES modules

Installation

pnpm add @effect-line/messaging-api

Usage

Configuration

First, set up your LINE API credentials as environment variables:

export LINE_CHANNEL_ID=your_channel_id
export LINE_CHANNEL_SECRET=your_channel_secret
export LINE_CHANNEL_ACCESS_TOKEN=your_channel_access_token

Push Message

Send a message to a single user:

import { Effect } from "effect"
import { NodeRuntime } from "@effect/platform-node"
import { MessagingApi } from "@effect-line/messaging-api"

const program = Effect.gen(function* () {
  const api = yield* MessagingApi

  // Send a text message
  const result = yield* api.pushMessage({
    to: "USER_ID",
    messages: [
      {
        type: "text",
        text: "Hello, world!"
      }
    ]
  })

  return result
})

// Run with automatic error handling
NodeRuntime.runMain(
  program.pipe(
    Effect.provide(MessagingApi.Default)
  )
)

Multicast Message

Send messages to multiple users:

const multicastProgram = Effect.gen(function* () {
  const api = yield* MessagingApi

  // Send to multiple users
  yield* api.multicast({
    to: ["USER_ID_1", "USER_ID_2"],
    messages: [
      {
        type: "text",
        text: "Hello, everyone!"
      }
    ]
  })
})

Error Handling

The API automatically handles common errors and implements retry policies:

import { Effect, Console } from "effect"

const programWithErrorHandling = program.pipe(
  Effect.catchTag("RateLimitError", (error) => 
    Console.log("Rate limit exceeded:", error)
  ),
  Effect.catchTag("InvalidTokenError", (error) => 
    Console.error("Invalid token:", error)
  )
)

Using Custom Channel Access Token

If you need to use a different channel access token than the one specified in environment variables, you can create a custom layer:

import { MessagingApi } from "@effect-line/messaging-api"

// Create a layer with custom access token
const customBot = MessagingApi.layer("your-custom-access-token")

const program = Effect.gen(function*() {
  const api = yield* MessagingApi
  
  yield* api.pushMessage({
    to: "USER_ID",
    messages: [{ type: "text", text: "Hello from custom bot!" }]
  })
})

// Use the custom bot layer
program.pipe(
  Effect.provide(customBot),
  Effect.runPromise
)

Using Multiple Bots

You can create multiple layers for different bots and use them in your program:

import { MessagingApi } from "@effect-line/messaging-api"

// Create layers for different bots
const bot1 = MessagingApi.layer("bot1-access-token")
const bot2 = MessagingApi.layer("bot2-access-token")

// Use bot1
const program1 = Effect.gen(function*() {
  const api = yield* MessagingApi
  yield* api.pushMessage({
    to: "USER_ID",
    messages: [{ type: "text", text: "Hello from Bot 1!" }]
  })
}).pipe(
  Effect.provide(bot1),
  Effect.runPromise
)

// Use bot2
const program2 = Effect.gen(function*() {
  const api = yield* MessagingApi
  yield* api.pushMessage({
    to: "USER_ID",
    messages: [{ type: "text", text: "Hello from Bot 2!" }]
  })
}).pipe(
  Effect.provide(bot2),
  Effect.runPromise
)

API Reference

MessagingApi

The main service interface:

interface MessagingApi {
  readonly pushMessage: (params: PushMessageParams) => Effect<never, ApiError, PushMessageResult>
  readonly multicast: (params: MulticastParams) => Effect<never, ApiError, MulticastResult>
  // ... more methods
}

Error Types

Common error types that may occur:

  • ApiError - Base error type
  • RateLimitError - When API rate limit is exceeded
  • InvalidTokenError - When access token is invalid
  • NetworkError - For connection issues
  • ValidationError - For invalid parameters

CLI Integration

This package is used by @effect-line/cli to provide command-line interface for LINE operations. See the CLI package for more details.

License

MIT License - see the LICENSE file for details