poshbotio / PoshBot

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

Teams Backend stopped working #244

Closed golubenkoff closed 1 year ago

golubenkoff commented 2 years ago

Expected Behavior

Connected to Teams and Members Loaded

Current Behavior

Message":"Error authenticating to Teams" Data":{"CommandName":"Invoke-RestMethod" Message":"The remote server returned an error: (404) Not Found." RequestUri":"https://webchat.botframework.com/v3/conversations/19:262ca4f4ea7a9f172c5ee99d4aa@thread.tacv2/members/"

Possible Solution

I think it is connected with API deprecation - is it possible to somehow quickly fix this? See url below with possible changes

Steps to Reproduce (for bugs)

Check this: https://docs.microsoft.com/en-us/microsoftteams/platform/resources/team-chat-member-api-changes https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/get-teams-context?tabs=json#tabpanel_CeZOj-G++Q_json

I think issue here: [void]LoadUsers() Line 9615: $uri = "$($this.ServiceUrl)v3/conversations/$($this.TeamId)/members/"

Context

Your Environment

golubenkoff commented 2 years ago

Managed to fix this via: TeamsBackend.ps1

Here:

Populate the list of users the team

[void]LoadUsers() 

Replace this:

$this.LogDebug('Getting Teams users')

$uri = "$($this.ServiceUrl)v3/conversations/$($this.TeamId)/members/"
$headers = @{
    Authorization = "Bearer $($this.Connection._AccessTokenInfo.access_token)"
}
$members = Invoke-RestMethod -Uri $uri -Headers $headers
$this.LogDebug('Finished getting Teams users')

To this:

$this.LogDebug('Getting Teams users')

$uri = "$($this.ServiceUrl)v3/conversations/$($this.TeamId)/pagedmembers?pageSize=500"
$headers = @{
    Authorization = "Bearer $($this.Connection._AccessTokenInfo.access_token)"
}

$members = @()
do {
    $Results = ''
    $StatusCode = ''
    do {
        try {
            $Results = Invoke-RestMethod -Headers $headers -Uri $Uri -UseBasicParsing -Method 'GET' -ContentType 'application/json'

            $StatusCode = $Results.StatusCode
        } catch {
            $StatusCode = $_.Exception.Response.StatusCode.value__

            if ($StatusCode -eq 429) {
                $this.LogDebug('Got throttled by Microsoft. Sleeping for 45 seconds...')
                Start-Sleep -Seconds 45
            } else {
                $this.LogDebug("Error Populating the list of users for the team: $($_.Exception.Message)")
            }
        }
    } while ($StatusCode -eq 429)
    if ($Results.continuationToken) {
        $uri = "$($this.ServiceUrl)v3/conversations/$($this.TeamId)/pagedmembers?pageSize=500&continuationToken=$($Results.continuationToken)"
        $members += $Results.members
    } else {
        $members += $Results.members
    }
} while ($Results.continuationToken)
$this.LogDebug('Finished getting Teams users') 

Checked on my environment - with 8000+ users - all is OK.

golubenkoff commented 2 years ago

Check Fixed Here: https://github.com/golubenkoff/PoshBot/commit/787fafce7d45e9e186d68cc2a44338ccf9ae3119

devblackops commented 2 years ago

@golubenkoff Mind doing a PR with the fix?

golubenkoff commented 1 year ago

https://github.com/poshbotio/PoshBot/pull/248#issue-1316775315

mengdahuang commented 1 year ago

@golubenkoff Hi, golubenkoff could you tell us does the poshbot (teams bot) still working normal or not? i got the same issue PoshBot not receiving webhook information in Teams console, hope you can help us. thanks a lot, have a nice day.