vv-telegram
v0.2.1
Published
JavaScript (NodeJs): api for create telegram bot, based on https://github.com/telegraf/telegraf
Downloads
7
Readme
Install & Use
npm i vv-telegram
const os = require('os')
const vvt = require('vv-telegram')
let bot = vvt.create('YOUR_TOKEN_BOT', {cache: {hours_life: 3}})
let t1 = 'short_text'
let t2 = 'long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text_long_text'
bot.start(error => {
if (error) {
console.log(error)
process.exit()
}
console.log('BOT STARTED')
bot.on_error(error => {
console.log('ERROR IN BOT WORK:')
console.log(error)
})
bot.on_message(message => {
console.log(''.concat('message.message_id:', os.EOL, message.message_id, os.EOL, 'message.type_text:', os.EOL, message.type_text, os.EOL, 'message.text:', os.EOL, message.text))
//demo delete prev message when user click on button in prev message
let prev_file_find = false
let prev_message = bot.get_from_cache_receiving({account: message.account, chat_id: message.chat_id, message_id: message.message_id, type_text: 'callback_data'})
prev_message.forEach(m => {
bot.message_delete(message.chat_id, m.message_id)
prev_file_find = true
})
/** @type {vvt.package_sending_button[][]} */
let buttons = [
[
{text: 'b1', callback_data: '1_'.concat(t1)}, {text: 'b2', callback_data: '2_'.concat(t1)}, {text: 'b3', callback_data: '3_'.concat(t1)},
], [
{text: 'b4', callback_data: '4_'.concat(t2)}, {text: 'b5', callback_data: '5_'.concat(t2)}, {text: 'b6', callback_data: '6_'.concat(t2)},
]
]
let message_text = message.error ? 'ERROR: '.concat(message.error) : 'replay for '.concat(message.text)
bot.message_send(
message.account,
message_text,
{buttons: buttons, reply_to_message_id: prev_file_find ? undefined : message.message_id},
undefined,
(error, sending) => {
if (error) {
console.log(error)
return
}
//demo edit prev message
setTimeout(() => {
bot.message_send(
message.account,
'edited: '.concat(message_text),
{reply_to_message_id: message.message_id, buttons: buttons},
sending.message_id,
(error, sending) => {
if (error) {
console.log(error)
return
}
}
)
}, 5000)
}
)
})
})
Functions
Typedefs
create(token, [options]) ⇒ app
Kind: global function
| Param | Type | | --- | --- | | token | string | | [options] | type.options |
button_line_to_matrix(button_line, width) ⇒ Array.<Array.<package_sending_button>>
Kind: global function
| Param | Type | | --- | --- | | button_line | Array.<package_sending_button> | | width | number |
- button_line_to_matrix(button_line, width) ⇒ Array.<Array.<package_sending_button>>
- ~result : Array.<Array.<package_sending_button>>
- ~line : Array.<package_sending_button>
button_line_to_matrix~result : Array.<Array.<package_sending_button>>
Kind: inner typedef of button_line_to_matrix
button_line_to_matrix~line : Array.<package_sending_button>
Kind: inner typedef of button_line_to_matrix
bot : app
Kind: global typedef
package_sending_button : type.package_sending_button
Kind: global typedef
package_sending_menu : type.package_sending_menu
Kind: global typedef
package_sending_options : type.package_sending_options
Kind: global typedef
package_sending_text_format : type.package_sending_text_format
Kind: global typedef
package_receiving : type.package_receiving
Kind: global typedef
package_sending : type.package_sending
Kind: global typedef
package_sending_queue : type.package_sending_queue
Kind: global typedef
file_info : type.file_info
Kind: global typedef