web2tg api for websites

source code: https://gitlab.com/jayendren/ruby_apis/tree/develop/web2tg

Getting started

About

  • sinatra/mongodb/rufus-scheduler based web2tg api (REST) for allowing websites with chat integration frames/windows to send messages to telegram and provide an interface to retrieve responses for display.

    • NB: early ALPHA release !
    ###############     ############    ##############     ############
    #   WWW_CHAT  # ->  #   REST   # -> # web2tg_api # ->  # TELEGRAM #
    ###############     ############    ##############     ############
           ^                 ^                  ^                ^
           |_________________|____getUpdates____|_______________<|
           |                                                     |
           |                                                     |
           V                                                     V
     ###############                                        ###########
     # WWW_VISITOR #                                        # TG_USER #
     ###############                                        ###########

Requirements

  • sessionid
unique_string for client side user session
  • user
user@tld (retrieved from sessionid of user sending message)
  • chatid
telegram chatid of user responding to txmsg
  • txmsg
message generated from chat_screen
  • website
website name sending message to TELEGRAM
  • key
api authorization key

API

Send Message

Send message to telegram user

  • URL

    api/v1/web2tg?key=yourkey

  • Method:

    POST

  • URL Params

    Required:

    sessionid=[user_websessionid]
    user=[user@tld]
    txtmsg=[string]
    website=[originating website]
    chatid=[telegram user id answering]
    ?key=[apikey]

    Optional:

    state=[new]

  • Data Params

    [
      {
        "sessionid":"99fdbbe3e5fd20af788c91c4ce679c61",
        "user":"john@doe.net",
        "txmsg":"I require 10x of itemx by next Friday, are you available? thanks.",
        "website":"test.dom",
        "chatid":"52061074"
      }
    ]
    
  • Success Response:

    • Code: 200
  • Error Response:

    • Code: 401 UNAUTHORIZED

      Content: { error : "Log in" }

    OR

    • Code: 422 UNPROCESSABLE ENTRY

      Content: { error : "KeyError: key not found: "txmsg"" }
  • Sample Call:

curl -i -X POST -H "Content-Type: application/json" -d \
  '[{"sessionid":"99fdbbe3e5fd20af788c91c4ce679c61",
  "user":"john@doe.net",
  "txmsg":"I require 10x of itemx by next Friday, are you available? thanks.",
  "website":"test.dom",
  "chatid":"52061074"}]' \
  "http://127.0.0.1:4567/api/v1/web2tg?key=65e4dc25b2ae4ee2ad96c6dafcb337b1"
HTTP/1.1 200 OK
Content-Type: application/json
Location: http://127.0.0.1:4567/api/v1/web2tg/676
Content-Length: 0
X-Content-Type-Options: nosniff
Server: WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)
Date: Sun, 04 Sep 2016 18:45:24 GMT
Connection: Keep-Alive
  • Notes:

    None

Get Message

Get message

  • URL

    api/v1/web2tg?key=yourkey&messageid=$messageid
    api/v1/web2tg/$messageid?key=yourkey

  • Method:

    GET

  • URL Params

    Required:

    messagid=[messageid]
    ?key=[apikey]

    Optional:

    None

  • Data Params

    None

  • Success Response:

    • Code: 200
  • Error Response:

    • Code: 401 UNAUTHORIZED

      Content: { error : "Log in" }
  • Sample Call:

curl -Ssv "http://127.0.0.1:4567/api/v1/web2tg?key=65e4dc25b2ae4ee2ad96c6dafcb337b1&messageid=676" | jq .
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 4567 (#0)
> GET /api/v1/web2tg?key=65e4dc25b2ae4ee2ad96c6dafcb337b1&messageid=676 HTTP/1.1
> Host: 127.0.0.1:4567
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 265
< X-Content-Type-Options: nosniff
< Server: WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)
< Date: Sun, 04 Sep 2016 19:45:53 GMT
< Connection: Keep-Alive
<
{ [265 bytes data]
* Connection #0 to host 127.0.0.1 left intact
[
  {
    "timestamp": "2016-09-04T20:45:22",
    "messageid": "676",
    "website": "test.dom",
    "txmsg": "I require 10x of itemx by next Friday, are you available? thanks.",
    "rxmsg": "sure, no prob",
    "state": "resending",
    "sessionid": "99fdbbe3e5fd20af788c91c4ce679c61",
    "user": "john@doe.net"
  }
]

curl -Ssv "http://127.0.0.1:4567/api/v1/web2tg/676?key=65e4dc25b2ae4ee2ad96c6dafcb337b1" | jq .
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 4567 (#0)
> GET /api/v1/web2tg/676?key=65e4dc25b2ae4ee2ad96c6dafcb337b1 HTTP/1.1
> Host: 127.0.0.1:4567
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 263
< X-Content-Type-Options: nosniff
< Server: WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)
< Date: Sun, 04 Sep 2016 19:47:11 GMT
< Connection: Keep-Alive
<
{ [263 bytes data]
* Connection #0 to host 127.0.0.1 left intact
{
  "timestamp": "2016-09-04T20:45:22",
  "messageid": "676",
  "website": "test.dom",
  "txmsg": "I require 10x of itemx by next Friday, are you available? thanks.",
  "rxmsg": "sure, no prob",
  "state": "resending",
  "sessionid": "99fdbbe3e5fd20af788c91c4ce679c61",
  "user": "john@doe.net"
}
  • Notes:

    None

Delete Message

Delete message from database

  • URL

    api/v1/web2tg?key=yourkey&messageid=$messageid

  • Method:

    DELETE

  • URL Params

    Required:

    messageid=[messageid]
    ?key=[apikey]

    Optional:

    None

  • Data Params

    None

  • Success Response:

    • Code: 204
  • Error Response:

    None

  • Sample Call:

curl -i -X DELETE -H "Content-Type: application/json" \
"http://127.0.0.1:4567/api/v1/web2tg/messageid=676?key=65e4dc25b2ae4ee2ad96c6dafcb337b1"
HTTP/1.1 204 No Content
X-Content-Type-Options: nosniff
Server: WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)
Date: Sun, 04 Sep 2016 18:50:43 GMT
Connection: Keep-Alive
  • Notes:

    None

Update Message

Mark transaction as completed

  • URL

    api/v1/web2tg/$messageid?key=yourkey"

  • Method:

    PATCH

  • URL Params

    Required:

    messageid
    ?key=[apikey]

    Optional:

    None

  • Data Params

    {"state":"completed",}
    
  • Success Response:

    • Code: 204
  • Error Response:

    • Code: 404 Not Found

      Content: { error : "Log in" }
      Content: { message : "Sorry... Not Found" }
  • Sample Call:

curl -i -X PATCH -H "Content-Type: application/json" -d \
'{"state":"completed"}' \
"http://127.0.0.1:4567/api/v1/web2tg/676?key=65e4dc25b2ae4ee2ad96c6dafcb337b1"
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 263
X-Content-Type-Options: nosniff
Server: WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)
Date: Sun, 04 Sep 2016 19:39:27 GMT
Connection: Keep-Alive

{"timestamp":"2016-09-04T20:45:22","messageid":"676","website":"test.dom","txmsg":"I require 10x of itemx by next Friday, are you available? thanks.","rxmsg":"sure, no prob","state":"completed","sessionid":"99fdbbe3e5fd20af788c91c4ce679c61","user":"john@doe.net"}%
  • Notes:

    WWW_CHAT should delete state=replied message after successful display.

Author

comments powered by Disqus