0.3.8 • Published 11 months ago

wa-chat-server-telegram v0.3.8

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

wa-chat-server-telegram

wa-chat-server-telegram is a wa-chat-server adapter for the Telegram messenger.

Create a Telegram Bot

  1. Contact BotFather from your Telegram messenger.
  2. Type /newbot and answer the questions.
  3. Save the token for further use.

Adapter Usage

Register The Adapter

An initialization of the wa-chat-server (in a chat application using wa-chat-server to communicate with Watson Assistant) with a wa-chat-server-telegram adapter looks like this:

require('dotenv').config();
import { WAChatServer, Types } from 'wa-chat-server';
import { WAChatServerTelegramAdapter } from 'wa-chat-server-telegram';
const server: WAChatServer = new WAChatServer((process.env as any) as Types.IWAChatServerConfig);
server.addAdapter('telegram', WAChatServerTelegramAdapter);
server.serve();

Configure The Adapter

We must set the following environment variables (e. g. in .env):

VariableObligatoryMeaning
adapter__telegram__tokenYThe Bot token generated by the BotFather
adapter__telegram__apiUrlYThe Telegram endpoint, typically https://api.telegram.org)
adapter__telegram__callbackUrlYThe endpoint to be called by Telegram (without the path adapters/telegram
adapter__telegram__webhookLifetimeSNThe Telegram webhook lifetime in seconds. Whenever the lifetime is reached a new webhook URL (containing a random token) is generated. 0 by default which means "generate URL only once at application startup"

Local Development

HTTP Tunnel Setup

For local development we need to set up a secure tunnel localhost and Telegram. We can use ngrok for this purpose:

ngrok http 3023

opens a tunel and gives us a public HTTPS URL to be used by Telegram (without an ngrook account the lifetime of the tunnel will be 2 hours). We must then use the Telegram API to configure this URL in Telegram:

export ENDPOINT="[NGROK_URL]"; export TOKEN=[BOT_TOKEN]; curl -X POST -H "Content-Type:application/json" -d "{\"url\":\"$ENDPOINT/adapters/telegram\"}" "https://api.telegram.org/bot$TOKEN/setwebhook"

We may inspect the communication tunneled by ngrok to localhost on http://localhost:4040.

IMPORTANT: Don't do it for a Telegram bot that is already attached to some server installation. Create your own Telegram bot instead.

Transpilation

Run

npm run dev

Linking The Library to The Chat Project

Run

npm link

from the wa-chat-server-telegram root folder.

and then

npm link wa-chat-server-telegram

from the root of your chat application.

0.3.8

11 months ago

0.3.7

11 months ago

0.3.6

12 months ago

0.3.5

12 months ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.0

2 years ago

0.1.4

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago