从Azure部署的bot向MS团队发送主动消息

时间:2019-09-17 12:14:35

标签: c# botframework microsoft-teams

我已经将Bot部署到了Azure,当以Azure方式连接到ms团队频道时,我能够ping Bot并接收消息,这很好。 我还在机器人中添加了主动消息传递,该消息将在频道中每隔一分钟触发一次消息。

Connecting to MS Teams

它可以在模拟器中运行,但是不能在网络聊天和MS团队中运行: 通知程序控制器未触发。

您能帮我吗? 我已将代码上传到GITHUB: https://github.com/nivinsunathree/Botv4.git

    public class FragmentA extends Fragment {

    private AdapterEventStore mAdapter;
    private List<Item> mStore;
    private final String hostName = "https://xxx.xxx.xxx";
    private View view;
    private Context context;
    private LottieAnimationView lottieAnimationView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_a, container, false);
        context = view.getContext();
        lottieAnimationView = view.findViewById(R.id.ais_lav_loading);
        lottieAnimationView.setVisibility(View.VISIBLE);
        initRecyclerView();
        return view;
    }


    private void initRecyclerView() {
        RecyclerView store = view.findViewById(R.id.ais_rv_event_store);
        store.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mStore = new ArrayList<>();
        mAdapter = new AdapterEventStore(context, mStore);
        store.setAdapter(mAdapter);
        fetchDataFromServer();
    }

    private void fetchDataFromServer() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        final Retrofit retrofit = new Retrofit.Builder()
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(hostName)
                .build();
        APIGetItem itemShop = retrofit.create(APIGetItem.class);



        Call<ModelEventExclusive> call = itemShop.getEventStore(hostName);
        call.enqueue(new Callback<ModelEventExclusive>() {
            @Override
            public void onResponse(Call<ModelEventExclusive> call, Response<ModelEventExclusive> response) {
                Log.e("Response: ", response.body().getItems().get(0).getItemName());
                mEventStore.addAll(response.body().getItems());
                mAdapter.notifyDataSetChanged();
                hideLottieAnimation();
            }

            @Override
            public void onFailure(Call<ModelEventExclusive> call, Throwable t) {
                hideLottieAnimation();
                Toast.makeText(context, "Error:"+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                Log.e("Error occurred: ", t.getMessage());
            }
        });
    }

    private void hideLottieAnimation(){
        lottieAnimationView.cancelAnimation();
        lottieAnimationView.setVisibility(View.GONE);
    }
  @Override
    public void onResume() {
        super.onResume();
        lottieAnimationView.setVisibility(View.GONE);
    }
}

一分钟后,它应在频道中触发以下消息:

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    await base.OnTurnAsync(turnContext, cancellationToken);


    System.Timers.Timer checkForTime = new System.Timers.Timer(interval60Minutes);
    checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
    checkForTime.Enabled = true;


    // Save any state changes that might have occured during the turn.
    await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
    await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}

void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
    bool timeIsReady = true;
    if (timeIsReady == true)
    {
        var url = "http://localhost:3978/api/notify";

        try
        {
            Process.Start(url);
        }
        catch
        {
            // hack because of this: https://github.com/dotnet/corefx/issues/10361
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                url = url.Replace("&", "^&");
                Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = false });
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Process.Start("xdg-open", url);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                Process.Start("open", url);
            }
            else
            {
                throw;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我通过电子邮件提到了此内容,但我将在此处将其重新发布给其他人:

您的主动消息传递在Emulator中有效,但在Teams中无效,因为您仅在ConversationReference中添加了EchoBot.OnConversationUpdateActivityAsync

here所述,Teams仅在安装机器人或用户第一次与机器人对话时才发送ConversationUpdate。 Here是测试的一种方式。

相关问题