Giter VIP home page Giter VIP logo

tele_notify's Introduction

TeleNotify

TeleNotify by ppati000 enables your Rails app to send notifications/messages to your users via Telegram's Bot API. Forget email newsletters!

A working demo can be tested here. For the source, see the TeleTest repository

Installation

Note: For any news, questions, suggestions or anything else, contact and/or follow me on Twitter or send me a mail

Prerequisites

Needless to say, you need a Telegram bot account first. To generate a new Telegram bot, message @botfather on Telegram. For more information, see the official guide. During this process, you will obtain a token which you will need in the following steps.

Your app must have HTTPS enabled.

Install

Just add the following to your Gemfile.

#Gemfile
gem 'tele_notify', '~> 0.1.1'

And follow that up with a bundle install.

Setup and Database Migrations

TeleNotify uses a telegram_users table to store all users. To generate and run the migration just use.

rails generate tele_notify:migration

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:

#Set your home URL, so Telegram callbacks work
#For production, just use your URL (e.g. https://myapp.com)
#You MUST NOT include a trailing slash and it MUST be https!
#INVALID URLS: e.g. http://myapp.com or https://myapp.com/
TeleNotify::TelegramUser.configure_home_url("YOUR PRODUCTION URL")

#For development, download ngrok from https://ngrok.com/.
#Extract it and run "./ngrok http 3000"
#Then copy the URL you get from the console window.
#Remember to use the HTTPS URL!
TeleNotify::TelegramUser.configure_dev_url("YOUR NGROK DEVELOPMENT URL")

#Set your Telegram Bot API token here
#Don't have your token yet? Create your bot using https://telegram.me/botfather
TeleNotify::TelegramUser.configure_token("YOUR TOKEN")

Set webhook, so bot will know where to send user data:

https://api.telegram.org/bot<TOKEN>/setWebHook?url=https://<EXAMPLE.COM>/<TOKEN>
{"ok":true,"result":true,"description":"Webhook was set"}

Next, add two lines of code to make your ApplicationController look like this:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  #IMPORTANT! THESE TWO LINES MUST COME AFTER protect_from_forgery!
  skip_before_filter :verify_authenticity_token, :only => :webhook
  include TeleNotify::Controller

  #other code...
end

Last but not least, add a callback URL for Telegram in config/routes.rb, which must be the same as your token.

#config/routes.rb
Rails.application.routes.draw do
  post '/<your token>' => 'application#webhook'
end

Now that everything is done, you can finally run bundle exec rake db:migrate and restart server. Congratulations! You successfully installed TeleNotify!

Using TeleNotify

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

<a href="https://telegram.me/YOUR_BOT_USERNAME">Send us a message to receive Push Notifications via Telegram!"</a>

Any user that sends a message to your bot will be stored in the database with their telegram_id and their first_name.

Send messages to those users like this: (e.g. from some controller in your app)

#in some controller action

#sends message to a user
TeleNotify::TelegramUser.find(1).send_message("How Obama won the internet: Read more at http://example.com")

#finds a user by his Telegram ID and sends him a message
TeleNotify::TelegramUser.find_by(telegram_id: 12345678).send_message("Some other notification")

#sends a message to everyone who signed up
TeleNotify::TelegramUser.send_message_to_all("Check out our new stuff!")

Testing

All testing is currently here: TeleTest repository

Changes

0.1.1

  • Initial release

Contributing

Fork it. Commit your changes. Make a pull request :)

License

TeleNotify was initially forked from Acts as votable by ryanto. It is released under the MIT License.

tele_notify's People

Contributors

ryanto avatar ppati000 avatar michaelwhi avatar leizzer avatar petergoldstein avatar halilim avatar jeewes avatar kondratenko-valeriy avatar fltiago avatar mnort9 avatar foood avatar astratto avatar strivedi183 avatar sj26 avatar sbycrosz avatar phlipper avatar polopi avatar ejholmes avatar elado avatar csun avatar 1v avatar jastkand avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.