Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
6b6ede656d | |||
e0e5a72e90 | |||
62bafd9af7 | |||
7d17744239 |
45
README.md
45
README.md
@ -1,4 +1,47 @@
|
|||||||
# SMS Hook
|
# SMS Hook
|
||||||
|
|
||||||
## What?
|
## What
|
||||||
The purpose of SmsHook is to provide a simple library that provides a webhook where texts can be send to it using something like [Sms Forwarder](https://github.com/bogkonstantin/android_income_sms_gateway_webhook).
|
The purpose of SmsHook is to provide a simple library that provides a webhook where texts can be send to it using something like [Sms Forwarder](https://github.com/bogkonstantin/android_income_sms_gateway_webhook).
|
||||||
|
|
||||||
|
## How
|
||||||
|
This captures all the requests coming into /webhook with `{"content":"%text"}` and puts them in a buffer.
|
||||||
|
There are 3 methods
|
||||||
|
|
||||||
|
### GetLast
|
||||||
|
Get last returns the last sms received and its location in the buffer.
|
||||||
|
|
||||||
|
### WaitForNew
|
||||||
|
WaitForNew keeps waiting for the next sms that will be send the the webhook
|
||||||
|
|
||||||
|
### WaitForNewWithTimeout
|
||||||
|
Same thing as WaitForNew but stops after a set interval
|
||||||
|
|
||||||
|
|
||||||
|
## EXAMPLE
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.directme.in/Joren/SmsHook/smshook"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
smshook.Init(10, "0.0.0.0:8080")
|
||||||
|
|
||||||
|
t := smshook.WaitForNew()
|
||||||
|
fmt.Printf("Received new item: %s\n", t)
|
||||||
|
|
||||||
|
text, pos := smshook.GetLast()
|
||||||
|
fmt.Printf("Last item: %s, Position: %d\n",text,pos)
|
||||||
|
|
||||||
|
t2, s := smshook.WaitForNewWithTimeout(10 * time.Second)
|
||||||
|
if s {
|
||||||
|
fmt.Printf("Received new item: %s\n", t2)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Timed out waiting for new item.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
@ -73,3 +73,21 @@ func (rb *RingBuffer) GetLastPosition() int {
|
|||||||
return rb.tail
|
return rb.tail
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rb *RingBuffer) ContainsItem(item string) bool {
|
||||||
|
rb.mu.Lock()
|
||||||
|
defer rb.mu.Unlock()
|
||||||
|
|
||||||
|
if rb.head == -1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < rb.size; i++ {
|
||||||
|
index := (rb.head + i) % rb.size
|
||||||
|
if rb.buffer[index] == item {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user