tele_notify 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +17 -0
- data/README.markdown +123 -0
- data/Rakefile +34 -0
- data/lib/generators/tele_notify/migration/migration_generator.rb +35 -0
- data/lib/generators/tele_notify/migration/templates/active_record/migration.rb +19 -0
- data/lib/generators/tele_notify/migration/templates/active_record/tele_notify.rb +15 -0
- data/lib/tele_notify.rb +12 -0
- data/lib/tele_notify/engine.rb +3 -0
- data/lib/tele_notify/telegram_controller.rb +13 -0
- data/lib/tele_notify/telegram_user.rb +56 -0
- data/lib/tele_notify/version.rb +3 -0
- data/tele_notify.gemspec +27 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bbe50097b22b33be87df0e943fd2e9d73299e60a
|
4
|
+
data.tar.gz: 3aa23a75e190c554ed96ad13b12c81d26cbbba12
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4879a4cdcf1abcd3facfe76fe70f0e17b036ae6c700ab773d17077be1d3e23b56f50977422c6144ea3fdab7b972492eced586ed04d99efa3e85f3f896a688005
|
7
|
+
data.tar.gz: 41bff14abafba193d77e8a5ab0c2a11604e15f3307879a1d448bfc3b9959847b2b25d5dcb81100de4c947e49b99638e56463f92a9fbdab862c24886476d65664
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in acts_as_votable.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
rails_version = ENV['RAILS_VERSION'] || 'default'
|
7
|
+
|
8
|
+
rails = case rails_version
|
9
|
+
when 'master'
|
10
|
+
{ :github => 'rails/rails'}
|
11
|
+
when 'default'
|
12
|
+
'~> 3.2.0'
|
13
|
+
else
|
14
|
+
"~> #{rails_version}"
|
15
|
+
end
|
16
|
+
|
17
|
+
gem 'rails', rails
|
data/README.markdown
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# TeleNotify
|
2
|
+
|
3
|
+
TeleNotify by [ppati000](http://twitter.com/ppati000) enables your Rails app to send notifications/messages to your users via Telegram's Bot API. Forget email newsletters!
|
4
|
+
|
5
|
+
A working demo can be tested [here](http://telenotify.herokuapp.com/). For the source, see the [TeleTest repository](https://github.com/ppati000/TeleTest/)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Note: For any news, questions, suggestions or anything else, contact and/or follow me on [Twitter](http://twitter.com/ppati000) or send me a [mail](mailto:ppati000@me.com)
|
10
|
+
|
11
|
+
### Prerequisites
|
12
|
+
|
13
|
+
Needless to say, you need a Telegram bot account first. To generate a new Telegram bot, message [@botfather](https://telegram.me/botfather) on Telegram. For more information, see the [official guide](https://core.telegram.org/bots#botfather).
|
14
|
+
During this process, you will obtain a token which you will need in the following steps.
|
15
|
+
|
16
|
+
### Install
|
17
|
+
|
18
|
+
Just add the following to your Gemfile.
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
#Gemfile
|
22
|
+
gem 'tele_notify', '~> 0.1'
|
23
|
+
```
|
24
|
+
|
25
|
+
And follow that up with a ``bundle install``.
|
26
|
+
|
27
|
+
### Setup and Database Migrations
|
28
|
+
|
29
|
+
TeleNotify uses a telegram_users table to store all users. To
|
30
|
+
generate and run the migration just use.
|
31
|
+
|
32
|
+
rails generate tele_notify:migration
|
33
|
+
|
34
|
+
This will also generate a config file in ```config/initializers/tele_notify.rb```. Make sure to set your Telegram token and application URLs here. It should look something like this:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
#Set your home URL, so Telegram callbacks work
|
38
|
+
#For production, just use your URL (e.g. https://myapp.com)
|
39
|
+
#You MUST NOT include a trailing slash and it MUST be https!
|
40
|
+
#INVALID URLS: e.g. http://myapp.com or https://myapp.com/
|
41
|
+
TeleNotify::TelegramUser.configure_home_url("YOUR PRODUCTION URL")
|
42
|
+
|
43
|
+
#For development, download ngrok from https://ngrok.com/.
|
44
|
+
#Extract it and run "./ngrok http 3000"
|
45
|
+
#Then copy the URL you get from the console window.
|
46
|
+
#Remember to use the HTTPS URL!
|
47
|
+
TeleNotify::TelegramUser.configure_dev_url("YOUR NGROK DEVELOPMENT URL")
|
48
|
+
|
49
|
+
#Set your Telegram Bot API token here
|
50
|
+
#Don't have your token yet? Create your bot using https://telegram.me/botfather
|
51
|
+
TeleNotify::TelegramUser.configure_token("YOUR TOKEN")
|
52
|
+
```
|
53
|
+
|
54
|
+
Next, add two lines of code to make your ApplicationController look like this:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
#app/controllers/application_controller.rb
|
58
|
+
class ApplicationController < ActionController::Base
|
59
|
+
protect_from_forgery with: :exception
|
60
|
+
|
61
|
+
#IMPORTANT! THESE TWO LINES MUST COME AFTER protect_from_forgery!
|
62
|
+
skip_before_filter :verify_authenticity_token, :only => :webhook
|
63
|
+
include TeleNotify::Controller
|
64
|
+
|
65
|
+
#other code...
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
Last but not least, add a callback URL for Telegram in ```config/routes.rb```, which must be the same as your token.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
#config/routes.rb
|
73
|
+
Rails.application.routes.draw do
|
74
|
+
post '/<your token>' => 'application#webhook'
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
Now that everything is done, you can finally run ```rake db:migrate```. Congratulations! Yout successfully installed TeleNotify!
|
79
|
+
|
80
|
+
## Using TeleNotify
|
81
|
+
|
82
|
+
To give users the ability to sign up for your Telegram Notifications, put a link to your telegram bot somewhere in your app. This can be as simple as
|
83
|
+
|
84
|
+
```html
|
85
|
+
<a href="https://telegram.me/YOUR_BOT_USERNAME">Send us a message to receive Push Notifications via Telegram!"</a>
|
86
|
+
```
|
87
|
+
|
88
|
+
Any user that sends a message to your bot will be stored in the database with their telegram_id and their first_name.
|
89
|
+
|
90
|
+
Send messages to those users like this: (e.g. from some controller in your app)
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
#in some controller action
|
94
|
+
|
95
|
+
#sends message to a user
|
96
|
+
TeleNotify::TelegramUser.find(1).send_message("How Obama won the internet: Read more at http://example.com")
|
97
|
+
|
98
|
+
#finds a user by his Telegram ID and sends him a message
|
99
|
+
TeleNotify::TelegramUser.find_by(telegram_id: 12345678).send_message("Some other notification")
|
100
|
+
|
101
|
+
#sends a message to everyone who signed up
|
102
|
+
TeleNotify::TelegramUser.send_message_to_all("Check out our new stuff!")
|
103
|
+
|
104
|
+
```
|
105
|
+
|
106
|
+
## Testing
|
107
|
+
|
108
|
+
All testing is currently here: [TeleTest repository](https://github.com/ppati000/TeleTest/)
|
109
|
+
|
110
|
+
## Changes
|
111
|
+
|
112
|
+
0.1
|
113
|
+
|
114
|
+
* Initial release
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
Fork it. Commit your changes. Make a pull request :)
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
TeleNotify was initially forked from [Acts as votable](https://github.com/ryanto/acts_as_votable) by ryanto. It is released under the [MIT
|
123
|
+
License](http://www.opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'TeleNotify'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
module TeleNotify
|
4
|
+
class MigrationGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
desc "Generates migration for votable (votes table)"
|
8
|
+
|
9
|
+
def self.orm
|
10
|
+
Rails::Generators.options[:rails][:orm]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.source_root
|
14
|
+
File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.orm_has_migration?
|
18
|
+
[:active_record].include? orm
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.next_migration_number(path)
|
22
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_migration_file
|
26
|
+
if self.class.orm_has_migration?
|
27
|
+
migration_template 'migration.rb', 'db/migrate/tele_notify_migration.rb'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def copy_initializer
|
32
|
+
copy_file 'tele_notify.rb', 'config/initializers/tele_notify.rb'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class TeleNotifyMigration < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
create_table :telegram_users do |t|
|
5
|
+
t.integer :telegram_id
|
6
|
+
t.string :first_name
|
7
|
+
t.string :username
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :telegram_users, :telegram_id
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :telegram_users
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#Set your home URL, so Telegram callbacks work
|
2
|
+
#For production, just use your URL (e.g. https://myapp.com)
|
3
|
+
#You MUST NOT include a trailing slash and it MUST be https!
|
4
|
+
#INVALID URLS: e.g. http://myapp.com or https://myapp.com/
|
5
|
+
TeleNotify::TelegramUser.configure_home_url("YOUR PRODUCTION URL")
|
6
|
+
|
7
|
+
#For development, download ngrok from https://ngrok.com/.
|
8
|
+
#Extract it and run "./ngrok http 3000"
|
9
|
+
#Then copy the URL you get from the console window.
|
10
|
+
#Remember to use the HTTPS URL!
|
11
|
+
TeleNotify::TelegramUser.configure_dev_url("YOUR NGROK DEVELOPMENT URL")
|
12
|
+
|
13
|
+
#Set your Telegram Bot API token here
|
14
|
+
#Don't have your token yet? Create your bot using https://telegram.me/botfather
|
15
|
+
TeleNotify::TelegramUser.configure_token("YOUR TOKEN")
|
data/lib/tele_notify.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module TeleNotify
|
2
|
+
module Controller
|
3
|
+
def webhook
|
4
|
+
if params[:message]
|
5
|
+
user = TelegramUser.create( { telegram_id: params[:message][:from][:id], first_name: params[:message][:from][:first_name] } )
|
6
|
+
if user
|
7
|
+
user.send_message("Notifications are now active. To cancel, stop this bot in Telegram.")
|
8
|
+
end
|
9
|
+
render :nothing => true, :status => :ok
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module TeleNotify
|
4
|
+
class TelegramUser < ::ActiveRecord::Base
|
5
|
+
|
6
|
+
validates_presence_of :telegram_id
|
7
|
+
validates_uniqueness_of :telegram_id
|
8
|
+
|
9
|
+
@@next_update_id = 0
|
10
|
+
|
11
|
+
def self.configure_home_url(url)
|
12
|
+
@@home_url = url
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure_dev_url(url)
|
16
|
+
@@dev_url = url
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.active_url
|
20
|
+
if Rails.env.production?
|
21
|
+
@@home_url
|
22
|
+
else
|
23
|
+
@@dev_url
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def self.configure_token(token)
|
29
|
+
if token =~ /^[0-9]+:[\w-]+$/ #hacker proof
|
30
|
+
@@token = token
|
31
|
+
@@url = "https://api.telegram.org/bot" + token + "/"
|
32
|
+
@@callback_url = active_url + "/" + @@token
|
33
|
+
RestClient.post(@@url + "setWebhook", { url: @@callback_url })
|
34
|
+
else
|
35
|
+
raise "Invalid token! Please add a valid Telegram token in config/initializers/tele_notify.rb or see https://github.com/ppati000/tele_notify for further instructions."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.send_message_to_all(text)
|
40
|
+
success = true
|
41
|
+
TeleNotify::TelegramUser.all.each do |user|
|
42
|
+
success = false if !user.send_message(text)
|
43
|
+
end
|
44
|
+
success
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def send_message(text)
|
49
|
+
response = JSON.parse(RestClient.post(@@url + "sendMessage", chat_id: self.telegram_id, text: text), { symbolize_names: true })
|
50
|
+
response[:ok]
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/tele_notify.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "tele_notify/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "tele_notify"
|
7
|
+
s.version = TeleNotify::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["ppati000", "Ryan"]
|
10
|
+
s.email = ["ppati000@me.com"]
|
11
|
+
s.homepage = "https://github.com/ppati000/tele_notify"
|
12
|
+
s.summary = "Rails gem to send notifications via Telegram"
|
13
|
+
s.description = "TeleNotify by ppati000 enables your Rails app to send notifications/messages to your users via Telegram's Bot API. Forget email newsletters!
|
14
|
+
|
15
|
+
* See installation instructions, demo app and source on https://github.com/ppati000/tele_notify. *"
|
16
|
+
|
17
|
+
s.rubyforge_project = "acts_as_votable"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
|
24
|
+
s.add_dependency "rest-client", "~> 2.0.0.rc1"
|
25
|
+
|
26
|
+
s.add_development_dependency "sqlite3", '~> 1.3.9'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tele_notify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ppati000
|
8
|
+
- Ryan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-07-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 2.0.0.rc1
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 2.0.0.rc1
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: sqlite3
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.3.9
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.3.9
|
42
|
+
description: |-
|
43
|
+
TeleNotify by ppati000 enables your Rails app to send notifications/messages to your users via Telegram's Bot API. Forget email newsletters!
|
44
|
+
|
45
|
+
* See installation instructions, demo app and source on https://github.com/ppati000/tele_notify. *
|
46
|
+
email:
|
47
|
+
- ppati000@me.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- ".gitignore"
|
53
|
+
- Gemfile
|
54
|
+
- README.markdown
|
55
|
+
- Rakefile
|
56
|
+
- lib/generators/tele_notify/migration/migration_generator.rb
|
57
|
+
- lib/generators/tele_notify/migration/templates/active_record/migration.rb
|
58
|
+
- lib/generators/tele_notify/migration/templates/active_record/tele_notify.rb
|
59
|
+
- lib/tele_notify.rb
|
60
|
+
- lib/tele_notify/engine.rb
|
61
|
+
- lib/tele_notify/telegram_controller.rb
|
62
|
+
- lib/tele_notify/telegram_user.rb
|
63
|
+
- lib/tele_notify/version.rb
|
64
|
+
- tele_notify.gemspec
|
65
|
+
homepage: https://github.com/ppati000/tele_notify
|
66
|
+
licenses: []
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project: acts_as_votable
|
84
|
+
rubygems_version: 2.4.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Rails gem to send notifications via Telegram
|
88
|
+
test_files: []
|