Created
April 20, 2016 17:09
-
-
Save par6n/55797c4f9c52817f719a1a5290ad2a67 to your computer and use it in GitHub Desktop.
An inline bot for formatting text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TelegramBot = require( 'node-telegram-bot-api' ); | |
var token = 'XXXX'; | |
var bot = new TelegramBot( token, { polling: true } ); | |
var formats = {}; | |
var waitingFor = {}; | |
var messageText = {}; | |
bot.on( 'inline_query', function( query ) { | |
if ( query.query == '' ) return; | |
bot.answerInlineQuery( query.id, [ | |
{ 'type': 'article', 'id': '1', 'title': 'Markdown', 'message_text': query.query, 'parse_mode': 'Markdown', 'hide_url': true, 'description': 'قسمت کوچکی از فرمت Markdown در دسترس است. *بولد* و _ایتالیک_ و [لینک](http://link.com)' }, | |
{ 'type': 'article', 'id': '2', 'title': 'HTML', 'message_text': query.query, 'parse_mode': 'HTML', 'hide_url': true, description: 'نوع محدودی از HTML در دسترس است. تگ های: <b>,<em>,<a href=""></a>' }, | |
] ); | |
} ); | |
bot.onText( /\/start/, function( msg ) { | |
bot.sendMessage( msg.chat.id, `✋🏻 سلام | |
من ربات هایپرلینک و HTML هستم، کار من تبدیل متن های خام به متن HTML یا Markdownه. | |
👈🏻 برای شروع، یه متن خام برام بفرستید تا اونو برات سبک دهیش کنم. بعد از اینکه متنت رو فرستادی، اونو با پیام OK ریپلایش کن. | |
✨ برای تعیین این که میخوای از چه سبکی استفاده کنی، از /setformat استفاده کن. | |
💚 همچنین میتونی عکس/فیلم/GIF هم برام بفرستی تا برات زیرنویس بزارم. | |
💜 اگه هم دوست داری میتونی متن هایی با سبک HTML یا Markdown توی گروه ها با تایپ @MyFirstInlineBot و نوشتن متنت بنویسی.`, { parse_mode: 'Markdown' } ); | |
} ); | |
bot.onText( /\/setformat/, function( msg ) { | |
waitingFor[msg.from.id] = 'format'; | |
bot.sendMessage( msg.chat.id, `فرمت موردنظرت رو انتخاب کن`, { | |
'reply_to_message_id' : msg['message_id'], | |
'reply_markup': JSON.stringify( { 'keyboard': [ [ 'Markdown', 'HTML' ] ], 'one_time_keyboard': true, 'resize_keyboard': true, 'selective': true, 'force_reply': true } ) | |
} ); | |
} ); | |
bot.onText( /HTML/, function( msg ) { | |
if ( waitingFor[msg.from.id] != 'format' ) | |
return; | |
formats[ msg.from.id ] = 'HTML'; | |
bot.sendMessage( msg.chat.id, `فرمت به *HTML* تغییر کرد.`, { | |
'parse_mode': 'Markdown', | |
'reply_markup': JSON.stringify( { 'hide_keyboard': true } ) | |
} ); | |
waitingFor[msg.from.id] = false; | |
} ); | |
bot.onText( /Markdown/, function( msg ) { | |
if ( waitingFor[msg.from.id] != 'format' ) | |
return; | |
formats[ msg.from.id ] = 'Markdown'; | |
bot.sendMessage( msg.chat.id, `فرمت به *Markdown* تغییر کرد.`, { | |
'parse_mode': 'Markdown', | |
'reply_markup': JSON.stringify( { 'hide_keyboard': true } ) | |
} ); | |
waitingFor[msg.from.id] = false; | |
} ); | |
bot.on( 'text', function( msg ) { | |
if ( msg.text != 'OK' ) | |
return; | |
var format = 'Markdown'; | |
if ( formats[ msg.from.id ] ) | |
format = formats[ msg.from.id ]; | |
if ( msg.reply_to_message ) { | |
bot.sendMessage( msg.chat.id, msg.reply_to_message.text, { 'parse_mode': format } ); | |
} | |
} ); | |
bot.on( 'text', function( msg ) { | |
if ( ! msg.reply_to_message ) | |
return; | |
if ( msg.reply_to_message.photo ) { | |
if ( msg.text.length > 200 ) { | |
bot.sendMessage( msg.chat.id, `شرمنده! زیرنویس نمیتونه بیش از 200تا حرف باشه`, { reply_to_message_id: msg['message_id'] } ); | |
return; | |
} | |
bot.sendPhoto( msg.chat.id, msg.reply_to_message.photo[0]['file_id'], { caption: msg.text } ); | |
} | |
if ( msg.reply_to_message.video ) { | |
if ( msg.text.length > 200 ) { | |
bot.sendMessage( msg.chat.id, `شرمنده! زیرنویس نمیتونه بیش از 200تا حرف باشه`, { reply_to_message_id: msg['message_id'] } ); | |
return; | |
} | |
bot.sendVideo( msg.chat.id, msg.reply_to_message.video['file_id'], { caption: msg.text } ); | |
} | |
if ( msg.reply_to_message.document ) { | |
if ( msg.text.length > 200 ) { | |
bot.sendMessage( msg.chat.id, `شرمنده! زیرنویس نمیتونه بیش از 200تا حرف باشه`, { reply_to_message_id: msg['message_id'] } ); | |
return; | |
} | |
bot.sendDocument( msg.chat.id, msg.reply_to_message.document['file_id'], { caption: msg.text } ); | |
} | |
} ); | |
bot.on( 'photo', function( msg ) { | |
bot.sendMessage( msg.chat.id, 'حالا به عکست متن موردنظرت رو ریپلای کن.', { 'reply_to_message_id': msg['message_id'] } ); | |
} ); | |
bot.on( 'video', function( msg ) { | |
bot.sendMessage( msg.chat.id, 'حالا به ویدیوت متن موردنظرت رو ریپلای کن.', { 'reply_to_message_id': msg['message_id'] } ); | |
} ); | |
bot.on( 'document', function( msg ) { | |
bot.sendMessage( msg.chat.id, 'حالا به فایلت متن موردنظرت رو ریپلای کن.', { 'reply_to_message_id': msg['message_id'] } ); | |
} ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "inline", | |
"version": "1.0.0", | |
"description": "Inline bot", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"node-telegram-bot-api": "^0.20.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment