mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-28 21:07:39 +00:00
[examples] Update examples
This commit is contained in:
parent
cc76b27cee
commit
f1b8e2d4f5
@ -1,12 +1,30 @@
|
||||
var TelegramBot = require('../src/telegram');
|
||||
/**
|
||||
* This example demonstrates setting up a webook, using a
|
||||
* self-signed certificate.
|
||||
*/
|
||||
|
||||
var options = {
|
||||
|
||||
const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN';
|
||||
const TelegramBot = require('..');
|
||||
const options = {
|
||||
webHook: {
|
||||
port: 443,
|
||||
key: __dirname+'/key.pem',
|
||||
cert: __dirname+'/crt.pem'
|
||||
key: `${__dirname}/key.pem`, // Path to file with PEM private key
|
||||
cert: `${__dirname}/crt.pem` // Path to file with PEM certificate
|
||||
}
|
||||
};
|
||||
// This URL must route to the port set above (i.e. 443)
|
||||
const url = 'https://<PUBLIC-URL>';
|
||||
const bot = new TelegramBot(TOKEN, options);
|
||||
|
||||
var bot = new TelegramBot('BOT_TOKEN', options);
|
||||
bot.setWebHook('IP:PORT/botBOT_TOKEN', __dirname+'/crt.pem');
|
||||
|
||||
// This informs the Telegram servers of the new webhook.
|
||||
bot.setWebHook(`${url}/bot${TOKEN}`, {
|
||||
certificate: options.webHook.cert,
|
||||
});
|
||||
|
||||
|
||||
// Just to ping!
|
||||
bot.on('message', function onMessage(msg) {
|
||||
bot.sendMessage(msg.chat.id, "I'm alive!");
|
||||
});
|
||||
|
@ -1,16 +1,32 @@
|
||||
// An example for OpenShift platform.
|
||||
var TelegramBot = require('node-telegram-bot-api');
|
||||
/**
|
||||
* This example demonstrates setting up webhook
|
||||
* on the OpenShift platform.
|
||||
*/
|
||||
|
||||
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
|
||||
|
||||
const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN';
|
||||
const TelegramBot = require('..');
|
||||
// See https://developers.openshift.com/en/node-js-environment-variables.html
|
||||
var port = process.env.OPENSHIFT_NODEJS_PORT;
|
||||
var host = process.env.OPENSHIFT_NODEJS_IP;
|
||||
var domain = process.env.OPENSHIFT_APP_DNS;
|
||||
const options = {
|
||||
webHook: {
|
||||
port: process.env.OPENSHIFT_NODEJS_PORT,
|
||||
host: process.env.OPENSHIFT_NODEJS_IP,
|
||||
// you do NOT need to set up certificates since OpenShift provides
|
||||
// the SSL certs already (https://<app-name>.rhcloud.com)
|
||||
},
|
||||
};
|
||||
// OpenShift routes from port :443 to OPENSHIFT_NODEJS_PORT
|
||||
const domain = process.env.OPENSHIFT_APP_DNS;
|
||||
const url = `${domain}:443`;
|
||||
const bot = new TelegramBot(TOKEN, options);
|
||||
|
||||
var bot = new TelegramBot(token, {webHook: {port: port, host: host}});
|
||||
// OpenShift enroutes :443 request to OPENSHIFT_NODEJS_PORT
|
||||
bot.setWebHook(domain+':443/bot'+token);
|
||||
bot.on('message', function (msg) {
|
||||
var chatId = msg.chat.id;
|
||||
bot.sendMessage(chatId, "I'm alive!");
|
||||
|
||||
// This informs the Telegram servers of the new webhook.
|
||||
// Note: we do not need to pass in the cert, as it already provided
|
||||
bot.setWebHook(`${url}/bot${TOKEN}`);
|
||||
|
||||
|
||||
// Just to ping!
|
||||
bot.on('message', function onMessage(msg) {
|
||||
bot.sendMessage(msg.chat.id, "I'm alive on OpenShift!");
|
||||
});
|
||||
|
@ -1,55 +1,54 @@
|
||||
var TelegramBot = require('../src/telegram');
|
||||
var request = require('request');
|
||||
/**
|
||||
* This example demonstrates using polling.
|
||||
* It also demonstrates how you would process and send messages.
|
||||
*/
|
||||
|
||||
var options = {
|
||||
|
||||
const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN';
|
||||
const TelegramBot = require('..');
|
||||
const request = require('request');
|
||||
const options = {
|
||||
polling: true
|
||||
};
|
||||
const bot = new TelegramBot(TOKEN, options);
|
||||
|
||||
var token = process.env.TELEGRAM_BOT_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';
|
||||
|
||||
var bot = new TelegramBot(token, options);
|
||||
bot.getMe().then(function (me) {
|
||||
console.log('Hi my name is %s!', me.username);
|
||||
});
|
||||
|
||||
// Matches /photo
|
||||
bot.onText(/\/photo/, function (msg) {
|
||||
var chatId = msg.chat.id;
|
||||
// From file
|
||||
var photo = __dirname+'/../test/bot.gif';
|
||||
bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"});
|
||||
bot.onText(/\/photo/, function onPhotoText(msg) {
|
||||
// From file path
|
||||
const photo = `${__dirname}/../test/data/photo.gif`;
|
||||
bot.sendPhoto(msg.chat.id, photo, {
|
||||
caption: "I'm a bot!"
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Matches /audio
|
||||
bot.onText(/\/audio/, function (msg) {
|
||||
var chatId = msg.chat.id;
|
||||
var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
|
||||
// From HTTP request!
|
||||
var audio = request(url);
|
||||
bot.sendAudio(chatId, audio)
|
||||
.then(function (resp) {
|
||||
// Forward the msg
|
||||
var messageId = resp.message_id;
|
||||
bot.forwardMessage(chatId, chatId, messageId);
|
||||
});
|
||||
bot.onText(/\/audio/, function onAudioText(msg) {
|
||||
// From HTTP request
|
||||
const url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
|
||||
const audio = request(url);
|
||||
bot.sendAudio(msg.chat.id, audio);
|
||||
});
|
||||
|
||||
|
||||
// Matches /love
|
||||
bot.onText(/\/love/, function (msg) {
|
||||
var chatId = msg.chat.id;
|
||||
var opts = {
|
||||
bot.onText(/\/love/, function onLoveText(msg) {
|
||||
const opts = {
|
||||
reply_to_message_id: msg.message_id,
|
||||
reply_markup: JSON.stringify({
|
||||
keyboard: [
|
||||
['Yes, you are the bot of my life ❤'],
|
||||
['No, sorry there is another one...']]
|
||||
['No, sorry there is another one...']
|
||||
]
|
||||
})
|
||||
};
|
||||
bot.sendMessage(chatId, 'Do you love me?', opts);
|
||||
bot.sendMessage(msg.chat.id, 'Do you love me?', opts);
|
||||
});
|
||||
|
||||
bot.onText(/\/echo (.+)/, function (msg, match) {
|
||||
var chatId = msg.chat.id;
|
||||
var resp = match[1];
|
||||
bot.sendMessage(chatId, resp);
|
||||
|
||||
// Matches /echo [whatever]
|
||||
bot.onText(/\/echo (.+)/, function onEchoText(msg, match) {
|
||||
const resp = match[1];
|
||||
bot.sendMessage(msg.chat.id, resp);
|
||||
});
|
||||
|
@ -1,24 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var TelegramBot = require('node-telegram-bot-api');
|
||||
|
||||
var TOKEN = 'BOT_TOKEN';
|
||||
var USER = 'USER_ID';
|
||||
|
||||
var bot = new TelegramBot(TOKEN, {polling: {timeout: 1, interval: 100}});
|
||||
|
||||
var opts = {
|
||||
reply_markup: JSON.stringify(
|
||||
{
|
||||
force_reply: true
|
||||
}
|
||||
)};
|
||||
|
||||
bot.sendMessage(USER, 'How old are you?', opts)
|
||||
.then(function (sended) {
|
||||
var chatId = sended.chat.id;
|
||||
var messageId = sended.message_id;
|
||||
bot.onReplyToMessage(chatId, messageId, function (message) {
|
||||
console.log('User is %s years old', message.text);
|
||||
});
|
||||
});
|
@ -19,7 +19,7 @@
|
||||
"test": "istanbul cover ./node_modules/mocha/bin/_mocha",
|
||||
"prepublish:test": "npm run prepublish && npm run test",
|
||||
"gen-doc": "jsdoc2md --src src/telegram.js -t README.hbs > README.md",
|
||||
"eslint": "eslint ./src ./test",
|
||||
"eslint": "eslint ./src ./test ./examples",
|
||||
"pretest": "npm run eslint && npm run build"
|
||||
},
|
||||
"author": "Yago Pérez <yagoperezs@gmail.com>",
|
||||
|
Loading…
x
Reference in New Issue
Block a user