Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Send message to Microsoft teams group via connector

Send message to Microsoft teams group via connector

Scheduled Pinned Locked Moved C#
csharpsharepointlinqgraphicscollaboration
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sdesilets
    wrote on last edited by
    #1

    Hello everybody, I pretty new to programming, what I want to do is a C# program that will send a message to a Microsoft teams group via a webhook connector of this group. I was able to achieve it with a Desktop code(with form),but I need it to be a console program, I have pretty much copy/paste the code from my Desktop project to my console project, but there something that don't work. Here is my code from my desktop project:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Text.Json;

    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace test_team_2
    {
    public partial class Form1 : Form
    {
    string[] cmdLineMe = Environment.GetCommandLineArgs();
    string webhookPdfTermine = "uri to my team groups;
    HttpClient client = new HttpClient();
    Message body = new Message();
    string finalMessage = "";

        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1\_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < cmdLineMe.Length; i++)
            {
                finalMessage = cmdLineMe\[i\];
            }
    
            sendMsg();
    
        }
    
        public class Message
        {
            public string text { get; set; }
        }
    
        private async Task SendMsgAsync()
        {
    
            MessageBox.Show(finalMessage);
            client.BaseAddress = new Uri(webhookPdfTermine);
            body.text = finalMessage;
            string serializeJson = JsonSerializer.Serialize(body);
            StringContent content = new StringContent(serializeJson, Encoding.UTF8, "application/json");
            \_ = await client.PostAsync(client.BaseAddress, content);
        }
    
        void sendMsg()
        {
            
            \_ = SendMsgAsync();
    
        }
    
    }
    

    }

    And here is my code for my console project:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text.Json;

    namespace TeamBots
    {
    class Program
    {
    string[] cmdLineMe = Environment.GetCommandLineArgs();
    string webhookPdfTermine = "uri to my team groups";
    HttpClient client = n

    L 1 Reply Last reply
    0
    • S sdesilets

      Hello everybody, I pretty new to programming, what I want to do is a C# program that will send a message to a Microsoft teams group via a webhook connector of this group. I was able to achieve it with a Desktop code(with form),but I need it to be a console program, I have pretty much copy/paste the code from my Desktop project to my console project, but there something that don't work. Here is my code from my desktop project:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Net.Http;
      using System.Net.Http.Headers;
      using System.Text;
      using System.Text.Json;

      using System.Threading.Tasks;
      using System.Windows.Forms;

      namespace test_team_2
      {
      public partial class Form1 : Form
      {
      string[] cmdLineMe = Environment.GetCommandLineArgs();
      string webhookPdfTermine = "uri to my team groups;
      HttpClient client = new HttpClient();
      Message body = new Message();
      string finalMessage = "";

          public Form1()
          {
              InitializeComponent();
          }
      
          private void Form1\_Load(object sender, EventArgs e)
          {
              for (int i = 0; i < cmdLineMe.Length; i++)
              {
                  finalMessage = cmdLineMe\[i\];
              }
      
              sendMsg();
      
          }
      
          public class Message
          {
              public string text { get; set; }
          }
      
          private async Task SendMsgAsync()
          {
      
              MessageBox.Show(finalMessage);
              client.BaseAddress = new Uri(webhookPdfTermine);
              body.text = finalMessage;
              string serializeJson = JsonSerializer.Serialize(body);
              StringContent content = new StringContent(serializeJson, Encoding.UTF8, "application/json");
              \_ = await client.PostAsync(client.BaseAddress, content);
          }
      
          void sendMsg()
          {
              
              \_ = SendMsgAsync();
      
          }
      
      }
      

      }

      And here is my code for my console project:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Net.Http;
      using System.Net.Http.Headers;
      using System.Text.Json;

      namespace TeamBots
      {
      class Program
      {
      string[] cmdLineMe = Environment.GetCommandLineArgs();
      string webhookPdfTermine = "uri to my team groups";
      HttpClient client = n

      L Offline
      L Offline
      lmoelleb
      wrote on last edited by
      #2

      The first suspicious thing I see is you call an async method SendMsgAsync() without waiting for it. This means your Main method can execute the code following myprog.sendMsg() before the message is put on the network. No problem you might think: There is no code there.... correct. And what happens to a console application when there is no more code to run. Well... it terminates the process (a bit simplified - you CAN get it to hang around, but you don't do that here, nor should you). So my guess (without debugging which is how you REALLY find out these things) is that the program simply terminates before it can send any message. You should make your Main async as well and await the async calls - then it will not terminate before completion (and you will also see any errors). You might also when googling find advise to call .Result or .Wait. Those are excellent advise for people who like to debug why there software occasionally hangs after they make a completely unrelated change. Sure you can use them if you know how it really works under the hood - but that is not a good topic for a beginner.

      S 1 Reply Last reply
      0
      • L lmoelleb

        The first suspicious thing I see is you call an async method SendMsgAsync() without waiting for it. This means your Main method can execute the code following myprog.sendMsg() before the message is put on the network. No problem you might think: There is no code there.... correct. And what happens to a console application when there is no more code to run. Well... it terminates the process (a bit simplified - you CAN get it to hang around, but you don't do that here, nor should you). So my guess (without debugging which is how you REALLY find out these things) is that the program simply terminates before it can send any message. You should make your Main async as well and await the async calls - then it will not terminate before completion (and you will also see any errors). You might also when googling find advise to call .Result or .Wait. Those are excellent advise for people who like to debug why there software occasionally hangs after they make a completely unrelated change. Sure you can use them if you know how it really works under the hood - but that is not a good topic for a beginner.

        S Offline
        S Offline
        sdesilets
        wrote on last edited by
        #3

        Thank you so much for your answer..

        lmoelleb wrote:

        The first suspicious thing I see is you call an async method SendMsgAsync() without waiting for it. This means your Main method can execute the code following myprog.sendMsg() before the message is put on the network.

        You was right, I was able to make it run by putting a Thread.Sleep(5000) after myprog.sendMsg(),is not optimal but it work for now.

        lmoelleb wrote:

        You should make your Main async as well and await the async calls - then it will not terminate before completion (and you will also see any errors). You might also when googling find advise to call .Result or .Wait. Those are excellent advise for people who like to debug why there software occasionally hangs after they make a completely unrelated change. Sure you can use them if you know how it really works under the hood - but that is not a good topic for a beginner.

        I gonna check this. Thank you so much for your help!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups