tim_sdk 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 120fff55b1cbf6d06d3f90863e1c2ef6a5ce41df228e539b3299f7ee60252a20
4
+ data.tar.gz: 1fe795963ff30c17d5eea55b812cdd50094d5978e4d548409adfbf4d6fdce70e
5
+ SHA512:
6
+ metadata.gz: 6a087161ed2a58f0da8baee2c7d4fb183ff37070eec59a187d54213adf37b8e04257bdda5effc792787c97785dce7b7ba10f3ac12090d2cc2545237678fc1359
7
+ data.tar.gz: 01dcf8d334714d6742c37cf6e7d56e4ca009281501c0f5243385135dccfe16462b2eb1bd75034b030afaa9e6840edcb379e6de1afad449bd9fc7c6a0bfd243b4
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake" /></Settings>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.8
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at vincent4502237@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tim_sdk.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
8
+ gem 'faraday', '~> 1.0', '>= 1.0.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tim_sdk (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ faraday (1.0.1)
11
+ multipart-post (>= 1.2, < 3)
12
+ multipart-post (2.1.1)
13
+ rake (12.3.3)
14
+ rspec (3.9.0)
15
+ rspec-core (~> 3.9.0)
16
+ rspec-expectations (~> 3.9.0)
17
+ rspec-mocks (~> 3.9.0)
18
+ rspec-core (3.9.2)
19
+ rspec-support (~> 3.9.3)
20
+ rspec-expectations (3.9.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-mocks (3.9.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-support (3.9.3)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ faraday (~> 1.0, >= 1.0.1)
33
+ rake (~> 12.0)
34
+ rspec (~> 3.0)
35
+ tim_sdk!
36
+
37
+ BUNDLED WITH
38
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 JiangYongKang
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 @@
1
+ # TimSdk
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tim_sdk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,130 @@
1
+ require 'faraday'
2
+
3
+ module TimSdk
4
+ class Api
5
+
6
+ def self.connection
7
+ @connection ||= Faraday.new('https://console.tim.qq.com', params: {
8
+ sdkappid: TimSdk.configuration.app_id,
9
+ identifier: TimSdk.configuration.admin_account,
10
+ usersig: TimSdk::Sign.signature(TimSdk.configuration.admin_account),
11
+ random: rand(0..4294967295),
12
+ contenttype: :json
13
+ }) do |faraday|
14
+ faraday.response :logger
15
+ faraday.adapter Faraday.default_adapter
16
+ end
17
+ end
18
+
19
+ # 导入单个帐号
20
+ def self.invoke_account_import(identifier, nick = nil, face_url = nil)
21
+ response = connection.post('/v4/im_open_login_svc/account_import') do |request|
22
+ request_body = { :Identifier => identifier.to_s }
23
+ request_body.merge!(:Nick => nick) if nick
24
+ request_body.merge!(:FaceUrl => face_url) if face_url
25
+ request.body = request_body.to_json
26
+ end
27
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
28
+ JSON.parse(response.body, symbolize_names: true) if response.success?
29
+ end
30
+
31
+ # 导入多个账号
32
+ def self.invoke_multi_account_import(accounts)
33
+ response = connection.post('/v4/im_open_login_svc/multiaccount_import') do |request|
34
+ request.body = {
35
+ :Accounts => accounts.map(&:to_s)
36
+ }.to_json
37
+ end
38
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
39
+ JSON.parse(response.body, symbolize_names: true) if response.success?
40
+ end
41
+
42
+ # 删除账号
43
+ def self.invoke_account_delete(accounts)
44
+ response = connection.post('/v4/im_open_login_svc/account_delete') do |request|
45
+ request.body = {
46
+ :DeleteItem => accounts.map do |account|
47
+ {
48
+ :UserID => account.to_s
49
+ }
50
+ end
51
+ }.to_json
52
+ end
53
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
54
+ JSON.parse(response.body, symbolize_names: true) if response.success?
55
+ end
56
+
57
+ # 查询账号
58
+ def self.invoke_account_check(accounts)
59
+ response = connection.post('/v4/im_open_login_svc/account_check') do |request|
60
+ request.body = {
61
+ :CheckItem => accounts.map do |account|
62
+ {
63
+ :UserID => account.to_s
64
+ }
65
+ end
66
+ }.to_json
67
+ end
68
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
69
+ JSON.parse(response.body, symbolize_names: true) if response.success?
70
+ end
71
+
72
+ # 失效帐号登录态
73
+ def self.invoke_kick(identifier)
74
+ response = connection.post('/v4/im_open_login_svc/kick') do |request|
75
+ request.body = {
76
+ :Identifier => identifier.to_s
77
+ }.to_json
78
+ end
79
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
80
+ JSON.parse(response.body, symbolize_names: true) if response.success?
81
+ end
82
+
83
+ # 查询帐号在线状态
84
+ def self.invoke_query_state(accounts, is_need_detail = 0)
85
+ response = connection.post('/v4/openim/querystate') do |request|
86
+ request.body = {
87
+ :To_Account => accounts.map(&:to_s),
88
+ :IsNeedDetail => is_need_detail
89
+ }.to_json
90
+ end
91
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
92
+ JSON.parse(response.body, symbolize_names: true) if response.success?
93
+ end
94
+
95
+ # 拉取运营数据
96
+ def self.invoke_fetch_app_info(fields = [])
97
+ response = connection.post('/v4/openconfigsvr/getappinfo') do |request|
98
+ request.body = {
99
+ :RequestField => fields
100
+ }.to_json
101
+ end
102
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
103
+ JSON.parse(response.body, symbolize_names: true) if response.success?
104
+ end
105
+
106
+
107
+ # 下载消息记录
108
+ def self.invoke_fetch_history(chat_type, msg_time)
109
+ response = connection.post('/v4/open_msg_svc/get_history') do |request|
110
+ request.body = {
111
+ :ChatType => chat_type,
112
+ :MsgTime => msg_time,
113
+ }.to_json
114
+ end
115
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
116
+ JSON.parse(response.body, symbolize_names: true) if response.success?
117
+ end
118
+
119
+
120
+ # 获取服务器 IP 地址
121
+ def self.invoke_fetch_ip_list
122
+ response = connection.post('/v4/ConfigSvc/GetIPList') do |request|
123
+ request.body = {}.to_json
124
+ end
125
+ raise TimServerError, "Response Status: #{response.status}" unless response.success?
126
+ JSON.parse(response.body, symbolize_names: true) if response.success?
127
+ end
128
+
129
+ end
130
+ end
@@ -0,0 +1,5 @@
1
+ module TimSdk
2
+ class Configuration
3
+ attr_accessor :app_id, :app_key, :admin_account
4
+ end
5
+ end
@@ -0,0 +1,55 @@
1
+ require 'tim_sdk/configuration'
2
+ require 'base64'
3
+ require 'openssl'
4
+ require 'json'
5
+ require 'zlib'
6
+
7
+ module TimSdk
8
+ class Sign
9
+ class << self
10
+
11
+ def signature(identifier, expire = 24 * 60 * 60)
12
+ do_signature(identifier, expire, nil)
13
+ end
14
+
15
+ def signature_with_user_buf(identifier, expire, user_buf)
16
+ do_signature(identifier, expire, user_buf)
17
+ end
18
+
19
+ private
20
+
21
+ def do_signature(identifier, expire = 180 * 86400, user_buf = nil)
22
+ timestamp = Time.now.to_i
23
+ raw_data = {}
24
+ base64_user_buf = nil
25
+ unless user_buf.nil?
26
+ base64_user_buf = Base64.encode64(user_buf)
27
+ raw_data.merge!('TLS.userbuf' => base64_user_buf)
28
+ end
29
+ raw_data.merge!('TLS.sig' => hmac_sha256(identifier, timestamp, expire, base64_user_buf),
30
+ 'TLS.expire' => expire.to_i,
31
+ 'TLS.sdkappid' => TimSdk.configuration.app_id,
32
+ 'TLS.ver' => '2.0',
33
+ 'TLS.time' => timestamp,
34
+ 'TLS.identifier' => identifier.to_s)
35
+ raw_sig = raw_data.to_json.gsub(/,/, ', ').gsub(/:/, ': ')
36
+ sig_zlib = Zlib::Deflate.deflate(raw_sig)
37
+ base64_encode(sig_zlib)
38
+ end
39
+
40
+ def hmac_sha256(identifier, curr_time, expire, base64_user_buf = nil)
41
+ data = ["TLS.identifier:#{identifier}", "TLS.sdkappid:#{TimSdk.configuration.app_id}", "TLS.time:#{curr_time}", "TLS.expire:#{expire}"]
42
+ data << "TLS.userbuf:#{base64_user_buf}" if base64_user_buf
43
+ data_string = "#{data.join("\n")}\n"
44
+ Base64.strict_encode64(
45
+ OpenSSL::HMAC.digest('sha256', TimSdk.configuration.app_key, data_string)
46
+ )
47
+ end
48
+
49
+ def base64_encode(data)
50
+ Base64.strict_encode64(data).gsub('+', '*').gsub('/', '-').gsub('=', '_')
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module TimSdk
2
+ VERSION = '0.1.1'
3
+ end
data/lib/tim_sdk.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "tim_sdk/version"
2
+ require 'tim_sdk/sign'
3
+ require 'tim_sdk/api'
4
+
5
+ module TimSdk
6
+
7
+ class TimServerError < StandardError; end
8
+
9
+ class << self
10
+ attr_writer :configuration
11
+
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def configure
17
+ yield configuration
18
+ end
19
+ end
20
+
21
+ end
data/tim_sdk.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/tim_sdk/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'tim_sdk'
5
+ spec.version = TimSdk::VERSION
6
+ spec.authors = ['JiangYongKang']
7
+ spec.email = ['vincent4502237@gmail.com']
8
+
9
+ spec.summary = 'tencent tim sdk for ruby'
10
+ spec.description = 'a ruby program to facilitate accessing tim service'
11
+ spec.homepage = 'https://github.com/JiangYongKang/tim_sdk'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/JiangYongKang/tim_sdk'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/JiangYongKang/tim_sdk'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tim_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - JiangYongKang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a ruby program to facilitate accessing tim service
14
+ email:
15
+ - vincent4502237@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rakeTasks"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/tim_sdk.rb
33
+ - lib/tim_sdk/api.rb
34
+ - lib/tim_sdk/configuration.rb
35
+ - lib/tim_sdk/sign.rb
36
+ - lib/tim_sdk/version.rb
37
+ - tim_sdk.gemspec
38
+ homepage: https://github.com/JiangYongKang/tim_sdk
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/JiangYongKang/tim_sdk
43
+ source_code_uri: https://github.com/JiangYongKang/tim_sdk
44
+ changelog_uri: https://github.com/JiangYongKang/tim_sdk
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.7.6.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: tencent tim sdk for ruby
65
+ test_files: []