iprog_sms 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/iprog_sms/api_client.rb +23 -0
- data/lib/iprog_sms/sender.rb +21 -0
- data/lib/iprog_sms/sms.rb +95 -0
- data/lib/iprog_sms/version.rb +6 -0
- data/lib/iprog_sms.rb +10 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 90b96ab4208fd86918b9b66a42e2b3d18153f93774e771da7db5ea33076652ae
|
4
|
+
data.tar.gz: bb92a9bd4c9da6a8931f09f7a97bcb59eab08c4fbdd76f362de65543ba857c18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '090c37cbbccef0b31f8f219bb8ce12d386e44a21ffbc9725e29b1be8e198bef6a437b8c692c84dc35f7d9346d9ca2333e7e724428b48abc92d2cbe5a240e2e3a'
|
7
|
+
data.tar.gz: d72af85bf9271c25a1dfb9548f3a9a086a3dfc678d4e4fc6402b161adcb577f656e839bfe697c05f9dd22ae20d2c7bf4971e986f73603df2c49f25e6d7d9c1e4
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# lib/iprog_sms/api_client.rb
|
3
|
+
|
4
|
+
require 'net/http'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module IprogSms
|
8
|
+
class ApiClient
|
9
|
+
def initialize(api_url)
|
10
|
+
@api_url = api_url
|
11
|
+
end
|
12
|
+
|
13
|
+
def fetch_sms_details
|
14
|
+
uri = URI(@api_url)
|
15
|
+
response = Net::HTTP.get(uri)
|
16
|
+
data = JSON.parse(response)
|
17
|
+
{ phone_number: data['phone_number'], message: data['message'] }
|
18
|
+
rescue StandardError => e
|
19
|
+
puts "Error fetching data from API: #{e.message}"
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# lib/iprog_sms/sender.rb
|
3
|
+
|
4
|
+
module IprogSms
|
5
|
+
class Sender
|
6
|
+
def initialize(api_url)
|
7
|
+
@api_url = api_url
|
8
|
+
@sms_sender = IprogSms::SMS.new
|
9
|
+
@api_client = IprogSms::ApiClient.new(api_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
def send_sms_from_api
|
13
|
+
sms_details = @api_client.fetch_sms_details
|
14
|
+
if sms_details
|
15
|
+
@sms_sender.send_sms(sms_details[:phone_number], sms_details[:message])
|
16
|
+
else
|
17
|
+
puts "Failed to retrieve SMS details from API."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# lib/iprog_sms/sms.rb
|
3
|
+
|
4
|
+
require 'rubyserial'
|
5
|
+
|
6
|
+
module IprogSms
|
7
|
+
class SMS
|
8
|
+
def initialize
|
9
|
+
@baud_rate = 9600
|
10
|
+
@port_str = detect_sim800c || "/dev/ttyUSB0" # Detect SIM800C port automatically
|
11
|
+
end
|
12
|
+
|
13
|
+
def send_sms(phone_number, message)
|
14
|
+
if connected?
|
15
|
+
begin
|
16
|
+
# Open the serial connection
|
17
|
+
serial = Serial.new(@port_str, @baud_rate)
|
18
|
+
|
19
|
+
# Set SMS to text mode
|
20
|
+
send_at_command(serial, "AT+CMGF=1")
|
21
|
+
|
22
|
+
# Specify the recipient phone number
|
23
|
+
send_at_command(serial, "AT+CMGS=\"#{phone_number}\"")
|
24
|
+
|
25
|
+
# Send the actual SMS message, followed by Ctrl+Z (ASCII 26)
|
26
|
+
send_at_command(serial, "#{message}\x1A", 5)
|
27
|
+
|
28
|
+
puts "SMS sent to #{phone_number}."
|
29
|
+
serial.close
|
30
|
+
rescue Exception => e
|
31
|
+
puts "Error: #{e.message}"
|
32
|
+
end
|
33
|
+
else
|
34
|
+
puts "SIM800C is not connected!"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def connected?
|
39
|
+
begin
|
40
|
+
serial = Serial.new(@port_str, @baud_rate)
|
41
|
+
response = send_at_command(serial, "AT")
|
42
|
+
serial.close
|
43
|
+
response.include?("OK") # Check if the response contains "OK"
|
44
|
+
rescue Exception => e
|
45
|
+
puts "Error checking connection: #{e.message}"
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def detect_sim800c
|
53
|
+
# List all potential serial ports (Linux/macOS). For Windows, replace with 'COM*'.
|
54
|
+
potential_ports = Dir['/dev/ttyUSB*'] + Dir['/dev/tty.usbserial*'] + Dir['/dev/tty.*']
|
55
|
+
|
56
|
+
potential_ports.each do |port|
|
57
|
+
puts "Checking port: #{port}"
|
58
|
+
begin
|
59
|
+
# Try opening the port
|
60
|
+
serial = Serial.new(port, @baud_rate)
|
61
|
+
|
62
|
+
# Send the AT command
|
63
|
+
serial.write("AT\r")
|
64
|
+
sleep(1) # Wait for a response
|
65
|
+
response = serial.read(100)
|
66
|
+
|
67
|
+
# If the response contains "OK", it's likely the SIM800C
|
68
|
+
if response.include?("OK")
|
69
|
+
puts "SIM800C detected on port: #{port}"
|
70
|
+
serial.close
|
71
|
+
return port # Return the correct port
|
72
|
+
else
|
73
|
+
puts "No response from #{port}, moving to next port..."
|
74
|
+
serial.close
|
75
|
+
end
|
76
|
+
rescue StandardError => e
|
77
|
+
puts "Error on #{port}: #{e.message}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# If no SIM800C is detected
|
82
|
+
puts "No SIM800C module detected."
|
83
|
+
nil
|
84
|
+
end
|
85
|
+
|
86
|
+
def send_at_command(serial, command, wait_time = 1)
|
87
|
+
puts "Sending command: #{command}"
|
88
|
+
serial.write("#{command}\r")
|
89
|
+
sleep(wait_time)
|
90
|
+
response = serial.read(1000)
|
91
|
+
puts "Response: #{response}"
|
92
|
+
return response
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/iprog_sms.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# lib/iprog_sms.rb
|
3
|
+
|
4
|
+
require_relative "../lib/iprog_sms/version"
|
5
|
+
require_relative "../lib/iprog_sms/sms"
|
6
|
+
require_relative "../lib/iprog_sms/api_client"
|
7
|
+
require_relative "../lib/iprog_sms/sender"
|
8
|
+
|
9
|
+
module IprogSms
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iprog_sms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jayson Presto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubyserial
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.0
|
27
|
+
description: This gem allows you to send SMS using the SIM800C module and supports
|
28
|
+
fetching SMS details from an API.
|
29
|
+
email:
|
30
|
+
- jaysonpresto.iprog21@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/iprog_sms.rb
|
36
|
+
- lib/iprog_sms/api_client.rb
|
37
|
+
- lib/iprog_sms/sender.rb
|
38
|
+
- lib/iprog_sms/sms.rb
|
39
|
+
- lib/iprog_sms/version.rb
|
40
|
+
homepage: https://rubygems.org/gems/iprog_sms
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://rubygems.org/gems/iprog_sms
|
45
|
+
source_code_uri: https://github.com/iprog21/iprog_sms.git
|
46
|
+
changelog_uri: https://github.com/iprog21/iprog_sms/blob/master/CHANGELOG.md
|
47
|
+
code_of_conduct_uri: https://github.com/iprog21/iprog_sms/blob/main/CODE_OF_CONDUCT.md
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 2.5.0
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.5.22
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: A gem to send SMS using SIM800C and Ruby.
|
67
|
+
test_files: []
|