2017-03-24 19 views
0

Skype、WebChat、およびFBチャットでエラーが発生した場合、次のコード行で失敗します。私はアプリの設定とweb.configでMS App IDとパスワードを入力しようとしました。これはエミュレータで問題なく正常に動作します。私はリモートデバッグを実行したとき、私は、Skype、これはウェブチャットに失敗したことがわかった、とのコード行でFBメッセンジャー:ステータスコードでMicrosoft App ID xxxの認証に失敗しました禁止および理由フレーズ「禁止」

await connector.Conversations.ReplyToActivityAsync(reply1); 

予想通り、スマートアクセサリーがオンとオフをされているので、私のボットがSmartThingsと統合されているが、チャットで返されるべき返答が失敗しているようです。私は新しいボットサービスアプリケーションを作成しようとしましたが、失敗します。すべてのコードを含む

更新:

using System; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web.Http; 
using Microsoft.Bot.Connector; 
using System.Net.Http.Headers; 
using System.Threading; 
using Microsoft.IdentityModel.Protocols; 
using System.Configuration; 

namespace FirstBotApp 
{ 
[BotAuthentication] 

public class Switches 
{ 
    public string name { get; set; } 
    public string value { get; set; } 

} 



public class MessagesController : ApiController 
{ 

    static HttpClient client = new HttpClient(); 
    static HttpResponseMessage responsemain = null; 
    static int j = 0; 

    static void ShowSwitches(Switches[] switches) 
    { 

     for (int i = switches.Length - 1; i >= 0; i--) 
     { 
      Console.WriteLine($"Name: {switches[i].name}\tPrice: {switches[i].value}\t"); 
     } 
     return; 
    } 

    static async Task<Switches[]> GetSwitchesAsync(string path) 
    { 
     Switches[] switches = null; 
     responsemain = await client.GetAsync(path); 
     //Console.WriteLine(client.GetAsync(path)); 
     //Console.WriteLine(response); 
     if (responsemain.IsSuccessStatusCode) 
     { 
      //Console.WriteLine($"Successful response: {response.StatusCode}"); 
      var response1 = await client.GetStringAsync(""); 
      Console.WriteLine($"{response1}"); 

      switches = await responsemain.Content.ReadAsAsync<Switches[]>(); 

      //JObject j = JObject.Parse(response1); 
     } 
     else Console.WriteLine($"Error in response: {responsemain.StatusCode}"); 

     return switches; 
    } 

    static async Task<Switches> UpdateSwitchesAsync(Switches switches) 
    { 
     Console.WriteLine("Turning off the light"); 
     HttpResponseMessage response = await client.PutAsJsonAsync($"switches/off?room=LivingRoom", switches); 
     response.EnsureSuccessStatusCode(); 
     Console.WriteLine($"The response from the server was: {response.StatusCode}"); 

     // Deserialize the updated switches from the response body. 
     switches = await response.Content.ReadAsAsync<Switches>(); 
     Thread.Sleep(10000); 
     Console.WriteLine($"Turning on the light"); 

     response = await client.PutAsJsonAsync($"switches/on?room=LivingRoom", switches); 
     Console.WriteLine($"The response from the server was: {response.StatusCode}"); 
     Thread.Sleep(10000); 
     return switches; 
    } 

    static async Task<HttpStatusCode> DeleteSwitchesAsync(string id) 
    { 
     HttpResponseMessage response = await client.DeleteAsync($"api/switchess/{id}"); 
     return response.StatusCode; 
    } 


