1.0.0-alpha.9 • Published 1 year ago

ezrun-telegram v1.0.0-alpha.9

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

ezrun-telegram

Support telegram API via telegraf librarry

Features:

  1. Send a text message via telegram bot
  2. Send a photo via telegram bot
  3. Send a document via telegram bot
  4. Send a sticker via telegram bot
  5. Handle a telegram command (/start, /help ...)
  6. Handle a text in the chat
  7. Handle events in the telegram

Tag details

TagsDescription
ezrun-telegramDeclare a global telegram bot which is reused in the others
ezrun-telegram'actionHandle callback in inline keyboard
ezrun-telegram'commandHandle command in chat. Example: "/start", "/custom" ...
ezrun-telegram'hearsIt's trigged when text in the chat is matched in the "text" property
ezrun-telegram'onListen events directly from telegram. Example: "sticker", "text"...
ezrun-telegram'sendSend/Edit/Reply a message in telegram
ezrun-telegram'sendDocumentSend a document file in telegram
ezrun-telegram'sendPhotoSend a photo in telegram
ezrun-telegram'sendStickerSend a photo in telegram
ezrun-telegram'stopStop telegram bot

ezrun-telegram

Declare a global telegram bot which is reused in the others

Example:

  - ezrun-telegram:
      token: ${BOT_TOKEN}
      runs:
        - ezrun-telegram'send:
            title: Send a hi message
            chatID: ${TELEGRAM_CHAT_ID}
            # chatIDs:
            #  - ${TELEGRAM_CHAT_ID_1}
            #  - ${TELEGRAM_CHAT_ID_2}
            text: Hi there

        - ezrun-telegram'command:
            title: Handle custom command
            name: custom           # /custom
            runs:
              - echo: ${this.parentState.botCtx.message.text}

        - ezrun-telegram'hears:
            title: Handle when user say hi
            text: Hi
            runs:
              - echo: ${this.parentState.botCtx.message.text}

ezrun-telegram'action

Handle callback in inline keyboard

Example:

  - ezrun-telegram'action:
      token: ${BOT_TOKEN}
      title: Handle inline keyboard when user pick one
      name: callback
      runs:
        # this.parentState.botCtx: is ref to telegraf in https://www.npmjs.com/package/telegraf
        - vars:
            callbackData: ${this.parentState.botCtx.update.callback_query.data}   # => VN/US
        - echo: ${vars.callbackData}
        - exec'js: |
            this.parentState.botCtx.reply('Picked ' + vars.callbackData)

  - ezrun-telegram'send:
      token: ${BOT_TOKEN}
      chatID: ${CHAT_ID}
      text: Send a message to help users to choose a language
      opts:
        reply_markup:
          one_time_keyboard: true
          inline_keyboard:
            -
              - text: VietNam
                callback_data: VN
              - text: US
                callback_data: US

ezrun-telegram'command

Handle command in chat. Example: "/start", "/custom" ...

Example:

  - ezrun-telegram'command:
      token: ${BOT_TOKEN}
      title: Handle custom command
      name: custom           # /custom
      runs:
        # this.parentState.botCtx: is ref to telegraf in https://www.npmjs.com/package/telegraf
        - vars:
            message: ${this.parentState.botCtx.message.text}
        - echo: ${vars.message}
        - exec'js: |
            this.parentState.botCtx.reply('This is custom command')

ezrun-telegram'hears

It's trigged when text in the chat is matched in the "text" property

Example:

  - ezrun-telegram'hears:
      token: ${BOT_TOKEN}
      title: User say hi
      text: Hi
      runs:
        # this.parentState.botCtx: is ref to telegraf in https://www.npmjs.com/package/telegraf
        - vars:
            message: ${this.parentState.botCtx.message.text}
        - echo: ${vars.message}
        - exec'js: |
            this.parentState.botCtx.reply('Hi there')

ezrun-telegram'on

Listen events directly from telegram. Example: "sticker", "text"...

Example:

  - ezrun-telegram'on:
      token: ${BOT_TOKEN}
      title: Handle text in the chat
      filter: text
      runs:
        # this.parentState.botCtx: is ref to telegraf in https://www.npmjs.com/package/telegraf
        - vars:
            message: ${this.parentState.botCtx.message.text}
        - echo: ${vars.message}
        - exec'js: |
            this.parentState.botCtx.reply('Hi there')

ezrun-telegram'send

Send/Edit/Reply a message in telegram

Example:

  - ezrun-telegram'send:
      token: ${BOT_TOKEN}
      chatID: ${TELEGRAM_CHAT_ID}
      # chatIDs:
      #  - ${TELEGRAM_CHAT_ID_1}
      #  - ${TELEGRAM_CHAT_ID_2}
      text: Hi there
      opts:
        reply_markup:
          inline_keyboard:
            - - text: Button 1
                callback_data: ACTION_1
              - text: Button 2
                callback_data: ACTION_2

Reuse bot in the ezrun-telegram

  - ezrun-telegram:
      token: ${BOT_TOKEN}
      runs:
        - ezrun-telegram'send:
            chatID: ${TELEGRAM_CHAT_ID}
            text: Hi there
            vars:
              messageID: ${this.result.message_id}

Edit a message

  - ezrun-telegram'send:
      token: ${BOT_TOKEN}
      editMessageID: ${vars.messageID}        # Message ID to edit
      chatID: ${TELEGRAM_CHAT_ID}
      text: Hi again

Remove a message

  - ezrun-telegram'send:
      token: ${BOT_TOKEN}
      removeMessageID: ${vars.messageID}      # Message ID to remove
      chatID: ${TELEGRAM_CHAT_ID}

Reply a message

  - ezrun-telegram'send:
      token: ${BOT_TOKEN}
      replyMessageID: ${vars.messageID}       # Message ID to reply
      chatID: ${TELEGRAM_CHAT_ID}
      text: Hi again

ezrun-telegram'sendDocument

Send a document file in telegram

Example:

  - ezrun-telegram'sendDocument:
      token: ${BOT_TOKEN}
      chatID: ${TELEGRAM_CHAT_ID}
      # chatIDs:
      #  - ${TELEGRAM_CHAT_ID_1}
      #  - ${TELEGRAM_CHAT_ID_2}
      file: http://.../README.md                # "file" is a path of local file or a URL
      caption: This is a image caption          # File caption

Reuse bot in the ezrun-telegram

  - ezrun-telegram:
      token: ${BOT_TOKEN}
      runs:
        - ezrun-telegram'sendDocument:
            chatID: ${TELEGRAM_CHAT_ID}
            file: http://.../README.md          # "file" is a path of local file or a URL
            caption: This is a image caption    # File caption

ezrun-telegram'sendPhoto

Send a photo in telegram

Example:

  - ezrun-telegram'sendPhoto:
      token: ${BOT_TOKEN}
      chatID: ${TELEGRAM_CHAT_ID}
      # chatIDs:
      #  - ${TELEGRAM_CHAT_ID_1}
      #  - ${TELEGRAM_CHAT_ID_2}
      file: http://.../image.jpg                # "file" is a path of local file or a URL
      caption: This is a image caption          # File caption

Reuse bot in the ezrun-telegram

  - ezrun-telegram:
      token: ${BOT_TOKEN}
      runs:
        - ezrun-telegram'sendPhoto:
            chatID: ${TELEGRAM_CHAT_ID}
            file: /tmp/image.jpg                # "file" is a path of local file or a URL
            caption: This is a image caption    # File caption

ezrun-telegram'sendSticker

Send a photo in telegram

Example:

  - ezrun-telegram'sendSticker:
      token: ${BOT_TOKEN}
      chatID: ${TELEGRAM_CHAT_ID}
      # chatIDs:
      #  - ${TELEGRAM_CHAT_ID_1}
      #  - ${TELEGRAM_CHAT_ID_2}
      sticker: http://.../image.jpg             # "file" is a character, path of local file or a URL
      caption: This is a image caption          # File caption

Reuse bot in the ezrun-telegram

  - ezrun-telegram:
      token: ${BOT_TOKEN}
      runs:
        - ezrun-telegram'sendSticker:
            chatID: ${TELEGRAM_CHAT_ID}
            sticker: /tmp/image.jpg             # "file" is a character, path of local file or a URL
            caption: This is a image caption    # File caption

ezrun-telegram'stop

Stop telegram bot

Example:

  - ezrun-telegram:
      token: ${BOT_TOKEN}
      runs:
        - ezrun-telegram'send:
            title: Send a hi message
            chatID: ${TELEGRAM_CHAT_ID}
            text: Hi there

        - ezrun-telegram'stop:            # Stop bot here
  - echo: Keep doing something after the bot is stoped here

Have fun :)

1.0.0-alpha.9

1 year ago

1.0.0-alpha.8

1 year ago

1.0.0-alpha.7

1 year ago

1.0.0-alpha.6

1 year ago

1.0.0-alpha.5

1 year ago

1.0.0-alpha.4

1 year ago

1.0.0-alpha.3

1 year ago

1.0.0-alpha.2

1 year ago

1.0.0-alpha.1

1 year ago

1.0.0-alpha.0

1 year ago

0.0.0-alpha.7

1 year ago

0.0.0-alpha.6

1 year ago

0.0.0-alpha.5

1 year ago

0.0.0-alpha.4

1 year ago

0.0.0-alpha.3

1 year ago

0.0.0-alpha.2

1 year ago

0.0.0-alpha.1

1 year ago

0.0.0-alpha.0

1 year ago