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

twilio-conversations-hooks

v1.1.0

Published

Package containing React Hooks for Twilio Conversations

Downloads

101

Readme

Twilio Conversations Hooks

What is it

twilio-conversations-hooks is a library which provides a bunch of React Hooks to make your life easier when using Twilio Conversations API.

Installation

You can get the latest release using npm:

$ npm install --save twilio-conversations-hooks

Example

In order to be able to use the hooks, you first have to render the Provider somewhere before your component and pass the connection options, like:

import React, FunctionComponent from "react"
import { TwilioProvider } from "twilio-conversations-hooks"

const Providers: FunctionComponent = ({ children }) => {
    return (
        <TwilioProvider>
            ...
            {children}
            ...
        </TwilioProvider>
    )
}

First you have to use the useTwilio hook to connect to twilio, and after that you can use the useConversation hook to get and send the messages.

Example:

import React, { VoidFunctionComponent, useState } from "react"
import { useTwilio, useConversation } from "twilio-conversations-hooks"

const Chat: VoidFunctionComponent = () => {
    const [conversationUniqueName, setConversationUniqueName] = useState<string>('')
    const [message, setMessage] = useState<string>('')

    const { connect, conversations } = useTwilioChat()
    const { messages, sendMessages } = useConversation(conversationUniqueName)

    useEffect(() => {
        connectToTwilio()
    }, [])

    // This function will connect to twilio using your token and set a default conversation to pass to useConversation hook
    const connectToTwilio = async (): Promise<void> => {
        const token = localStorage.getItem('token')

        // Here you need to pass your token, in this example I'll get from local storage
        // Optionally you can pass the identity
        const conversations = await connect(token)

        const conversation = conversations.find(conversation => conversation.uniqueName === 'my-conversation-unique-name')

        if (conversation) {
            setConversationUniqueName('my-conversation-unique-name') 
        }
    }

    const addMessage = (): void => {
        sendMessage(message)
        setMessage('')
    }

    const switchConversation = (uniqueName: string): void {
        setConversationUniqueName(uniqueName)
    }

    return (
        <div>
            <ul>
                {
                    conversations.map(conversation => (
                        <li onClick={() => switchConversation(conversation.uniqueName)} key={conversation.uniqueName}>
                            {conversation.uniqueName}
                        </li>
                    ))
                }
            </ul>
            <ul>
                {
                    messages.map(message => (
                        <li>
                            {message.body}
                        </li>
                    ))
                }
            </ul>
            <input value={message} onChange={event => setMessage(event.target.value)} />
            <button onClick={() => addMessage()}>Send Message</button>
        </div>
    )
}

API

useTwilio()

Returns

  • connect | Function - The function to connect to twilio server, you must call this function in order to receive and send messages
  • loading | boolean - True if is connecting to twilio and false if the connection is done
  • conversations | Conversation[] - List of conversations returned from the connection
  • identity | string or undefined - If you pass the identity to the connect function, you can access it through the hook

useConversation()

Parameters

  • uniqueName | string - The unique name of your conversation

Returns

  • messages | Message[] - The list of messages of that conversation
  • sendMessage | Function - The function to send a new message to the conversation