    /// <summary> 
    /// POST: api/Messages 
    /// Receive a message from a user and reply to it 
    /// </summary> 
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity) 
    { 

     Switches[] switches = null; 
     ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); 
     //string appdId = AppSettings.Settings["MicrosoftAppId"]; 
     //string appPassword = ConfigurationManager.AppSettings["MicrosoftAppPassword"]; 
     if (j == 0) 
     { 
      //client.Timeout = TimeSpan.FromSeconds(4); 

      //Declaration of client and Switches variable 

      string accessToken = "xxxxx"; 
      client.BaseAddress = new Uri("xxxxx"); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); 
      j++; 
      //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Authorization", "Bearer " + accessToken); 
     } 

     if (activity.Type == ActivityTypes.Message) 
     { 
      //, appdId, appPassword); 
      //Switches switches = new Switches; 
      Activity reply1;// = activity.CreateReply($"breakpoint1"); 
          //await connector.Conversations.ReplyToActivityAsync(reply1); 


      switch (activity.Text.ToLower()) 
      { 
       case"turn on livingroom": 
        try 
        { 
         Console.WriteLine("Turning on the light"); 
         responsemain = await client.PutAsJsonAsync($"switches/on?room=LivingRoom", switches); 
         responsemain.EnsureSuccessStatusCode(); 
         //Console.WriteLine($"The response from the server was: {responsemain.StatusCode}"); 

         // Deserialize the updated product from the response body. 
         switches = await responsemain.Content.ReadAsAsync<Switches[]>(); 
         reply1 = activity.CreateReply($"Successfully turned on LivingRoom Light"); 
         await connector.Conversations.ReplyToActivityAsync(reply1); 
        } 
        catch 
        { 
         reply1 = activity.CreateReply($"Error"); 
         await connector.Conversations.ReplyToActivityAsync(reply1); 
        } 
        break; 


       case "turn off livingroom": 
        try 
        { 
         Console.WriteLine("Turning off the light"); 
         responsemain = await client.PutAsJsonAsync($"switches/off?room=LivingRoom", switches); 
         responsemain.EnsureSuccessStatusCode(); 
         Console.WriteLine($"The response from the server was: {responsemain.StatusCode}"); 

         // Deserialize the updated product from the response body. 
         switches = await responsemain.Content.ReadAsAsync<Switches[]>(); 
         reply1 = activity.CreateReply($"Successfully turned off LivingRoom Light"); 
         await connector.Conversations.ReplyToActivityAsync(reply1); 
        } 

        catch 
        { 
         reply1 = activity.CreateReply($"Error"); 
         await connector.Conversations.ReplyToActivityAsync(reply1); 
        } 
        break; 


       default: //"What lights are on?": 
        try 
        { 
         switches = await GetSwitchesAsync(""); 
         //Console.WriteLine($"About to show the product"); 
         ShowSwitches(switches); 
         //await connector.Conversations.ReplyToActivityAsync(switches[].ToString); 

         for (int i = switches.Length - 1; i >= 0; i--) 
         { 
          reply1 = activity.CreateReply($"Room: ");//{switches[i].name}\tStatus: {switches[i].value}\t"); 
          responsemain.EnsureSuccessStatusCode(); 

          await connector.Conversations.ReplyToActivityAsync(reply1); 
         } 



         break; 
        } 
        catch 
        { 

        } 
        break; 




      } 

      // calculate something for us to return 
      //int length = (activity.Text ?? string.Empty).Length; 

      // return our reply to the user 
      //Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters"); 
      //await connector.Conversations.ReplyToActivityAsync(reply); 
     } 
     else 
     { 
      HandleSystemMessage(activity); 
     } 
     // var response = Request.CreateResponse(HttpStatusCode.OK); 

     return responsemain; 
    } 

    static Activity HandleSystemMessage(Activity message) 
    { 
     if (message.Type == ActivityTypes.DeleteUserData) 
     { 
      // Implement user deletion here 
      // If we handle user deletion, return a real message 
     } 
     else if (message.Type == ActivityTypes.ConversationUpdate) 
     { 
      // Handle conversation state changes, like members being added and removed 
      // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info 
      // Not available in all channels 
     } 
     else if (message.Type == ActivityTypes.ContactRelationUpdate) 
     { 
      // Handle add/remove from contact lists 
      // Activity.From + Activity.Action represent what happened 
     } 
     else if (message.Type == ActivityTypes.Typing) 
     { 
      // Handle knowing tha the user is typing 
     } 
     else if (message.Type == ActivityTypes.Ping) 
     { 
     } 

     return null; 
    }`` 
    //client.dispose(); 

} 
} 
+0

私はそれを再現するための詳細が含まれている必要がありますので、オフトピックとして、この質問を閉じるために投票しています。 http://stackoverflow.com/questions/42611413/bot-framework-emulator-not-working/42729525#42729525から何かが役立つかどうかを確認してください。 –

+0

@MrBrooks特に "reply1"がどこから来たのか、それをどのように生成するのか、もっとコードを追加してください。 –

+0

@JustShadowが私のフルコードで投稿を更新しました – MrBrooks

答えて

2

私はリモートデバッグといくつかの追加のトラブルシューティング後にエラーを見つけました。 @ JimLewallenのレスポンスに基づいて、私はコネクターオブジェクトの "Credentials"部分を詳しく調べました。 Botframeworkのバージョン3.0のOAuthScope(リモートデバッガのローカルのコネクタ - >認証 - > OAuthScope)は、 "graph.microsoft.com/"を指していたはずのapi.botframework.com/.defaultを指していました。デフォルト"。ボットテンプレートを使用してVisual Studiosで新しいボットサービスアプリケーションを作成したとき、OAuthScopeは正しいエンドポイントを示しました。

無効な値: OAuthScope「api.botframework.com/.default」

正しい値:Image from the Remote Debugger showing the correct OAuthScope

関連する問題