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: {
|
webHook: {
|
||||||
port: 443,
|
port: 443,
|
||||||
key: __dirname+'/key.pem',
|
key: `${__dirname}/key.pem`, // Path to file with PEM private key
|
||||||
cert: __dirname+'/crt.pem'
|
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
|
// See https://developers.openshift.com/en/node-js-environment-variables.html
|
||||||
var port = process.env.OPENSHIFT_NODEJS_PORT;
|
const options = {
|
||||||
var host = process.env.OPENSHIFT_NODEJS_IP;
|
webHook: {
|
||||||
var domain = process.env.OPENSHIFT_APP_DNS;
|
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
|
// This informs the Telegram servers of the new webhook.
|
||||||
bot.setWebHook(domain+':443/bot'+token);
|
// Note: we do not need to pass in the cert, as it already provided
|
||||||
bot.on('message', function (msg) {
|
bot.setWebHook(`${url}/bot${TOKEN}`);
|
||||||
var chatId = msg.chat.id;
|
|
||||||
bot.sendMessage(chatId, "I'm alive!");
|
|
||||||
|
// 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
|
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
|
// Matches /photo
|
||||||
bot.onText(/\/photo/, function (msg) {
|
bot.onText(/\/photo/, function onPhotoText(msg) {
|
||||||
var chatId = msg.chat.id;
|
// From file path
|
||||||
// From file
|
const photo = `${__dirname}/../test/data/photo.gif`;
|
||||||
var photo = __dirname+'/../test/bot.gif';
|
bot.sendPhoto(msg.chat.id, photo, {
|
||||||
bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"});
|
caption: "I'm a bot!"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Matches /audio
|
// Matches /audio
|
||||||
bot.onText(/\/audio/, function (msg) {
|
bot.onText(/\/audio/, function onAudioText(msg) {
|
||||||
var chatId = msg.chat.id;
|
// From HTTP request
|
||||||
var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
|
const url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
|
||||||
// From HTTP request!
|
const audio = request(url);
|
||||||
var audio = request(url);
|
bot.sendAudio(msg.chat.id, audio);
|
||||||
bot.sendAudio(chatId, audio)
|
|
||||||
.then(function (resp) {
|
|
||||||
// Forward the msg
|
|
||||||
var messageId = resp.message_id;
|
|
||||||
bot.forwardMessage(chatId, chatId, messageId);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Matches /love
|
// Matches /love
|
||||||
bot.onText(/\/love/, function (msg) {
|
bot.onText(/\/love/, function onLoveText(msg) {
|
||||||
var chatId = msg.chat.id;
|
const opts = {
|
||||||
var opts = {
|
reply_to_message_id: msg.message_id,
|
||||||
reply_to_message_id: msg.message_id,
|
reply_markup: JSON.stringify({
|
||||||
reply_markup: JSON.stringify({
|
keyboard: [
|
||||||
keyboard: [
|
['Yes, you are the bot of my life ❤'],
|
||||||
['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;
|
// Matches /echo [whatever]
|
||||||
var resp = match[1];
|
bot.onText(/\/echo (.+)/, function onEchoText(msg, match) {
|
||||||
bot.sendMessage(chatId, resp);
|
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",
|
"test": "istanbul cover ./node_modules/mocha/bin/_mocha",
|
||||||
"prepublish:test": "npm run prepublish && npm run test",
|
"prepublish:test": "npm run prepublish && npm run test",
|
||||||
"gen-doc": "jsdoc2md --src src/telegram.js -t README.hbs > README.md",
|
"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"
|
"pretest": "npm run eslint && npm run build"
|
||||||
},
|
},
|
||||||
"author": "Yago Pérez <yagoperezs@gmail.com>",
|
"author": "Yago Pérez <yagoperezs@gmail.com>",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user