Skip to content

Instantly share code, notes, and snippets.

@Siedlerchr
Last active June 8, 2020 14:20
Show Gist options
  • Select an option

  • Save Siedlerchr/a9446c64d08033e3d68e7b9515d3255f to your computer and use it in GitHub Desktop.

Select an option

Save Siedlerchr/a9446c64d08033e3d68e7b9515d3255f to your computer and use it in GitHub Desktop.

Revisions

  1. Siedlerchr revised this gist Jun 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TimeoutTelegrafjsBot.ts
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ textScence.enter((ctx: MyContext) => {

    const textPromise = new Promise<MyContext>((resolve, reject) => {
    textScence.on("text", (ctx: MyContext) => {
    ctx.session.text = this.getText(ctx);
    ctx.session.text = ctx.message.text;
    resolve(ctx)
    });
    })
  2. Siedlerchr revised this gist Jun 8, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions TimeoutTelegrafjsBot.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import * as util from 'util'
    const setTimeoutPromise = util.promisify(setTimeout);
    const TIMEOUT_MILLI_SECONDS = 10000;

    export interface MyContext extends SceneContextMessageUpdate {
  3. Siedlerchr revised this gist Jun 8, 2020. 1 changed file with 41 additions and 36 deletions.
    77 changes: 41 additions & 36 deletions TimeoutTelegrafjsBot.ts
    Original file line number Diff line number Diff line change
    @@ -10,40 +10,45 @@ const localSession = new LocalSession({
    });

    const textScence = new Scene("textScene");
    textScence.enter((ctx: MyContext) => {
    const timeout = setTimeoutPromise<MyContext>(TIMEOUT_MILLI_SECONDS, ctx);

    ctx.reply("Please send some text");

    const textPromise = new Promise<MyContext>((resolve, reject) => {
    textScence.on("text", (ctx: MyContext) => {
    ctx.session.text = this.getText(ctx);
    resolve(ctx)
    });
    })

    Promise.race([textPromise, timeout]).then(data => {
    if (data.session.text) {
    // swtich to next scene
    this.switchScene(data);
    }
    else {
    data.reply("Timeout! No addtional text received. Exiting);
    const key = localSession.getSessionKey(ctx)
    localSession.saveSession(key, null);
    }
    }).catch(e => {
    console.log("Got error in text scene", e);
    const key = localSession.getSessionKey(ctx)
    localSession.saveSession(key, null);
    })
    textScence.enter((ctx: MyContext) => {
    const timeout = setTimeoutPromise<MyContext>(TIMEOUT_MILLI_SECONDS, ctx);

    ctx.reply("Please send some text");

    const textPromise = new Promise<MyContext>((resolve, reject) => {
    textScence.on("text", (ctx: MyContext) => {
    ctx.session.text = this.getText(ctx);
    resolve(ctx)
    });

    this.bot.on('photo', async (ctx) => {
    console.log("START->Photo");

    //get photo ...
    ctx.scence.enter("textScene")
    })


    })

    Promise.race([textPromise, timeout]).then(data => {
    if (data.session.text) {
    // swtich to next scene
    this.switchScene(data);
    }
    else {
    data.reply("Timeout! No addtional text received. Exiting);
    const key = localSession.getSessionKey(ctx)
    localSession.saveSession(key, null);
    }
    }).catch(e => {
    console.log("Got error in text scene", e);
    const key = localSession.getSessionKey(ctx)
    localSession.saveSession(key, null);
    })
    });

    this.bot.on('photo', async (ctx) => {
    console.log("START->Photo");

    //get photo ...
    ctx.scence.enter("textScene")
    })

    const stage = new Stage(["textScene"])
    stage.command('cancel', leave());

    this.bot.use(stage.middleware())
    this.bot.start((ctx) => ctx.reply('Welcome!'))

  4. Siedlerchr created this gist Jun 8, 2020.
    49 changes: 49 additions & 0 deletions TimeoutTelegrafjsBot.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    const TIMEOUT_MILLI_SECONDS = 10000;

    export interface MyContext extends SceneContextMessageUpdate {
    session: any,
    }

    const localSession = new LocalSession({
    database: 'example_db.json',
    property: 'session'
    });

    const textScence = new Scene("textScene");
    textScence.enter((ctx: MyContext) => {
    const timeout = setTimeoutPromise<MyContext>(TIMEOUT_MILLI_SECONDS, ctx);

    ctx.reply("Please send some text");

    const textPromise = new Promise<MyContext>((resolve, reject) => {
    textScence.on("text", (ctx: MyContext) => {
    ctx.session.text = this.getText(ctx);
    resolve(ctx)
    });
    })

    Promise.race([textPromise, timeout]).then(data => {
    if (data.session.text) {
    // swtich to next scene
    this.switchScene(data);
    }
    else {
    data.reply("Timeout! No addtional text received. Exiting);
    const key = localSession.getSessionKey(ctx)
    localSession.saveSession(key, null);
    }
    }).catch(e => {
    console.log("Got error in text scene", e);
    const key = localSession.getSessionKey(ctx)
    localSession.saveSession(key, null);
    })
    });

    this.bot.on('photo', async (ctx) => {
    console.log("START->Photo");

    //get photo ...
    ctx.scence.enter("textScene")
    })