telegramAPI 1.2.1 → 1.2.2

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +56 -4
  3. data/lib/telegramAPI.rb +13 -10
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf59e092f4b48681343b6e8141146e4fc12fcd42
4
- data.tar.gz: 1d57c5ae51722ed541fe5269b9524e979e1dafbf
3
+ metadata.gz: 533ace198fb97babf39504545d1e05142a95ab28
4
+ data.tar.gz: 9ba7f63de0c468d69b868673e593c1c289157536
5
5
  SHA512:
6
- metadata.gz: 0f9386146372a702adbf658e78d0a2ddc2d80e913c5e42caf083ee30a8bd84c303c97781a3c32191a306007fcd43ba76e000ea94c379d8318e3e063f65f3e923
7
- data.tar.gz: f81bf7b70bb806b63822c79cef72d64be62b6fee98068737e53a9b7ddc1fcbef9aa23784e78268541832543f44a02b74767f9fa1a29bd7e7b6e2c55c0da99031
6
+ metadata.gz: 2030bcbf930947303a459b60464d0984b73416b43bf8951cc57631c0fbd17b5107d357cac0dc1f868c96c4a2bd4820319b5d1a213bc705d28694c64c24655054
7
+ data.tar.gz: 6279be9813e5e94d511e9f5b3c04b1ff45e42daa2f5b884a07901be8c02feae5976cba8c4ef670645ad9c6e6ab05e296857edf5e18af2d078c74acc563c51441
data/README.md CHANGED
@@ -31,17 +31,69 @@ To test your access token, you can use the *getMe* method
31
31
  require 'telegramAPI'
32
32
 
33
33
  token = "******"
34
- api = TelegramAPI.new token
35
- bot = api.getMe
34
+ api = TelegramAPI.new(token)
35
+ bot = api.getMe()
36
36
  puts "I'm bot #{bot['first_name']} with id #{bot['id']}"
37
37
  puts "But you can call me @#{bot['username']}"
38
38
  ```
39
39
 
40
+ ## Getting Updates
41
+
42
+ There are two ways of getting updates from Telegram.
43
+ The first one is the simpliest, but less powerful: **getUpdates**
44
+
45
+ ### 1. getUpdates
46
+
47
+ ```ruby
48
+ require 'telegramAPI'
49
+
50
+ token = "******"
51
+ api = TelegramAPI.new(token)
52
+ while true do
53
+ updates = api.getUpdates({"timeout"=>180})
54
+ updates.each do |update|
55
+ usr = update['message']['chat']['username'] || "unknown"
56
+ puts "Received update from @#{usr}"
57
+ end
58
+ end
59
+ ```
60
+
61
+ ### 2. setWebhook
62
+
63
+ Using setWebhook is not so complicated, but you need a web server with https support enabled (for example: Openshift or Heroku)
64
+
65
+ In the example below I will use Sinatra framework for the Web Server.
66
+
67
+ ```ruby
68
+ require 'telegramAPI'
69
+ require 'sinatra'
70
+ require 'json'
71
+
72
+ token = "******"
73
+ api = TelegramAPI.new token
74
+
75
+ post "/#{token}" do
76
+ status 200
77
+ # Get Telegram Data
78
+ request.body.rewind
79
+ data = JSON.parse(request.body.read)
80
+
81
+ # Output data on stdout
82
+ p data
83
+ # Return an empty json, to say "ok" to Telegram
84
+ "{}"
85
+ end
86
+
87
+ r = api.setWebhook("https://YOUR_URL/#{token}").to_json
88
+ puts "setWebhook Result: #{r}"
89
+ ```
90
+
91
+
40
92
  ## Documentation
41
93
 
42
- Here you can use the Telegram official [documentation](https://core.telegram.org/bots/api#available-methods)
94
+ You can use the Telegram official [documentation](https://core.telegram.org/bots/api#available-methods)
43
95
 
44
- ## List of methods available
96
+ ## List of available methods
45
97
 
46
98
  Every method has a optional parameter. See the Send Custom Keyboard example for more.
47
99
 
data/lib/telegramAPI.rb CHANGED
@@ -32,17 +32,20 @@ class TelegramAPI
32
32
  params_s = "?#{p.join("&")}" if p.length!=0
33
33
 
34
34
  r = JSON.parse(RestClient.get(@@core+@token+"/"+api+params_s).body)
35
- if r['ok']!=true then return nil end
36
- if r['result'][-1]!=nil then @last_update=r['result'][-1]['update_id']+1 end
35
+ if r['result'].class==Array and r['result'][-1]!=nil then @last_update=r['result'][-1]['update_id']+1 end
37
36
  return r["result"]
38
37
  end
39
38
 
40
39
  def post api, name, path, to, options={}
41
40
  JSON.parse(RestClient.post(@@core+@token+api, {name=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash(options))).body)["result"]
42
41
  end
42
+
43
+ def setWebhook url
44
+ self.query("setWebhook", {"url"=>URI::encode(url)})
45
+ end
43
46
 
44
47
  def getUpdates options={"timeout"=>0, "limit"=>100}
45
- self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash(options))
48
+ self.query("getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash(options)))
46
49
  end
47
50
 
48
51
  def getMe
@@ -61,19 +64,19 @@ class TelegramAPI
61
64
  end
62
65
 
63
66
  def sendPhoto to, path, options={}
64
- self.post "/sendPhoto", :photo, path, to, options
67
+ self.post("/sendPhoto", :photo, path, to, options)
65
68
  end
66
69
 
67
70
  def sendAudio to, path, options={}
68
- self.post "/sendAudio", :audio, path, to, options
71
+ self.post("/sendAudio", :audio, path, to, options)
69
72
  end
70
73
 
71
74
  def sendDocument to, path, options={}
72
- self.post "/sendDocument", :document, path, to, options
75
+ self.post("/sendDocument", :document, path, to, options)
73
76
  end
74
77
 
75
78
  def sendStickerFromFile to, path, options={}
76
- self.post "/sendSticker", :sticker, path, to, options
79
+ self.post("/sendSticker", :sticker, path, to, options)
77
80
  end
78
81
 
79
82
  def sendSticker to, id, options={}
@@ -81,11 +84,11 @@ class TelegramAPI
81
84
  end
82
85
 
83
86
  def sendVideo to, path, options={}
84
- self.post "/sendVideo", :video, path, to, options
87
+ self.post("/sendVideo", :video, path, to, options)
85
88
  end
86
89
 
87
90
  def sendVoice to, path, options={}
88
- self.post "/sendVoice", :voice, path, to, options
91
+ self.post("/sendVoice", :voice, path, to, options)
89
92
  end
90
93
 
91
94
  def sendLocation to, lat, long, options={}
@@ -102,7 +105,7 @@ class TelegramAPI
102
105
 
103
106
  # act is one between: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location
104
107
  def sendChatAction to, act
105
- self.query "sendChatAction", {:chat_id=>to, :action=>act}
108
+ self.query("sendChatAction", {:chat_id=>to, :action=>act})
106
109
  end
107
110
 
108
111
  def getUserProfilePhotos id, options={}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegramAPI
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedetto Nespoli