poshbotio / PoshBot

Powershell-based bot framework
MIT License
540 stars 108 forks source link

Pasted Messages in Teams not being parsed properly #150

Closed AndrewPla closed 4 years ago

AndrewPla commented 5 years ago

Expected Behavior

Pasting !help into Teams should cause poshbot to parse the command name as help and then execute the command.

Log output would look like this

{"DataTime":"2019-02-19 18:25:47Z","Class":"Bot","Method":"HandleMessage","Severity":"Normal","LogLevel":"Debug","Message":"Parsed bot command","Data":{"Plugin":"","Command":"help",<trimmed>

Current Behavior

If you paste !help into a message with poshbot the command parser should parse away the leading /r/n from the message and then remove the exclamation mark in the beginning

{"DataTime":"2019-02-19 18:26:56Z","Class":"Bot","Method":"HandleMessage","Severity":"Normal","LogLevel":"Debug","Message":"Parsed bot command","Data":{"Plugin":"","Command":"!help",

Note that there is still an exclamation mark before help in this example.

Possible Solution

Steps to Reproduce (for bugs)

  1. Copy and paste !help and send to Poshbot in Teams

Context

It seems to me like the issue is stemming from the trimprefix() method. We are calling trimprefix on an unparsed command, which would cause it to still have the leading /r/n.

This is where I'm looking (lines 331-340) of bot.ps1

        if (-not $isBotCommand) {
            $cmdSearch = $false
            $this.LogDebug('Message is not a bot command. Command triggers WILL NOT be searched.')
        } else {
            # The message is intended to be a bot command
            $Message = $this.TrimPrefix($Message)
        }

        $parsedCommand = [CommandParser]::Parse($Message)
        $this.LogDebug('Parsed bot command', $parsedCommand)

Similar issue was raised in #109

Your Environment

AndrewPla commented 5 years ago

This may seem somewhat dirty, but adding an additional $Message = $this.TrimPrefix($Message) fixes the issue. trimprefix will remove the /r/n from the message, but it won't remove '/r/n' AND '!' if it's only called once. This is what I have as of right now and it is working.

        if (-not $isBotCommand) {
            $cmdSearch = $false
            $this.LogDebug('Message is not a bot command. Command triggers WILL NOT be searched.')
        } else {
            # The message is intended to be a bot command
            $this.LogDebug('Message before trim', $Message)

            $Message = $this.TrimPrefix($Message)
            $this.LogDebug('Trimmed', $Message)

            $Message = $this.TrimPrefix($Message)
            $this.LogDebug('Trimmed again', $Message)
        }

These are the results of my logs.

Message Before Trim "Message":"Message before trim","Data":{"Type":2,"Subtype":0,"Id":"1550606665522","Text":"\r\n!about\n",

After Being trimmed
"Message":"Trimmed","Data":{"Type":2,"Subtype":0,"Id":"1550606665522","Text":"!about"

After being trimmed twice "Message":"Trimmed again","Data":{"Type":2,"Subtype":0,"Id":"1550606665522","Text":"about"

AndrewPla commented 5 years ago

I will do some testing on this over the weekend to better determine what needs to be changed on trimprefix()

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

kevinblumenfeld commented 5 years ago

I am having this same issue. If I copy and paste !help from Teams and paste it back into Teams, I get "No command found matching [!help]" Typing the command works fine. Also, if I copy and paste from notepad or notepad++ it works properly.

devblackops commented 5 years ago

@kevinblumenfeld Teams is "trying" to be helpful and automatically pasting the formatted text. The extra characters from this formatting is seen in PoshBot and prevents it from properly matching the text to a registered command. Have you tried using CTRL+SHIFT+V to paste it unformatted?

krknopp commented 4 years ago

CTRL+SHIFT-V does work. Do we think it's possible to fix it so that's not a requirement?

devblackops commented 4 years ago

I believe I have a fix for pasted messages in https://github.com/poshbotio/PoshBot/commit/016120a92d7f3e358fa68bea0d1b3468f660afdc but could use some folks who Teams to test it. Can you pull down the latest from master, build it with ./build.ps1 -task build and test it out?

krknopp commented 4 years ago

@devblackops :boom: looks like it works now.

I wasn't smart enough to figure out how to install the 0.14.0 output of ./build.ps1 -task build over my existing 0.13.0 module that was installed via Install-Module PoshBot, so I just manually made the edits to my 0.13.0 module code and restarted PoshBot.

devblackops commented 4 years ago

Thanks for testing @krknopp. Until .0.14.0 is officially released, you can adjust your script that starts PoshBot to load the version from the build output, or you can manually copy the 0.14.0 directory to C:\Users\<username>\Documents\PowerShell\Modules\PoshBot (or wherever you have PoshBot installed).