Git Product home page Git Product logo

viesvatcheckerbot's Introduction

What's This?

This is a Telegram bot implementation for checking the validity of VAT numbers in VIES (VAT Information Exchange System).

What Does It Do?

You can submit a VAT number that will be checked in VIES. If the number is valid, the bot will tell you exactly that. If the number is not valid, the bot will store the number and check it periodically until it becomes valid. The bot will notify you when that happens.

You can do the same manually here: https://ec.europa.eu/taxation_customs/vies

How Does It Do That?

Code is TypeScript/Node.js that is intended to be deployed on Azure Functions. Database is CosmosDB which is also deployed on Azure (and is accessed through MongoDB APIs). Telegraf is a framework of choice for handling Telegram bot interactions.

Why Do I Use What I Use?

  • Serverless: I didn't want to pay for the hosting (serverless is basically free for small workloads) and also wanted to try, well, serverless.
  • Azure: because I already have an account and have a bit of familiarity with the ecosystem.
  • Typescript/Node.js: quick feedback loop for hacking/exploring solutions, can be deployed on Azure.
  • CosmosDB / MongoDB: provided by Azure, low effort deployment and usage in the context of Node.js (especially for such a simple use case).
  • Telegram: I use it a lot so wanted to explore implementing bots. Simple solution for providing UI and notifications.
  • Telegraf: simple intuitive API.

Implementation Details

There are 4 functions:

  • TgBotApi: HTTP Trigger that sets up Telegram webhooks and command handlers upon initialization. Webhook invocations will trigger command handlers which then delegate execution to a different HTTP Trigger: HttpApi.

  • HttpApi: HTTP Trigger that can perform various actions with VAT numbers. This function can store VAT numbers in the MongoDB database and validate them against VIES API.

  • TimerTrigger: Timer Trigger that checks all pending VAT numbers once an hour and notifies Telegram users when those numbers become valid. If TimerTrigger encounters unrecoverable error, it will put the VAT number in question into a separate DB collection (VatRequestErrors) which you can use to investigate the source of the issue and maybe re-register the VAT number for monitoring after the issue was resolved.

  • HttpAdminApi: HTTP Trigger that you can use to manually re-register VAT numbers that encountered unrecoverable errors during validation process.

Run/Debug

