telegram_notifications 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e858008d1ae12f788e2ec3f4fcac5827220ee82
4
+ data.tar.gz: 8e7a4fee96ef7e564f754eb5fab6cfe270de05a2
5
+ SHA512:
6
+ metadata.gz: 54e0efa527f37d12322ba6502be7701e4621c534edd481725c4908f73e4ac445bde6f462a10bbd47fc7ff2adaded7eccd002ae913937a68e4a1a1a7dc8fa344f
7
+ data.tar.gz: 4a6facb1f7f8c6ca1e860521f502581d55fc3c9cae3f70a30e78d9cb80516f97bc6e684dab56b381c66074a34134c87ee0d56544ea98278aa14e6b48c31c8ea5
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ nbproject/*
12
+ bin
13
+
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at hasanbasher1989@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in telegram_notifications.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
+ '~> 4.2.5'
13
+ else
14
+ "~> #{rails_version}"
15
+ end
16
+
17
+ gem 'rails', rails
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Hasan Basheer
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # TelegramNotifications
2
+
3
+ TelegramNotifications enables your Rails app to send notifications/messages to your users via Telegram's Bot API
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'telegram_notifications'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install telegram_notifications
21
+
22
+ ## Usage
23
+
24
+ TelegramNotifications uses a telegram_users table to store all users. To
25
+ generate and run the migration just use.
26
+
27
+ rails generate telegram_notifications:migration
28
+
29
+ This will also generate a config file in ```config/initializers/telegram_notifications.rb```,model "telegram_user.rb" and "" .
30
+
31
+
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hbasheer/telegram_notifications. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
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
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ post '/kkk' => 'telegram_notifications/subscribe#webhook'
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module TelegramNotifications
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ desc "Generates files for TelegramNotifications gem"
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/create_telegram_notifications.rb'
28
+ end
29
+ end
30
+
31
+ def copy_initializer
32
+ copy_file 'telegram_notifications.rb', 'config/initializers/telegram_notifications.rb'
33
+ copy_file 'subscribe_controller.rb', 'app/controllers/subscribe_controller.rb'
34
+ copy_file 'telegram_user.rb', 'app/models/telegram_user.rb'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ class TelegramNotificationsMigration < 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 :last_name
8
+ t.string :username
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :telegram_users, :telegram_id
14
+ end
15
+
16
+ def self.down
17
+ drop_table :telegram_users
18
+ end
19
+
20
+ end
@@ -0,0 +1,11 @@
1
+ class SubscribeController < ApplicationController
2
+ def webhook
3
+ if params[:message]
4
+ user = TelegramUser.create( { telegram_id: params[:message][:from][:id], first_name: params[:message][:from][:first_name],last_name: params[:message][:from][:last_name] } )
5
+ if user
6
+ user.send_message("Notifications are now active. To cancel, stop this bot in Telegram.")
7
+ end
8
+ render :nothing => true, :status => :ok
9
+ end
10
+ end
11
+ 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
+ TelegramNotifications::TelegramUser.configure_production_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
+ TelegramNotifications::TelegramUser.configure_development_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
+ TelegramNotifications::TelegramUser.configure_token("YOUR TOKEN")
@@ -0,0 +1,49 @@
1
+ class TelegramUser < ActiveRecord::Base
2
+
3
+ validates_presence_of :telegram_id
4
+ validates_uniqueness_of :telegram_id
5
+
6
+
7
+ def self.configure_production_url(url)
8
+ @production_url = url
9
+ end
10
+
11
+ def self.configure_development_url(url)
12
+ @devolopment_url = url
13
+ end
14
+
15
+ def self.active_url
16
+ if Rails.env.production?
17
+ @production_url
18
+ else
19
+ @devolopment_url
20
+ end
21
+ end
22
+
23
+
24
+ def self.configure_token(token)
25
+ if token =~ /^[0-9]+:[\w-]+$/
26
+ @@token = token
27
+ @@url = "https://api.telegram.org/bot" + token + "/"
28
+ @@callback_url = active_url + "/" + @@token
29
+ RestClient.post(@@url + "setWebhook", { url: @@callback_url })
30
+ else
31
+ raise "Invalid token! Please add a valid Telegram token in config/initializers/telegram_notifications.rb "
32
+ end
33
+ end
34
+
35
+ def self.send_message_to_all(text)
36
+ success = true
37
+ TelegramNotifications::TelegramUser.all.each do |user|
38
+ success = false if !user.send_message(text)
39
+ end
40
+ success
41
+ end
42
+
43
+
44
+ def send_message(text)
45
+ response = JSON.parse(RestClient.post(@@url + "sendMessage", chat_id: self.telegram_id, text: text), { symbolize_names: true })
46
+ response[:ok]
47
+ end
48
+
49
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_record'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ module TelegramNotifications
6
+ if defined?(ActiveRecord::Base)
7
+ require 'telegram_notifications/telegram_user'
8
+ require 'telegram_notifications/telegram_controller'
9
+ end
10
+ end
11
+
@@ -0,0 +1,3 @@
1
+ module TelegramNotifications
2
+ class Engine < Rails::Engine; end
3
+ end
@@ -0,0 +1,13 @@
1
+ module TelegramNotifications
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,54 @@
1
+ module TelegramNotifications
2
+ class TelegramUser < ::ActiveRecord::Base
3
+
4
+ validates_presence_of :telegram_id
5
+ validates_uniqueness_of :telegram_id
6
+
7
+ @@next_update_id = 0
8
+
9
+
10
+ def self.configure_production_url(url)
11
+ @production_url = url
12
+ end
13
+
14
+ def self.configure_development_url(url)
15
+ @devolopment_url = url
16
+ end
17
+
18
+ def self.active_url
19
+ if Rails.env.production?
20
+ @production_url
21
+ else
22
+ @devolopment_url
23
+ end
24
+ end
25
+
26
+
27
+ def self.configure_token(token)
28
+ if token =~ /^[0-9]+:[\w-]+$/
29
+ @@token = token
30
+ @@url = "https://api.telegram.org/bot" + token + "/"
31
+ @@callback_url = active_url + "/" + @@token
32
+ RestClient.post(@@url + "setWebhook", { url: @@callback_url })
33
+ else
34
+ raise "Invalid token! Please add a valid Telegram token in config/initializers/telegram_notifications.rb "
35
+ end
36
+ end
37
+
38
+ def self.send_message_to_all(text)
39
+ success = true
40
+ TelegramNotifications::TelegramUser.all.each do |user|
41
+ success = false if !user.send_message(text)
42
+ end
43
+ success
44
+ end
45
+
46
+
47
+ def send_message(text)
48
+ response = JSON.parse(RestClient.post(@@url + "sendMessage", chat_id: self.telegram_id, text: text), { symbolize_names: true })
49
+ response[:ok]
50
+ end
51
+
52
+
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module TelegramNotifications
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'telegram_notifications/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "telegram_notifications"
8
+ spec.version = TelegramNotifications::VERSION
9
+ spec.authors = ["Hasan Basheer"]
10
+ spec.email = ["hasanbasher1989@gmail.com"]
11
+
12
+ spec.summary = "Rails gem to send notifications via Telegram"
13
+ spec.description = "telegram_notifications enables your Rails app to send notifications/messages to your users via Telegram's Bot API."
14
+ spec.homepage = "https://github.com/hbasheer/telegram_notifications"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ #end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegram_notifications
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hasan Basheer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: telegram_notifications enables your Rails app to send notifications/messages
42
+ to your users via Telegram's Bot API.
43
+ email:
44
+ - hasanbasher1989@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - config/routes.rb
56
+ - lib/generators/telegram_notifications/migration/migration_generator.rb
57
+ - lib/generators/telegram_notifications/migration/templates/active_record/migration.rb
58
+ - lib/generators/telegram_notifications/migration/templates/active_record/subscribe_controller.rb
59
+ - lib/generators/telegram_notifications/migration/templates/active_record/telegram_notifications.rb
60
+ - lib/generators/telegram_notifications/migration/templates/active_record/telegram_user.rb
61
+ - lib/telegram_notifications.rb
62
+ - lib/telegram_notifications/engine.rb
63
+ - lib/telegram_notifications/telegram_controller.rb
64
+ - lib/telegram_notifications/telegram_user.rb
65
+ - lib/telegram_notifications/version.rb
66
+ - telegram_notifications.gemspec
67
+ homepage: https://github.com/hbasheer/telegram_notifications
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.5.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Rails gem to send notifications via Telegram
91
+ test_files: []