@@ -6,6 +6,7 @@ import UserType from "@/types/user";
66import { formatDate , isEmpty } from "@/utils" ;
77import { Markup , Telegraf } from "telegraf" ;
88import { BroadcastMessageSceneName , ConfigSceneName } from "./scene" ;
9+ import { getScrapingStatus , startScraping , stopScraping } from "@/scraper" ;
910
1011const commands : {
1112 command : string ;
@@ -17,6 +18,14 @@ const commands: {
1718 command : "broadcast" ,
1819 description : "Broadcast a message to all users (admin only)" ,
1920 } ,
21+ {
22+ command : "start_scraping" ,
23+ description : "Start scraping job postings (admin only)" ,
24+ } ,
25+ {
26+ command : "stop_scraping" ,
27+ description : "Stop scraping job postings (admin only)" ,
28+ } ,
2029] ;
2130
2231const TELEGRAM_MSG_LIMIT = 3000 ;
@@ -40,7 +49,7 @@ const setup_commands = async (bot: Telegraf) => {
4049 const args = ctx . message . text . split ( " " ) ;
4150 const referrerId = args [ 1 ] ;
4251
43- const username = ctx . update . message . from . username ;
52+ const username = ctx . update . message . from . username || "Unknown" ;
4453 const userId = ctx . update . message . from . id ;
4554
4655 const existUser = await User . findOne ( {
@@ -174,6 +183,53 @@ const setup_commands = async (bot: Telegraf) => {
174183 }
175184 } ) ;
176185
186+ let canStart = false ;
187+
188+ bot . command ( "start_scraping" , async ( ctx ) => {
189+ try {
190+ const userId = ctx . update . message . from . id ;
191+ if ( config . ADMIN_ID !== userId . toString ( ) )
192+ return ctx . reply ( `🚫 This command is for admin only.` ) ;
193+
194+ const scraping = getScrapingStatus ( ) ;
195+
196+ if ( scraping ) return await ctx . reply ( "Scraping is already ongoing." ) ;
197+
198+ if ( ! canStart )
199+ return await ctx . reply ( "Scraping is not allowed to start for now." ) ;
200+
201+ await ctx . reply ( "🔍 Scraping started." ) ;
202+ startScraping ( ) ;
203+ } catch ( error ) {
204+ console . error ( "Error in /start_scraping:" , error ) ;
205+ ctx . reply ( "An error occurred. Please try again later." ) ;
206+ }
207+ } ) ;
208+
209+ bot . command ( "stop_scraping" , async ( ctx ) => {
210+ try {
211+ const userId = ctx . update . message . from . id ;
212+ if ( config . ADMIN_ID !== userId . toString ( ) )
213+ return ctx . reply ( `🚫 This command is for admin only.` ) ;
214+
215+ const scraping = getScrapingStatus ( ) ;
216+
217+ if ( ! scraping ) return await ctx . reply ( "Scraping is not ongoing." ) ;
218+
219+ canStart = false ;
220+
221+ setTimeout ( ( ) => {
222+ canStart = true ;
223+ } , 60000 ) ;
224+
225+ await ctx . reply ( "🛑 Scraping stopped." ) ;
226+ stopScraping ( ) ;
227+ } catch ( error ) {
228+ console . error ( "Error in /stop_scraping:" , error ) ;
229+ ctx . reply ( "An error occurred. Please try again later." ) ;
230+ }
231+ } ) ;
232+
177233 bot . hears ( SOURCE_URL , async ( ctx ) => {
178234 try {
179235 ctx . reply (
0 commit comments