telegram-webhooks 0.1.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 +7 -0
- data/lib/telegram.rb +31 -0
- data/lib/telegram/bot.rb +24 -0
- data/lib/telegram/chat.rb +25 -0
- data/lib/telegram/message.rb +36 -0
- data/lib/telegram/update.rb +21 -0
- data/lib/telegram/user.rb +19 -0
- metadata +64 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: ed5d036d01bde71e42d38b13d03a7e5c00e0950b
|
|
4
|
+
data.tar.gz: 299d4f61442d2c12dac11983bf10e4ba8d2cec89
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9e038cdb1646acdb7cb8339e577ad9a558646bf68c143920d2c4cb6c7ceaff76d95eb915a4af49624ff6501510aff75d88ea329a73888f7b61c691a26fe3ff5f
|
|
7
|
+
data.tar.gz: e38d3bfbf1dd14d454957049ecde4d51598fa0424e0a7f525a281dc6526418f538b98ee5f8b4a5a2e11227b135c0172247e666ff36a93024ce7b9a266db14e19
|
data/lib/telegram.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
require 'telegram/bot'
|
|
6
|
+
require 'telegram/chat'
|
|
7
|
+
require 'telegram/message'
|
|
8
|
+
require 'telegram/update'
|
|
9
|
+
require 'telegram/user'
|
|
10
|
+
|
|
11
|
+
# Is a representation of Telegram API
|
|
12
|
+
module Telegram
|
|
13
|
+
@telegram_token = ''
|
|
14
|
+
|
|
15
|
+
def self.token=(telegram_token)
|
|
16
|
+
@telegram_token = telegram_token
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.method_missing(method_name, *args, &_block)
|
|
20
|
+
uri = URI("https://api.telegram.org/bot#{@telegram_token}/#{method_name}")
|
|
21
|
+
Net::HTTP.post_form(uri, args.first)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.path_verified(path)
|
|
25
|
+
path.start_with? "/#{@telegram_token}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.parse_update(raw_update)
|
|
29
|
+
Update.from_hash JSON.parse(raw_update)
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/telegram/bot.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Encapsulates the Bot behavior, inherit your app from Bot class
|
|
4
|
+
class Bot
|
|
5
|
+
def call(env)
|
|
6
|
+
return [403, {}, []] unless Telegram.path_verified env['PATH_INFO']
|
|
7
|
+
|
|
8
|
+
update = Telegram.parse_update(env['rack.input'].read)
|
|
9
|
+
|
|
10
|
+
self.class.commands.each do |command, block|
|
|
11
|
+
instance_exec(update, &block) if update.message.contains_command?(command)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
[200, {}, []]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.commands
|
|
18
|
+
@commands ||= {}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.on(command, &block)
|
|
22
|
+
commands[:"#{command}"] = block
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Is a representation of Telegrams Chat object
|
|
4
|
+
class Chat
|
|
5
|
+
attr_reader :id, :type, :title, :username, :first_name, :last_name
|
|
6
|
+
|
|
7
|
+
def self.from_hash(chat)
|
|
8
|
+
return nil unless chat.is_a? Hash
|
|
9
|
+
|
|
10
|
+
Chat.new(id: chat['id'], type: chat['type'], title: chat['title'], username: chat['username'], first_name: chat['first_name'], last_name: chat['last_name'])
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize(id:, type:, title: nil, username: nil, first_name: nil, last_name: nil)
|
|
14
|
+
@id = id
|
|
15
|
+
@type = type
|
|
16
|
+
@title = title
|
|
17
|
+
@username = username
|
|
18
|
+
@first_name = first_name
|
|
19
|
+
@last_name = last_name
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def reply(text, opts = {})
|
|
23
|
+
Telegram.sendMessage opts.merge(chat_id: id, text: text)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Is a representation of Telegrams Message object
|
|
4
|
+
class Message
|
|
5
|
+
attr_reader :chat, :text
|
|
6
|
+
|
|
7
|
+
def self.from_hash(message)
|
|
8
|
+
return nil unless message.is_a? Hash
|
|
9
|
+
|
|
10
|
+
chat = Chat.from_hash message['chat']
|
|
11
|
+
from = User.from_hash message['from']
|
|
12
|
+
|
|
13
|
+
Message.new(message_id: message['message_id'], from: from, chat: chat, text: message['text'])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(message_id:, from:nil, chat:, text:)
|
|
17
|
+
@message_id = message_id
|
|
18
|
+
@from = from
|
|
19
|
+
@chat = chat
|
|
20
|
+
@text = text
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def contains_command?(command)
|
|
24
|
+
if command.is_a?(Symbol) && text.split(' ').first.eql?("/#{command}")
|
|
25
|
+
true
|
|
26
|
+
elsif text.start_with?(command.to_s)
|
|
27
|
+
true
|
|
28
|
+
else
|
|
29
|
+
false
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def reply(text, opts = {})
|
|
34
|
+
chat.reply text, opts.merge(reply_to_message_id: @message_id)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Is a representation of Telegrams Update object
|
|
4
|
+
class Update
|
|
5
|
+
attr_reader :message
|
|
6
|
+
|
|
7
|
+
def self.from_hash(update)
|
|
8
|
+
return nil unless update.is_a? Hash
|
|
9
|
+
|
|
10
|
+
message = Message.from_hash update['message']
|
|
11
|
+
|
|
12
|
+
Update.new(update_id: update['update_id'], message: message)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(update_id:, message: nil, inline_query: nil, chosen_inline_result: nil)
|
|
16
|
+
@update_id = update_id
|
|
17
|
+
@message = message
|
|
18
|
+
@inline_query = inline_query
|
|
19
|
+
@chosen_inline_result = chosen_inline_result
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Is a representation of Telegrams user object
|
|
4
|
+
class User
|
|
5
|
+
attr_reader :first_name, :last_name, :username
|
|
6
|
+
|
|
7
|
+
def self.from_hash(user)
|
|
8
|
+
return nil unless user.is_a? Hash
|
|
9
|
+
|
|
10
|
+
User.new(id: user['id'], first_name: user['first_name'], last_name: user['last_name'], username: user['username'])
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize(id:, first_name:, last_name:nil, username:nil)
|
|
14
|
+
@id = id
|
|
15
|
+
@first_name = first_name
|
|
16
|
+
@last_name = last_name
|
|
17
|
+
@username = username
|
|
18
|
+
end
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: telegram-webhooks
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Steffen Schröder
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rack
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 1.6.4
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 1.6.4
|
|
27
|
+
description: This project gives you a nice DSL-like interface for the Telegram Bot
|
|
28
|
+
API
|
|
29
|
+
email: steffen@schroeder-blog.de
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- "./lib/telegram.rb"
|
|
35
|
+
- "./lib/telegram/bot.rb"
|
|
36
|
+
- "./lib/telegram/chat.rb"
|
|
37
|
+
- "./lib/telegram/message.rb"
|
|
38
|
+
- "./lib/telegram/update.rb"
|
|
39
|
+
- "./lib/telegram/user.rb"
|
|
40
|
+
homepage: https://github.com/ChaosSteffen/telegram-webhooks
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata: {}
|
|
44
|
+
post_install_message:
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubyforge_project:
|
|
60
|
+
rubygems_version: 2.5.1
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 4
|
|
63
|
+
summary: A DSL-like interface for the Telegram messengers Bot API
|
|
64
|
+
test_files: []
|