Now, we are ready to start coding. I would split the code application into sections and for each section, I would explain the code and provide helpful comments I have.
Settings service
This is the service responsible for providing the configurations for the entire system.
Steps:
- create a
config
Folder belowsrc/app/
. - create a
config-service.ts
undersrc/app/config
. - Copy the code below to
config-service.ts
.
explanation:
- Here we define the interface
IConfigManager
Which will provide the required configuration settings. - We also define the class
ConfigManager
That implements the interfaceIConfigManager
. - In implementing the properties, we use
config
Node package to read the configurations. - That’s why we had to
import config from ‘config’;
- We decorated the classroom with the
@injectable()
Decorator – Defined byInverisfyJS
Junction package – so thatConfigManager
A ward could be injected whenever needed. - However, this is not enough for
InverisfyJS
Node package do its job. We also need to define the aSymbol
Serve as an identification token for this injection. - That’s why we added
export const ConfigManagerLocator = ConfigManager: Symbol.for(‘IConfigManager’) ;
- And we needed imports
“reflect-metadata”; import injectable from ‘inversify’;
Twitter service
This is the service responsible for integrating with Twitter APIs. It wraps around Twitter APIs – the ones we need in our project – and displays them in a specific language for a specific domain (DSL).
Steps:
- create a
twitter
Folder belowsrc/app/
. - create a
twitter-service.ts
undersrc/app/twitter
. - Copy the code below to
twitter-service.ts
.
explanation:
- Here we define the interface
ITwitterService
Which will provide theWatchToFilterStream
function. - We also define the class
TwitterService
That implements the interfaceITwitterService
. - Now, this class depends on
ConfigService
Use it to get the configuration related to Twitter API keys and tokens. - This is why we inject it into the builder as follows
@inject(ConfigManagerLocator.ConfigManager) private configManager: IConfigManager
. - This is the way to parent
InversifyJS
Inject a show ofIConfigManager
. - Then, in the constructor application, we use injected
IConfigManager
To retrieve Twitter API keys and tokens. - We also use
twit
A node package for making calls to Twitter APIs, but first we need to create an object of it that will go through the keys and tokens of the Twitter API we received from the streaming roomIConfigManager
. - For the
WatchToFilterStream
Function, we usetwit
A node package for listening to update public tweets, filter them by the keywords passed, and in case any interesting tweet is found, it will be retweeted. - God
TwitterService
The class will be injectable as well. So, we decorate it with@injectable()
designer. - Will also export the icon
export const TwitterServiceLocator = TwitterService: Symbol.for(‘ITwitterService’) ;
- And imported:
import "reflect-metadata"; import inject, injectable from 'inversify'; import ConfigManagerLocator, IConfigManager from './../config/config-service'; import Twit from 'twit';
Twitter bot service
This is the service responsible for wrapping the main bot module.
Steps:
- create a
twitter-bot
Folder belowsrc/app/
. - create a
twitter-bot.ts
undersrc/app/twitter-bot
. - Copy the code below to
twitter-bot.ts
.
explanation:
- Here we define the interface
ITwitterBot
Which will provide theInitialize
function. - We also define the class
TwitterBot
That implements the interfaceITwitterBot
. - Now, this class depends on
TwitterService
. - This is why we inject it into the builder in the following way
@inject(TwitterServiceLocator.TwitterService) private twitterService: ITwitterService
. - God
Initialize
The function simply callsWatchToFilterStream
A function that passes through an array of keywords that will be used to filter public tweets. - God
TwitterBot
The class will be injectable as well. So, we decorate it with@injectable()
designer. - Will also export the icon
export const TwitterBotLocator = TwitterBot: Symbol.for(‘ITwitterBot’) ;
. - And imported
import “reflect-metadata”; import TwitterServiceLocator from ‘../twitter/twitter-service’; import inject, injectable from “inversify”; import ITwitterService from “../twitter/twitter-service”;
.