├── .npmignore ├── src ├── index.js ├── utils.js ├── bot.js ├── http-client.js ├── keyboards-and-buttons.js ├── events.js ├── telegram-api.js ├── updater.js └── context.js ├── docs ├── reply.gif ├── send-img.gif ├── custom-btns.gif ├── menu-exemple-1.png ├── the-bot-father.jpg ├── submenu-exemple.gif └── custom-inline-btns.gif ├── .github └── workflows │ └── npmpublish.yml ├── allmethods ├── package.json ├── .gitignore ├── README.md └── LICENSE /.npmignore: -------------------------------------------------------------------------------- 1 | docs/ 2 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | Bot: require('./bot'), 3 | } 4 | -------------------------------------------------------------------------------- /docs/reply.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/reply.gif -------------------------------------------------------------------------------- /docs/send-img.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/send-img.gif -------------------------------------------------------------------------------- /docs/custom-btns.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/custom-btns.gif -------------------------------------------------------------------------------- /docs/menu-exemple-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/menu-exemple-1.png -------------------------------------------------------------------------------- /docs/the-bot-father.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/the-bot-father.jpg -------------------------------------------------------------------------------- /docs/submenu-exemple.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/submenu-exemple.gif -------------------------------------------------------------------------------- /docs/custom-inline-btns.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tawsbob/telegram-bot-maker/HEAD/docs/custom-inline-btns.gif -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | const callbackDataStringify = (callback_id, params) => `${callback_id}|${JSON.stringify(params)}` 2 | 3 | const callbackDataParse = callback_data => { 4 | const withParams = callback_data.split('|') 5 | let params = null 6 | 7 | if (withParams.length > 1) { 8 | params = JSON.parse(withParams[1]) 9 | return { callback_data: withParams[0], params } 10 | } 11 | 12 | return { callback_data, params } 13 | } 14 | 15 | module.exports = { 16 | callbackDataStringify, 17 | callbackDataParse, 18 | } 19 | -------------------------------------------------------------------------------- /src/bot.js: -------------------------------------------------------------------------------- 1 | require('./events') 2 | 3 | const Updater = require('./updater') 4 | const Telegram = require('./telegram-api') 5 | 6 | const { Events } = global 7 | 8 | class Bot extends Telegram { 9 | constructor(props) { 10 | super(props) 11 | this.Updater = new Updater(props) 12 | Events.setContexProps(props) 13 | } 14 | 15 | lauch() { 16 | this.Updater.lauch() 17 | } 18 | 19 | stop() { 20 | this.Updater.stop() 21 | } 22 | 23 | command(command, handler) { 24 | Events.command(command, handler) 25 | } 26 | 27 | on(listener, handler) { 28 | Events.on(listener, handler) 29 | } 30 | } 31 | 32 | module.exports = Bot 33 | -------------------------------------------------------------------------------- /.github/workflows/npmpublish.yml: -------------------------------------------------------------------------------- 1 | name: Publish to npm 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v1 12 | - uses: actions/setup-node@v1 13 | with: 14 | node-version: 12 15 | - run: npm ci 16 | - run: npm run prettier 17 | 18 | publish-npm: 19 | needs: build 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: actions/checkout@v1 23 | - uses: actions/setup-node@v1 24 | with: 25 | node-version: 12 26 | registry-url: https://registry.npmjs.org/ 27 | - run: npm ci 28 | - run: npm publish 29 | env: 30 | NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}} 31 | -------------------------------------------------------------------------------- /allmethods: -------------------------------------------------------------------------------- 1 | [ 2 | "getMe", 3 | "sendMessage", 4 | "Formatting options", 5 | "forwardMessage", 6 | "sendPhoto", 7 | "sendAudio", 8 | "sendDocument", 9 | "sendVideo", 10 | "sendAnimation", 11 | "sendVoice", 12 | "sendVideoNote", 13 | "sendMediaGroup", 14 | "sendLocation", 15 | "editMessageLiveLocation", 16 | "stopMessageLiveLocation", 17 | "sendVenue", 18 | "sendContact", 19 | "sendPoll", 20 | "sendChatAction", 21 | "getUserProfilePhotos", 22 | "getFile", 23 | "kickChatMember", 24 | "unbanChatMember", 25 | "restrictChatMember", 26 | "promoteChatMember", 27 | "exportChatInviteLink", 28 | "setChatPhoto", 29 | "deleteChatPhoto", 30 | "setChatTitle", 31 | "setChatDescription", 32 | "pinChatMessage", 33 | "unpinChatMessage", 34 | "leaveChat", 35 | "getChat", 36 | "getChatAdministrators", 37 | "getChatMembersCount", 38 | "getChatMember", 39 | "setChatStickerSet", 40 | "deleteChatStickerSet", 41 | "answerCallbackQuery", 42 | "Inline mode methods" 43 | ] 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "telegram-bot-maker", 3 | "version": "1.0.3", 4 | "description": "nodejs telegram api wrapper", 5 | "main": "./src/index.js", 6 | "scripts": { 7 | "prepare": "npm run prettier", 8 | "prettier": "prettier --single-quote --trailing-comma es5 --no-semi --print-width 120 --write \"**/*.js\"" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/tawsbob/telegram-bot-maker.git" 13 | }, 14 | "keywords": [ 15 | "telegram", 16 | "nodejs", 17 | "api", 18 | "bot" 19 | ], 20 | "author": "Dellean Santos", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/tawsbob/telegram-bot-maker/issues" 24 | }, 25 | "husky": { 26 | "hooks": { 27 | "pre-commit": "npm run prettier" 28 | } 29 | }, 30 | "homepage": "https://github.com/tawsbob/telegram-bot-maker#readme", 31 | "devDependencies": { 32 | "husky": "^3.0.0", 33 | "prettier": "^1.18.2" 34 | }, 35 | "dependencies": { 36 | "auto-bind": "^2.1.0", 37 | "form-data": "^2.5.0", 38 | "got": "^9.6.0", 39 | "query-string": "^6.8.1" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | *.DS_Store 64 | *test.js 65 | -------------------------------------------------------------------------------- /src/http-client.js: -------------------------------------------------------------------------------- 1 | const got = require('got') 2 | const FormData = require('form-data') 3 | const fs = require('fs') 4 | 5 | const defaults = { 6 | timeout: 20000, 7 | json: true, 8 | } 9 | 10 | //arranjar um jeito de melhorar esses spreads 11 | const buildOptions = opts => { 12 | const { file, ...rest } = opts 13 | 14 | if (file) { 15 | const { filePath, type, url } = file 16 | 17 | if (filePath) { 18 | const { method, url, body, ...aditional } = rest 19 | const form = new FormData() 20 | Object.keys(body).forEach(att => { 21 | form.append(att, body[att]) 22 | }) 23 | form.append(type, fs.createReadStream(filePath)) 24 | 25 | return { method, url, ...aditional, ...defaults, body: form, json: undefined } 26 | } 27 | 28 | if (url) { 29 | const { method, url, body, ...aditional } = rest 30 | const _body = { 31 | [type]: url, 32 | ...body, 33 | } 34 | return { method, url, ...aditional, ...defaults, body: _body } 35 | } 36 | } 37 | 38 | return { ...opts, ...defaults } 39 | } 40 | 41 | const client = opts => { 42 | const requestParams = buildOptions(opts) 43 | return got(requestParams) 44 | } 45 | 46 | module.exports = client 47 | -------------------------------------------------------------------------------- /src/keyboards-and-buttons.js: -------------------------------------------------------------------------------- 1 | const { callbackDataStringify, callbackDataParse } = require('./utils') 2 | 3 | const build = Events => { 4 | const Keyboard = (type = 'inline', buttons, opts) => { 5 | if (type === 'inline') { 6 | return { reply_markup: { inline_keyboard: buttons } } 7 | } 8 | 9 | const options = opts ? opts : { resize_keyboard: true, one_time_keyboard: true } 10 | return { reply_markup: { keyboard: buttons, ...options } } 11 | } 12 | 13 | class Buttons { 14 | withParams(params, callback_id) { 15 | if (params && typeof params === 'object') { 16 | return callbackDataStringify(callback_id, params) 17 | } 18 | return callback_id 19 | } 20 | 21 | CallBack(text, callback_id, params, handdler, hide = false) { 22 | const callback_data = params ? this.withParams(params, callback_id) : callback_id 23 | 24 | if (handdler) { 25 | if (Events) { 26 | Events.setCallback_query(callback_data, handdler) 27 | } 28 | } 29 | return { 30 | text, 31 | callback_data, 32 | hide, 33 | } 34 | } 35 | Keyboard(text, opts = null) { 36 | return { 37 | text, 38 | ...opts, 39 | } 40 | } 41 | } 42 | 43 | return { 44 | Buttons: new Buttons(), 45 | Keyboard, 46 | } 47 | } 48 | 49 | module.exports = build 50 | -------------------------------------------------------------------------------- /src/events.js: -------------------------------------------------------------------------------- 1 | const autoBind = require('auto-bind') 2 | const Context = require('./context') 3 | const buildKeyboardAndButtons = require('./keyboards-and-buttons') 4 | const { callbackDataParse } = require('./utils') 5 | 6 | class Events { 7 | constructor() { 8 | const { Keyboard, Buttons } = buildKeyboardAndButtons(this) 9 | this.listeners = { 10 | message: null, 11 | update: null, 12 | command: [], 13 | callback_query: [], 14 | reply: [], 15 | middleware: [], 16 | } 17 | this.contextProps = null 18 | this.Keyboard = Keyboard 19 | this.Buttons = Buttons 20 | autoBind(this) 21 | } 22 | 23 | setContexProps(props) { 24 | this.contextProps = props 25 | } 26 | 27 | replaceRepliesListeners(listeners) { 28 | this.listeners.reply = listeners 29 | } 30 | 31 | onUpdate(updates) { 32 | if (this.listeners.update) { 33 | this.listeners.update(updates) 34 | } 35 | } 36 | 37 | callMiddlewares(update) { 38 | const length = this.listeners.middleware.length 39 | 40 | for (let i = 0; i < length; i++) { 41 | this.listeners.middleware[i](update) 42 | } 43 | } 44 | 45 | removeReplyListenersFromThisRef(ref) { 46 | if (ref) { 47 | const { chat_id, from_id } = ref 48 | this.listeners.reply = this.listeners.reply.reduce((acc, l) => { 49 | if (chat_id == l.ref.chat_id && from_id == l.ref.from_id) { 50 | //console.log('removendo listenerS') 51 | } else { 52 | acc.push(l) 53 | } 54 | 55 | return acc 56 | }, []) 57 | } 58 | } 59 | 60 | triggerCallBackQueryListeners(data, update) { 61 | const length = this.listeners.callback_query.length 62 | for (let i = 0; i < length; i++) { 63 | if (this.listeners.callback_query[i].data === data) { 64 | this.listeners.callback_query[i].handdler(callbackDataParse(this.listeners.callback_query[i].data)) 65 | } 66 | } 67 | } 68 | 69 | triggerMsgListener(update) { 70 | if (this.listeners.message) { 71 | this.listeners.message(this.newContex(update), update) 72 | } 73 | } 74 | 75 | emit(command, update) { 76 | const length = this.listeners.command.length 77 | for (let i = 0; i < length; i++) { 78 | if (this.listeners.command[i].command === command) { 79 | const { setReplyListener } = this 80 | this.listeners.command[i].handdler(this.newContex(update), update) 81 | } 82 | } 83 | } 84 | 85 | command(command, handdler) { 86 | this.listeners.command.push({ command, handdler }) 87 | } 88 | 89 | setCallback_query(data, handdler) { 90 | this.listeners.callback_query.push({ data, handdler }) 91 | } 92 | 93 | setReplyListener(ref, handdler, addUpdate) { 94 | this.removeReplyListenersFromThisRef(ref) 95 | this.listeners.reply.push({ ref, handdler, addUpdate }) 96 | } 97 | 98 | on(listener, handdler) { 99 | if (this.listeners[listener] !== 'undefined') { 100 | this.listeners[listener] = handdler 101 | } 102 | } 103 | 104 | newContex(update) { 105 | const { setReplyListener, Keyboard, Buttons } = this 106 | const contextProps = { ...this.contextProps, update, setReplyListener, Keyboard, Buttons } 107 | return new Context(contextProps) 108 | } 109 | } 110 | 111 | module.exports = global.Events = new Events() 112 | -------------------------------------------------------------------------------- /src/telegram-api.js: -------------------------------------------------------------------------------- 1 | const queryString = require('query-string') 2 | const autoBind = require('auto-bind') 3 | const client = require('./http-client') 4 | 5 | class Telegram { 6 | constructor({ token }) { 7 | this.baseUrl = 'https://api.telegram.org/' 8 | this.baseBotUrl = `${this.baseUrl}bot${token}/` 9 | autoBind(this) 10 | } 11 | 12 | async apiCall({ method, params, endpoint }) { 13 | try { 14 | const url = this.baseBotUrl + endpoint 15 | const { body } = await client({ url, method, ...params }) 16 | 17 | //thats is necessary because GOT (http request module) cant work with JSON module when body is form data 18 | const jsonResult = typeof body == 'string' ? JSON.parse(body) : body 19 | 20 | if (jsonResult && jsonResult.result) { 21 | return jsonResult.result 22 | } 23 | 24 | return null 25 | } catch (e) { 26 | console.warn(e) 27 | return null 28 | } 29 | } 30 | 31 | getUpdate(params) { 32 | return this.apiCall({ endpoint: `getUpdates?${queryString.stringify(params)}`, method: 'get' }) 33 | } 34 | getMe() { 35 | return this.apiCall({ endpoint: 'getMe', method: 'get' }) 36 | } 37 | sendMessage(params) { 38 | return this.apiCall({ endpoint: 'sendMessage', method: 'post', params }) 39 | } 40 | forwardMessage(params) { 41 | return this.apiCall({ endpoint: 'forwardMessage', method: 'post', params }) 42 | } 43 | sendPhoto(params) { 44 | return this.apiCall({ endpoint: 'sendPhoto', method: 'post', params }) 45 | } 46 | sendAudio(params) { 47 | return this.apiCall({ endpoint: 'sendAudio', method: 'post', params }) 48 | } 49 | sendDocument(params) { 50 | return this.apiCall({ endpoint: 'sendDocument', method: 'post', params }) 51 | } 52 | sendVideo(params) { 53 | return this.apiCall({ endpoint: 'sendVideo', method: 'post', params }) 54 | } 55 | sendAnimation(params) { 56 | return this.apiCall({ endpoint: 'sendAnimation', method: 'post', params }) 57 | } 58 | sendVoice(params) { 59 | return this.apiCall({ endpoint: 'sendVoice', method: 'post', params }) 60 | } 61 | sendVideoNote(params) { 62 | return this.apiCall({ endpoint: 'sendVideoNote', method: 'post', params }) 63 | } 64 | sendMediaGroup(params) { 65 | return this.apiCall({ endpoint: 'sendMediaGroup', method: 'post', params }) 66 | } 67 | 68 | sendLocation(params) { 69 | return this.apiCall({ endpoint: 'sendLocation', method: 'post', params }) 70 | } 71 | 72 | editMessageLiveLocation(params) { 73 | return this.apiCall({ endpoint: 'editMessageLiveLocation', method: 'post', params }) 74 | } 75 | 76 | stopMessageLiveLocation(params) { 77 | return this.apiCall({ endpoint: 'stopMessageLiveLocation', method: 'post', params }) 78 | } 79 | 80 | sendVenue(params) { 81 | return this.apiCall({ endpoint: 'sendVenue', method: 'post', params }) 82 | } 83 | 84 | sendContact(params) { 85 | return this.apiCall({ endpoint: 'sendContact', method: 'post', params }) 86 | } 87 | 88 | sendPoll(params) { 89 | return this.apiCall({ endpoint: 'sendPoll', method: 'post', params }) 90 | } 91 | sendChatAction(params) { 92 | return this.apiCall({ endpoint: 'sendChatAction', method: 'post', params }) 93 | } 94 | 95 | getUserProfilePhotos(params) { 96 | return this.apiCall({ endpoint: `getUserProfilePhotos?${queryString.stringify(params)}`, method: 'get' }) 97 | } 98 | 99 | editMessageText(params) { 100 | return this.apiCall({ endpoint: `editMessageText`, method: 'post', params }) 101 | } 102 | } 103 | 104 | module.exports = Telegram 105 | -------------------------------------------------------------------------------- /src/updater.js: -------------------------------------------------------------------------------- 1 | const Telegram = require('./telegram-api') 2 | 3 | const { Events } = global 4 | 5 | class Updater extends Telegram { 6 | constructor(props) { 7 | super(props) 8 | const { updateInterval, updateLimit } = props 9 | this.pollingTimeout = null 10 | this.updateInterval = updateInterval || 350 11 | this.updateLimit = updateLimit || 100 12 | this.started = false 13 | this.offset = 0 14 | this.isInitial = true 15 | } 16 | 17 | stop() { 18 | this.started = false 19 | clearTimeout(this.pollingTimeout) 20 | } 21 | 22 | lauch() { 23 | this.started = true 24 | this.updateTrigger() 25 | } 26 | 27 | updateTrigger() { 28 | this.pollingTimeout = setTimeout(this.lookingForUpdates, this.updateInterval) 29 | } 30 | 31 | async lookingForUpdates(isInitial) { 32 | if (!this.started) { 33 | return 34 | } 35 | 36 | try { 37 | const { offset } = this 38 | const updates = await this.getUpdate({ offset, limit: this.updateLimit }) 39 | 40 | if (updates && updates.length) { 41 | //https://core.telegram.org/bots/api#getting-updates 42 | //Must be greater by one than the highest among the identifiers of previously received updates 43 | this.offset = updates[updates.length - 1].update_id + 1 44 | } 45 | 46 | //ignore all updates while bot is offline to prevent bugs 47 | if (!this.isInitial) { 48 | if (updates && updates.length) { 49 | Events.onUpdate(updates) 50 | this.check(updates) 51 | } 52 | } 53 | 54 | this.isInitial = false 55 | 56 | this.updateTrigger() 57 | } catch (e) { 58 | console.warn(e) 59 | } 60 | } 61 | 62 | check(updates) { 63 | const length = updates.length 64 | 65 | for (let i = 0; i < length; i++) { 66 | Events.callMiddlewares(updates[i]) 67 | this.checkUpdate(updates[i], updates.length - 1 == i) 68 | } 69 | } 70 | 71 | checkUpdate(update, isLast) { 72 | if (update.callback_query) { 73 | const { data } = update.callback_query 74 | Events.triggerCallBackQueryListeners(data, update) 75 | } 76 | 77 | if (update.message) { 78 | const { message } = update 79 | const { text, entities } = message 80 | 81 | //commands are trigger here 82 | const isCommand = this.checkEntities(entities, text, update) 83 | 84 | if (!isCommand) { 85 | //se tem reply e se é para o usuário correto 86 | this.makeReply(update, isLast) 87 | } 88 | } 89 | } 90 | 91 | updateMatchRef(update, ref) { 92 | return ( 93 | ref.message_id < update.message.message_id && 94 | ref.chat_id == update.message.chat.id && 95 | ref.from_id == update.message.from.id 96 | ) 97 | } 98 | 99 | matchRef(ref, _ref) { 100 | return ref.message_id == _ref.message_id && ref.chat_id == _ref.chat_id && ref.from_id == _ref.from_id 101 | } 102 | 103 | makeReply(update, isLast) { 104 | const length = Events.listeners.reply.length 105 | let mustReply = false 106 | 107 | let toDelete = [] 108 | 109 | for (let i = 0; i < length; i++) { 110 | const replyListener = Events.listeners.reply[i] 111 | 112 | if (update.message && replyListener.ref && this.updateMatchRef(update, replyListener.ref)) { 113 | mustReply = true 114 | replyListener.handdler(update) 115 | replyListener.addUpdate(update) 116 | toDelete.push(replyListener.ref) 117 | } 118 | } 119 | 120 | if (!mustReply) { 121 | Events.triggerMsgListener(update) 122 | } 123 | 124 | if (isLast) { 125 | const lastIndex = Events.listeners.reply.length - 1 126 | 127 | const FilteredListeners = Events.listeners.reply.reduce((acc, listener) => { 128 | const isInDeleteList = toDelete.reduce((_acc, _ref) => { 129 | if (this.matchRef(listener.ref, _ref)) return acc 130 | }, false) 131 | 132 | if (!isInDeleteList) { 133 | acc.push(listener) 134 | } 135 | 136 | return acc 137 | }, []) 138 | 139 | Events.replaceRepliesListeners(FilteredListeners) 140 | } 141 | } 142 | 143 | checkEntities(entities, text, update) { 144 | let isCommand = false 145 | if (entities && entities.length) { 146 | const length = entities.length 147 | for (let i = 0; i < length; i++) { 148 | if (entities[i].type === 'bot_command') { 149 | Events.emit(text.trim(), update) 150 | isCommand = true 151 | } 152 | } 153 | } 154 | 155 | return isCommand 156 | } 157 | } 158 | 159 | module.exports = Updater 160 | -------------------------------------------------------------------------------- /src/context.js: -------------------------------------------------------------------------------- 1 | const Telegram = require('./telegram-api') 2 | 3 | class Context extends Telegram { 4 | constructor(props) { 5 | const { update, setReplyListener, Keyboard, Buttons } = props 6 | super(props) 7 | this.state = {} 8 | this.updates = [update] 9 | this.lastBotMsg = null 10 | this.menuMarkups = [] 11 | this.replyListeners = [] 12 | this._addUpdate = this.addUpdate.bind(this) 13 | this.setReplyListener = setReplyListener 14 | ;(this.keyboard = Keyboard), (this.buttons = Buttons) 15 | } 16 | 17 | setState(stateProps) { 18 | this.state = { 19 | ...this.state, 20 | ...stateProps, 21 | } 22 | } 23 | 24 | getState() { 25 | return this.state 26 | } 27 | 28 | getType() { 29 | const { callback_query, message } = this.getLastUpdate() 30 | if (callback_query) { 31 | return 'callback_query' 32 | } 33 | 34 | if (message) { 35 | return 'message' 36 | } 37 | 38 | return null 39 | } 40 | 41 | addUpdate(update) { 42 | this.updates.push(update) 43 | } 44 | 45 | getLastUpdate() { 46 | return this.updates[this.updates.length - 1] 47 | } 48 | 49 | getInsideObj() { 50 | return this.getLastUpdate()[this.getType()] 51 | } 52 | 53 | getFromId() { 54 | return this.getInsideObj().from.id 55 | } 56 | 57 | getChatId() { 58 | const { chat, message } = this.getInsideObj() 59 | 60 | if (chat) { 61 | return chat.id 62 | } 63 | 64 | if (message.chat.id) { 65 | return message.chat.id 66 | } 67 | } 68 | 69 | ref() { 70 | //new error if not message_id 71 | const { message_id } = this.lastBotMsg 72 | 73 | return { 74 | message_id, 75 | chat_id: this.getChatId(), 76 | from_id: this.getFromId(), 77 | } 78 | } 79 | 80 | contextParams(params) { 81 | const { file, ...rest } = params 82 | 83 | if (file) { 84 | return { 85 | file, 86 | body: { 87 | chat_id: this.getChatId(), 88 | ...rest, 89 | }, 90 | } 91 | } 92 | 93 | return { 94 | body: { 95 | chat_id: this.getChatId(), 96 | ...params, 97 | }, 98 | } 99 | } 100 | 101 | reply(text, params) { 102 | this.sendMessage(this.contextParams({ text, ...params })) 103 | .then(this.afterBotReply) 104 | .catch(this.onError) 105 | return this 106 | } 107 | 108 | replyWithImage(params) { 109 | this.sendPhoto(this.contextParams(params)) 110 | .then(this.afterBotReply) 111 | .catch(this.onError) 112 | return this 113 | } 114 | 115 | editMsgWithKeyboard(text, params) { 116 | const { message_id } = this.lastBotMsg 117 | this.editMessageText( 118 | this.contextParams({ 119 | text, 120 | message_id, 121 | ...params, 122 | }) 123 | ) 124 | .then(this.afterBotReply) 125 | .catch(this.onError) 126 | return this 127 | } 128 | 129 | backClick(opts) { 130 | const menuId = opts.id 131 | 132 | const lastMenu = this.menuMarkups.reduce((acc, menu) => { 133 | if (menu.id == menuId) acc = menu 134 | return acc 135 | }, null) 136 | 137 | const { text, markup } = lastMenu 138 | 139 | this.editMsgWithKeyboard(text, markup) 140 | } 141 | 142 | subMenuReply(text, menu) { 143 | return () => { 144 | this.editMsgWithKeyboard(text, menu) 145 | } 146 | } 147 | 148 | buildButton(opts, isBack) { 149 | const { label, id, params = null, onSelect, hide, submenu } = opts 150 | 151 | if (submenu) { 152 | const { text } = submenu 153 | const _submenuMarkUp = this.buildMenu(submenu) 154 | return this.buttons.CallBack(label, id, params, this.subMenuReply(text, _submenuMarkUp), hide) 155 | } 156 | 157 | if (isBack) { 158 | return this.buttons.CallBack( 159 | label, 160 | id, 161 | params, 162 | () => { 163 | this.backClick(opts) 164 | }, 165 | hide 166 | ) 167 | } 168 | 169 | return this.buttons.CallBack(label, id, params, onSelect, hide) 170 | } 171 | 172 | buildGrid(grid, options, backButton) { 173 | const rowsAndCols = grid.split('x') 174 | const rows = parseInt(rowsAndCols[0]) 175 | const cols = parseInt(rowsAndCols[1]) || 0 176 | 177 | let filledRows = 0 178 | let filledCols = 0 179 | 180 | const elements = options.reduce((acc, opts, i) => { 181 | if (filledRows < rows) { 182 | if (!acc.length) { 183 | acc.push([this.buildButton(opts)]) 184 | } else { 185 | acc[filledCols].push(this.buildButton(opts)) 186 | } 187 | filledRows++ 188 | 189 | if (filledRows == rows) { 190 | filledRows = 0 191 | filledCols++ 192 | if (i < options.length - 1) { 193 | acc.push([]) 194 | } 195 | } 196 | 197 | if (i == options.length - 1) { 198 | if (backButton) { 199 | acc.push([this.buildButton(backButton, true)]) 200 | } 201 | } 202 | } 203 | 204 | return acc 205 | }, []) 206 | 207 | return elements 208 | } 209 | 210 | buildMenu(MenuConfiguration) { 211 | const { options, grid, backButton, text, id } = MenuConfiguration 212 | const menu = this.buildGrid(grid, options, backButton) 213 | const markup = this.keyboard('inline', menu) 214 | 215 | this.menuMarkups.push({ text, id, markup }) 216 | return markup 217 | } 218 | 219 | replyWithMenu(MenuConfiguration) { 220 | const { text } = MenuConfiguration 221 | const markup = this.buildMenu(MenuConfiguration) 222 | this.reply(text, markup) 223 | } 224 | 225 | afterBotReply(lastBotMsg) { 226 | this.lastBotMsg = lastBotMsg 227 | this.triggerBotReply() 228 | } 229 | 230 | triggerBotReply() { 231 | if (this.replyListeners.length) { 232 | this.setReplyListener(this.ref(), this.replyListeners[0], this._addUpdate) 233 | this.clearFirstReplyListener() 234 | } 235 | } 236 | 237 | clearFirstReplyListener() { 238 | if (this.replyListeners.length > 1) { 239 | this.replyListeners = this.replyListeners.filter((l, i) => i > 0) 240 | } else { 241 | this.replyListeners = [] 242 | } 243 | } 244 | 245 | waitForReply(listener) { 246 | this.replyListeners.push(listener) 247 | return this 248 | } 249 | 250 | onError(err) { 251 | console.warn(err) 252 | } 253 | } 254 | 255 | module.exports = Context 256 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # telegram-bot-maker 2 | 3 | 4 |
5 | 6 |
7 | 8 | A nodejs lightweight library wrapper to [telegram bot api](https://core.telegram.org/bots/api) 9 | 10 | ## Learn More about telegram bots 11 | 12 | if you want learn more about [Telegram bots](https://core.telegram.org/bots) 13 | 14 | ## How did it come about? 15 | 16 | After try make may own bot with currents nodejs libraries ( Telegraf and others) and face some issues like: 17 | 18 | - Uncaught exception 19 | - Bugs, memory leaks 20 | - About weeks to accept pull request with bugfix 21 | - Very verbose to work listening to answers and creating buttons / menus 22 | 23 | That module arose from my need to create a bot, so I needed to quickly develop a module that would meet my needs, so I didn't bother to implement all the methods / features available in the telegram API. 24 | 25 | **My only concern was to develop code that didn't break the application.** 26 | 27 | #### If you want to help implement new features or improve code quality, your help is most welcome! 😊 28 | 29 | ## Requirements 30 | 31 | - Nodejs V8 + 32 | 33 | ## Limitations 34 | It's only for long polling bot, **Webhook** will be implemented soon. Feel free to help us! 35 | 36 | ## Install 37 | 38 | ```bash 39 | $ npm install telegram-bot-maker 40 | ``` 41 | 42 | ## Usage 43 | 44 | ```javascript 45 | 46 | const { Bot } = require('telegram-bot-maker') 47 | 48 | const bot = new Bot({ 49 | token: 'YOUR-BOT-TOKEN', 50 | updateInterval: 500, // interval that bot will looking for updates 51 | }) 52 | 53 | bot.lauch() 54 | 55 | ``` 56 | 57 | ## Bot Methods 58 | 59 | Start the bot 60 | ```javascript 61 | bot.lauch() 62 | ``` 63 | 64 | Stop the bot 65 | ```javascript 66 | bot.stop() 67 | ``` 68 | 69 | Add listener to command 70 | ```javascript 71 | // * Is Required 72 | // command * 73 | // handler * 74 | bot.command(command, handler) 75 | 76 | //exemple 77 | bot.command('/menu', (ctx, update) => { 78 | ctx.reply('There is a reply to menu command') 79 | }) 80 | 81 | ``` 82 | 83 | Add listener to bot 84 | ```javascript 85 | // * Is Required 86 | // listener * 'message' || 'update' 87 | // handler * 88 | bot.on(listener, handler) 89 | 90 | //exemple 91 | bot.on('message', (ctx, update) => { 92 | ctx.reply('A reply to your msg') 93 | }) 94 | 95 | ``` 96 | 97 | 98 | ## Wait For User reply 99 | ![Wait for user reply](https://github.com/tawsbob/telegram-bot-maker/blob/master/docs/reply.gif?raw=true) 100 | 101 | ctx.waitForReply(handler) 102 | 103 | ```javascript 104 | 105 | bot.on('message', (ctx, update) => { 106 | 107 | ctx 108 | .reply('Whats is your first name?') 109 | .waitForReply((userReply)=>{ 110 | const { message } = userReply 111 | ctx.setState({ firstName: message.text }) 112 | ctx.reply(`nice ${message.text}, so whats is your last name?`) 113 | }) 114 | .waitForReply((userReply)=>{ 115 | const { message } = userReply 116 | const { firstName } = ctx.getState() 117 | ctx.reply(`your full name is ${firstName} ${message.text}`) 118 | }) 119 | }) 120 | 121 | ``` 122 | 123 | ## Creating Menu 124 | 125 | ![Menu 2x1](https://github.com/tawsbob/telegram-bot-maker/blob/master/docs/menu-exemple-1.png?raw=true) 126 | 127 | ctx.replyWithMenu(MenuSettings) 128 | 129 | ```javascript 130 | 131 | bot.on('message', (ctx, update) => { 132 | ctx.replyWithMenu({ 133 | text: 'Menu Level 0', // text ( Text of message ) 134 | grid: '2x1', // grid *Required 'ColsxRow' ( Grid of the Menu ) 135 | id: 'id-menu-0', // id *Required (Id of menu, without it backButton wont work) 136 | options: [ 137 | { 138 | label: 'Button 1', // label *Required (Label of button) 139 | id: 'btn-1', // id *Required (Id of button) 140 | params: { 'foo': 'bar' },// params (Your custom params if you need) 141 | onSelect: params => { 142 | console.log('Button 1 click', params) 143 | //Output: Button 1 click { 'foo': 'bar' } 144 | 145 | }, // onSelect *Required (Called when user press the button) 146 | }, 147 | { 148 | label: 'Button 2', 149 | id: 'btn-2', 150 | onSelect: params => { 151 | console.log('Button 2 click') 152 | }, 153 | }, 154 | ], 155 | }) 156 | 157 | }) 158 | 159 | ``` 160 | 161 | ## Submenu 162 | ![Menu 1x1](https://github.com/tawsbob/telegram-bot-maker/blob/master/docs/submenu-exemple.gif?raw=true) 163 | 164 | ```javascript 165 | 166 | bot.on('message', (ctx, update) => { 167 | ctx.replyWithMenu({ 168 | text: 'Menu Level 0', 169 | grid: '2x1', 170 | id: 'id-menu-0', 171 | options: [ 172 | { 173 | label: 'Button 1', 174 | id: 'btn-1', 175 | params: { 'my-custom-params': 'my-custom-value' }, 176 | onSelect: params => { 177 | console.log('Button 1 click', params) 178 | }, 179 | }, 180 | { 181 | label: 'Button 2', 182 | id: 'btn-2', 183 | onSelect: params => { 184 | console.log('Button 2 click') 185 | }, 186 | submenu: { 187 | text: 'Menu Level 1', 188 | grid: '1x1', 189 | id: 'id-menu-1', 190 | backButton: { 191 | label: 'Back to level zero menu', 192 | id: 'id-menu-0', 193 | }, // Add at the bottom of grid a back button 194 | options: [ 195 | { 196 | label: 'Button 3', 197 | id: 'btn-3', 198 | onSelect: params => { 199 | console.log('Button 3 click') 200 | }, 201 | }, 202 | { 203 | label: 'Button 4', 204 | id: 'btn-4', 205 | onSelect: params => { 206 | console.log('Button 4 click') 207 | }, 208 | }, 209 | ] 210 | } 211 | }, 212 | ], 213 | }) 214 | }) 215 | 216 | ``` 217 | 218 | ## Custom Inline Buttons 219 | 220 | ![Custom inline btn](https://github.com/tawsbob/telegram-bot-maker/blob/master/docs/custom-inline-btns.gif?raw=true) 221 | 222 | ```javascript 223 | 224 | bot.on('message', (ctx, update) => { 225 | ctx.reply( 226 | 'Testing custom BTNS', 227 | ctx.keyboard('inline', [ 228 | [ 229 | ctx.buttons.CallBack('Button 1', 'id-btn-1', { params: 'to-btn-1' }, params => { 230 | console.log('User hit button 1', params) 231 | }) 232 | ], 233 | [ 234 | ctx.buttons.CallBack('Button 2', 'id-btn-2', { params: 'to-btn-2' }, params => { 235 | console.log('User hit button 2', params) 236 | }) 237 | ], 238 | ]) 239 | ) 240 | }) 241 | 242 | ``` 243 | 244 | ## Custom Buttons 245 | 246 | ![Custom btn](https://github.com/tawsbob/telegram-bot-maker/blob/master/docs/custom-btns.gif?raw=true) 247 | 248 | ```javascript 249 | 250 | bot.on('message', (ctx, update) => { 251 | ctx.reply( 252 | 'Testing custom BTNS', 253 | ctx.keyboard(null, [ //Just the inline params changed 254 | [ 255 | ctx.buttons.CallBack('Button 1', 'id-btn-1', { params: 'to-btn-1' }, params => { 256 | console.log('User hit button 1', params) 257 | }) 258 | ], 259 | [ 260 | ctx.buttons.CallBack('Button 2', 'id-btn-2', { params: 'to-btn-2' }, params => { 261 | console.log('User hit button 2', params) 262 | }) 263 | ], 264 | ]) 265 | ) 266 | }) 267 | 268 | ``` 269 | 270 | ## send Photos to user 271 | 272 | ![Send Image](https://github.com/tawsbob/telegram-bot-maker/blob/master/docs/send-img.gif?raw=true) 273 | 274 | To send photos to user you must pass url or filePath param. 275 | 276 | ```javascript 277 | ctx 278 | .reply('Want a photo?') 279 | .waitForReply(() => { 280 | ctx.replyWithImage({ 281 | file: { 282 | type: 'photo', // Is required 283 | //url: 'https://images.freeimages.com/images/large-previews/b31/butterfly-1392408.jpg', 284 | filePath: './docs/menu-exemple-1.png', 285 | }, 286 | }) 287 | }) 288 | ``` 289 | 290 | 291 | ## Availables Api Methods 292 | 293 | - getMe 294 | - sendMessage 295 | - forwardMessage 296 | - sendPhoto 297 | - sendAudio 298 | - sendDocument 299 | - sendVideo 300 | - sendAnimation 301 | - sendVoice 302 | - sendVideoNote 303 | - sendMediaGroup 304 | - sendLocation 305 | - editMessageLiveLocation 306 | - stopMessageLiveLocation 307 | - sendVenue 308 | - sendContact 309 | - sendPoll 310 | - sendChatAction 311 | - getUserProfilePhotos 312 | - editMessageText 313 | 314 | ## Notes 315 | All updates that remain when the bot is off will be bypassed, it will only react to updates that happen while it is alive, I choose for this architecture to prevent anomalous behavior. 316 | 317 | **This module is in its infancy and still relies on possible help to make it more robust.** 318 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------