Skip to content

Commit 1c3a409

Browse files
committed
initial commit
1 parent a8a7735 commit 1c3a409

File tree

10 files changed

+613
-0
lines changed

10 files changed

+613
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
15+
[*.txt]
16+
trim_trailing_whitespace = false

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["standard", "standard-react"]
3+
}

.gitignore

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
node_modules
28+
29+
# Optional npm cache directory
30+
.npm
31+
32+
# Optional REPL history
33+
.node_repl_history

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Telegram
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

examples/simple-bot.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var debug = require('debug')('telegraf:simple-bot')
2+
var Telegraf = require('../lib/app')
3+
4+
var app = new Telegraf('--BOT TOKEN--')
5+
6+
// Sample middleware
7+
var sayYoMiddleware = function * (next) {
8+
yield this.reply('yo')
9+
yield next
10+
}
11+
// Logger middleware
12+
app.use(function * (next) {
13+
var start = new Date
14+
this.state.started = start
15+
yield next
16+
var ms = new Date - start
17+
debug('message from: %s, time: %sms', ms)
18+
})
19+
20+
// Shared download middleware
21+
var downloadMiddleware = function * (next) {
22+
debug('Downloading...')
23+
this.state.downloaded = true
24+
yield next
25+
debug('Cleanup downloads...')
26+
}
27+
28+
// Middlewares, widdlewares everwhere
29+
app.on('video', downloadMiddleware, function * (next) {
30+
debug(this.state)
31+
})
32+
33+
// Random advice bot!
34+
app.on('text', function * (next) {
35+
if (Math.random() < 0.3) {
36+
return
37+
}
38+
yield this.reply('Highly advised to visit:')
39+
yield this.replyWithLocation((Math.random() * 180) - 90, (Math.random() * 180) - 90)
40+
})
41+
42+
// Text messages handling
43+
app.hears('/answer', sayYoMiddleware, function * () {
44+
this.reply('42')
45+
})
46+
47+
// Wow! RegEx
48+
app.hears(/reverse (.+)/, function * () {
49+
// Copy/Pasted from StackOverflow
50+
function reverse (s) {
51+
for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
52+
return o
53+
}
54+
this.reply(reverse(this.match[1]))
55+
})
56+
57+
app.startPolling()

0 commit comments

Comments
 (0)