public async System.Threading.Tasks.Task CreateTicket() { ManageApiClient client = GetApiClient(); Ticket ticket = await client.CreateTicket( "IT Nation Ticket", "IndigoStrawberryCo", SeverityEnum.High); Assert.IsTrue(ticket != null); }
protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { TelemetryClient telemetry = new TelemetryClient(); telemetry.InstrumentationKey = _configuration.GetSection("ApplicationInsights")["InstrumentationKey"]; telemetry.TrackTrace("OnMessageActivityAsync"); string activityJson = JsonConvert.SerializeObject(turnContext.Activity); string from = "unknown"; try { dynamic activity = JObject.Parse(activityJson); from = activity.from.id; } catch (Exception ex) { telemetry.TrackException(ex); } if (turnContext.Activity.Type == ActivityTypes.Message) { string message = turnContext.Activity.Text; message = message.ToLower(); ManageApiClient client = GetApiClient(); UserConfig cfg = await _storage.UserConfigAccessor.GetAsync(turnContext, () => new UserConfig()); if (string.IsNullOrEmpty(cfg.Id) == false) { // Already verified, turn the message into a ticket Ticket newTicket = new Ticket() { Company = new CompanyReference() { Id = Int32.Parse(cfg.CompanyId) }, Contact = new ContactReference() { Id = Int32.Parse(cfg.Id) }, Summary = turnContext.Activity.Text, Severity = SeverityEnum.High // what user created ticket ever had less? :) }; newTicket = await client.CreateTicket(newTicket); if (newTicket != null) { await turnContext.SendActivityAsync( MessageFactory.Text($"Thanks {cfg.Name}, a new ticket was created with ID #{newTicket.Id.ToString()}."), cancellationToken); } } else { if (message.StartsWith("cwverify")) { string phone = StrippedPhone(from); List <Contact> contacts = await client.GetContactByCommunication(phone); if (contacts.Count == 1) { cfg.Name = contacts[0].FirstName; cfg.Id = contacts[0].Id.ToString(); cfg.CompanyId = contacts[0].Company.Id.ToString(); await _storage.UserConfigAccessor.SetAsync(turnContext, cfg); await _storage.State.SaveChangesAsync(turnContext); await turnContext.SendActivityAsync( MessageFactory.Text($"Thanks {cfg.Name}, we have verified your account using {from}. You can now send messages to this number to create tickets."), cancellationToken); } else { await turnContext.SendActivityAsync( MessageFactory.Text($"Sorry, we cannot identify the account associated with {from}."), cancellationToken); } } else { // No response } } } }
protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { TelemetryClient telemetry = new TelemetryClient(); if (turnContext.Activity.Type == ActivityTypes.Message) { string activityJson = JsonConvert.SerializeObject(turnContext.Activity); string input = turnContext.Activity.Text; telemetry.TrackTrace("Activity - " + activityJson); ManageApiClient client = GetApiClient(); var parts = Regex.Matches(input, @"[\""].+?[\""]|[^ ]+") .Cast <Match>() .Select(m => m.Value) .ToList(); string command = parts[0].ToLower(); switch (command) { case "createticket": { var ticket = await client.CreateTicket(parts[3], parts[1], (Thoughtpost.ConnectWise.Manage.Models.SeverityEnum) System.Enum.Parse(typeof(Thoughtpost.ConnectWise.Manage.Models.SeverityEnum), parts[2])); string url = _configuration.GetSection("ConnectWise")["TicketUrl"]; url = url.Replace("###", ticket.Id.ToString()); var reply = MessageFactory.ContentUrl( url, @"text/html", @"Ticket #" + ticket.Id.ToString() + " - " + ticket.Summary, @"Ticket #" + ticket.Id.ToString() + " was created successfully."); await turnContext.SendActivityAsync(reply, cancellationToken); } break; case "getticket": { var ticket = await client.GetTicket(System.Int32.Parse(parts[1])); string url = _configuration.GetSection("ConnectWise")["TicketUrl"]; url = url.Replace("###", ticket.Id.ToString()); string lastUpdate = ticket.Info["lastUpdated"]; System.DateTime dtUpdate = DateTime.Parse(lastUpdate); StringBuilder sb = new StringBuilder(); sb.AppendLine($"**Ticket #{ticket.Id} - {ticket.Summary}**"); sb.AppendLine(""); sb.AppendLine($"**Status: ** {ticket.Status.Name}"); sb.AppendLine(""); sb.AppendLine($"**Severity: ** {ticket.Severity.Value.ToString()}"); sb.AppendLine(""); sb.AppendLine($"**Last Updated: ** {dtUpdate.ToString()}"); sb.AppendLine(""); sb.AppendLine($"**Contact: ** {ticket.ContactName} ({ticket.ContactEmailAddress})"); sb.AppendLine(""); sb.AppendLine($"Click here for the complete ticket: [Link]({url})"); var reply = MessageFactory.Text(sb.ToString()); await turnContext.SendActivityAsync(reply, cancellationToken); } break; default: { } break; } } else { await turnContext.SendActivityAsync( MessageFactory.Text($"Sorry, I didn't understand that."), cancellationToken); } }