Code Monkey home page Code Monkey logo

b24's Introduction

Bitrix24 Client Library

Installation

npm install b24 --save

Modes

There are 2 modes to use this library. API mode and Webhook mode.

You can specify the mode when initializing Bitrix24.

API Mode

When you use API mode, you must authenticate the user using OAuth. See Authentication for example. You also must provide client_id, client_secret, and redirect_uri in config block.

Method Hook

There are 2 method that will called when using API mode. saveToken(data) and retriveToken. You can use this to save and get token from database.

Example

const b24 = require('b24');

const bitrix24 = new b24.Bitrix24({
    config: {
        mode: "api",
        host: "your bitrix host",
        client_id : "your client id",
        client_secret : "your client secret",
        redirect_uri : "http://localhost:3000/callback"
    },
    methods: {
        async saveToken(data){
            //Save token to database
        },
        async retriveToken(){
            //Retrive token from database
            return {
                access_token: "youraccesstoken",
                refresh_token: "yourrefreshtoken"
            }
        }
    }
})

Webhook Mode

It use webhook feature from Bitrix24 To use this you must provide user_id and code in config block.

user_id can be obtained throught my profile page. See the URL, e.g https://k2d2.bitrix24.com/company/personal/user/4/. The value would be 4

code can be obtained throught Application -> Web hooks -> ADD WEB HOOK -> Inbound web hook

Example

const b24 = require('b24');

const bitrix24 = new b24.Bitrix24({
    config: {
        mode: "webhook",
        host: "your bitrix host",
        user_id: "1",
        code: "your_webhook_code"
    }
})

Authentication

You must do authentication when use API mode. It just OAuth2

Step:

  1. Visit url provided by bitrix24.auth.authorization_uri
  2. It will give you the code in callback url, use this to get token
  3. Get token with bitrix24.auth.getToken(code)
  4. Profit!!!

Example:

const express = require('express');
const b24 = require('b24');

const app = express()

const bitrix24 = new b24.Bitrix24({
    config: {
        mode: "api",
        host: "your bitrix host",
        client_id : "your client id",
        client_secret : "your client secret",
        redirect_uri : "http://localhost:3000/callback"
    },
    methods: {
        async saveToken(data){
            //Save token to database
        },
        async retriveToken(){
            //Retrive token from database
            return {
                access_token: "youraccesstoken",
                refresh_token: "yourrefreshtoken"
            }
        }
    }
})

// Bitrix auth
app.get('/auth', (req, res) => {
    res.redirect(bitrix24.auth.authorization_uri);
});

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
    try{
        const code = req.query.code;
        const result = await bitrix24.auth.getToken(code);
        return res.json(result);
    }catch(err){
        console.log(err)
        return res.status(500).json({message:"Authentication Failed"});
    }
});

app.listen(3000, () => {
    console.log('Server started on port 3000');
});

Run Bitrix24 API

For complete API refrence visit https://training.bitrix24.com/rest_help/index.php

To Run Bitrix24 API we need to use callMethod(method, param) method.

pass the method you want to run in method parameter, param parameter is optional, it used to add parameter to method that you call, visit the official API reference to see all possible parameter.

Example:

const express = require('express');
const b24 = require('b24');

const app = express()

const bitrix24 = new b24.Bitrix24({
    config: {
        mode: "webhook",
        host: "your bitrix host",
        user_id: "1",
        code: "your_webhook_code"
    }
})

// Get all Bitrix24 User
app.get('/allUser', async (req, res) => {
    try{
        const result = await bitrix24.callMethod('user.get');
        return res.json(result);        
    }catch(err){
        console.log(err)
        return res.status(500).json({message:"Internal Server Error"});
    }
})

app.listen(3000, () => {
    console.log('Server started on port 3000');
});

b24's People

Contributors

setyongr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

b24's Issues

Use another QS library

When we send a find method, the "querystring" native package can't handle nasted objects.

I'll send a pull request with a new "qs" package installation dependency to correct it.

License

Dont forget to add License โœŒ๏ธ

Unable to call B24 from external Library

Hi baws,
I have tried everything i can but can't get to call the methods from inside my libray, kindly have a look

const express = require('express');
const b24 = require('b24');
const app = express();
var ethers = require('ethers');

// Connect to the network
var providers = require('ethers').providers;
var network = providers.networks.ropsten;
// Connect to a local Parity instance
var provider = new providers.JsonRpcProvider('http://localhost:8545', network);

const bitrix24 = new b24.Bitrix24({
config: {
mode: "api",
host: "xxxxxxxxx",
client_id : "local.cccccccccccccccccccccccccc",
client_secret : "dddddddddddddddddddddddddddd",
redirect_uri : "http://localhost:3000/callback"
},
methods: {
async saveToken(data){
//Save token to database
},
async retriveToken(){
//Retrive token from database
return {
access_token: "youraccesstoken",
refresh_token: "yourrefreshtoken"
}
}
}
})

// Get notified when a contract event is logged
var eventTopic = '0xf538d33cf0aeab2f474d1ec307854fed7e411946cb8e4239785e4f7d2c424047';
provider.on([ eventTopic ], function(log) {
console.log('Event Log');
console.log(log);
var Call = log;

// Bitrix auth
app.get('/auth', (req, res) => {
res.redirect(bitrix24.auth.authorization_uri);
});

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
try{
const code = req.query.code;
const result = await bitrix24.auth.getToken(code);
return res.json(result);
}catch(err){
console.log(err)
return res.status(500).json({message:"Authentication Failed"});
}
});

// if (Call == "xxxxxxx") {

app.get('/allUser', async (req, res) => {
try{
const result = await bitrix24.callMethod('user.add', {"EMAIL": "[email protected]"});
return res.json(result);
}catch(err){
console.log(err)
return res.status(222).json({"result":222});
}
});
});

app.listen(3000, () => {
console.log('Server started on port 3000');
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.