You can't run this as is.

  1. Use VS Code with Azure Tools extension.
  2. Create a Telegram bot. Put it's access token into settings file (see step 6).
  3. Go to Azure > Databases pane and create new "MongoDB" database (in Azure it's Azure Cosmos DB for MongoDB API). Put the connection string into settings file (see step 6).
  4. Since Telegram can only invoke webhooks for publicly available URLs, you'll need something like localtunnel or ngrok to setup a proxy that will redirect traffic to your app running on localhost. Put the proxy URL into settings file (see step 6).
  5. Go to Azure > Functions pane, click on Local Project's initialize button.
  6. Go to Run and Debug pane and try to run the project. Follow the steps to connect your project to your Azure Account.
  7. Modify local.settings.json at the root of the repo. It should contain following settings:
{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": "YOUR_AUTOGENERATED_AZURE_CONNECTION_STRING",
    "MONGODB_CONNECTION_STRING": "YOUR_MONGODB_CONNECTION_STRING",
    "TG_BOT_TOKEN": "YOUR_TELEGRAM_BOT_TOKEN",
    "TG_BOT_API_URL": "LOCAL_TUNNEL_OR_NGROK_OR_WHATEVER_BASE_URL/api/TgBotApi",
    "TG_BOT_API_TOKEN": "", // not required for local deployment
    "HTTP_API_URL": "http://localhost:7071/api/HttpApi",
    "HTTP_API_TOKEN": "", // not required for local deployment
    "VIES_URL": "https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl",
    "MAX_PENDING_VAT_NUMBERS_PER_USER": 10,
    "VAT_NUMBER_EXPIRATION_DAYS": 90
  }
}
  1. You can now run your functions locally (either through npm run or through the VS Code's Run and Debug pane).
  2. You can also deploy your functions to Azure using Azure > Functions pane.
  3. After you've deployed your functions, you can now update Azure > Functions > {Your Project} > Application Settings to contain the same settings that you added to local.settings.json:
    • MONGODB_CONNECTION_STRING and TG_BOT_TOKEN will be the same.
    • TG_BOT_API_URL is the URL of TgBotApi function (without code param).
    • TG_BOT_API_TOKEN is the auth code of TgBotApi function.
    • HTTP_API_URL is the URL of HttpApi function (without code param).
    • HTTP_API_TOKEN is the auth code of HttpApi function.
    • Note that you can retrieve the URL with code by selecting "Copy Function URL" in the context menu of a specific deployed function (e.g. https://{your-project-name}.azurewebsites.net/api/httpapi?code={http-api-function-auth-code}).
    • MAX_PENDING_APP_NUMBERS_PER_USER should be self-explanatory.
    • VAT_NUMBER_EXPIRATION_DAYS defines a period after which VAT number should stop being monitored (to handle the case when it never becomes valid).

Tests

Unit Tests

You need to create .env.test file first so configuration can be initialized properly. You can just duplicate .env.example in this case. You can run unit tests with npm run test. Tests are written with mocha, chai and sinon.

E2E

Tests are located in the Admin Portal repo and are supposed to be executed against the backend running in E2E mode.

E2E mode is just an express server reusing the same handlers that are used in Azure Functions APIs + a little extra helpers.

You need to create .env.e2e file first so configuration can be initialized properly. You can just duplicate .env.example in this case. You can run backend in E2E mode with npm run start:e2e.

Why Does It Do That?

Main Use Case

When you register an entity in EU, chances are that you need to be registered in VIES as well. It can take some time during which you'll probably get bored manually entering your VAT number and seeing that it's not yet valid. This bot is aimed to automate VAT number validation for you.

Alternative Use Case

You need to verify that some other entity has a valid VAT number for transactions within EU.

Anything Else Worth Looking At?

Simple admin portal app that will allow you to resolve errors more conveniently than calling the admin API by hand: https://github.com/megafinz/ViesVatCheckerBotAdminPortal.

viesvatcheckerbot's People

Contributors

dependabot[bot] avatar megafinz avatar renovate[bot] avatar

Watchers

 avatar

viesvatcheckerbot's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency soap to v1.0.4
  • Update dependency ts-node to v10.9.2
  • Update dependency axios to ^0.28.0
  • Update dependency dotenv to v16.4.5
  • Update dependency eslint to v8.57.0
  • Update dependency eslint-config-prettier to v9.1.0
  • Update dependency lint-staged to v15.2.7
  • Update dependency mongodb-memory-server to v9.3.0
  • Update dependency nodemon to v3.1.4
  • Update dependency prettier to v3.3.2
  • Update dependency telegraf to v4.16.3
  • Update dependency typescript to v5.5.2
  • Update dependency zod to v3.23.8
  • Update dependency chai to v5
  • Update dependency eslint to v9
  • Update dependency husky to v9
  • Update dependency sinon to v18
  • Update typescript-eslint monorepo to v7 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/pr.yml
  • actions/checkout v4
  • actions/setup-node v4
npm
package.json
  • axios ^0.27.2
  • dotenv ^16.0.1
  • express ^4.18.1
  • mongodb-memory-server ^9.0.0
  • mongoose ^7.6.3
  • soap ^1.0.0
  • telegraf ^4.14.0
  • zod 3.22.4
  • @azure/functions ^3.5.1
  • @types/chai ^4.3.6
  • @types/express ^4.17.20
  • @types/mocha ^10.0.1
  • @types/node ^20.8.9
  • @types/sinon ^17.0.0
  • @typescript-eslint/eslint-plugin ^6.9.0
  • @typescript-eslint/parser ^6.8.0
  • chai ^4.3.8
  • cross-env 7.0.3
  • eslint ^8.46.0
  • eslint-config-google ^0.14.0
  • eslint-config-prettier 9.0.0
  • husky 8.0.3
  • lint-staged 15.0.2
  • mocha ^10.0.0
  • nodemon ^3.0.1
  • prettier 3.0.3
  • sinon ^17.0.0
  • ts-mocha 10.0.0
  • ts-node ^10.7.0
  • tsconfig-paths 4.2.0
  • typescript ^5.2.2

  • Check this box to trigger a request for Renovate to run again on this repository

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.