Skype bot (translation from VB) not working - c#

I was following a tutorial on youtube on how to create a simple Skype bot. It was written in VB and with my limited knowledge I did my best to recreate it in C#
I stumbled upon "handles" which I can only assume is related to the eventhandler in C#
This is the code I've got so far but when I message myself from another skype account it doesn't respond. I've made sure to accept the little popup on skype that allows 3rd party software.
public partial class Form1 : Form
{
Skype oSkype = new Skype();
string trigger = "!";
public Form1()
{
InitializeComponent();
oSkype.Attach(7, false);
oSkype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(oSkype_MessageStatus);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void oSkype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
{
if (Status == TChatMessageStatus.cmsReceived || Status == TChatMessageStatus.cmsSent)
{
string msg = pMessage.Body;
Chat c = pMessage.Chat;
if (msg.StartsWith(trigger))
{
listBox1.Items.Add(DateTime.Now.ToLongTimeString() + ": " + pMessage.Sender.Handle + " sent you a message");
msg = msg.Remove(0, 1).ToLower();
if (msg == "test")
{
c.SendMessage("Test");
}
else
{
c.SendMessage("Unrecognizable command.");
}
}
}
}
}
The code from the tutorial that I was following had this instead:
oSkype_MessageStatus(pMessage as ChatMessage, Status as TChatMessageStatus) Handles oSkype.MessageStatus
The closest to what I could come to implement this in c# was to add the void to the eventhandler in public Form1() which you can see in my code.
Thanks in advance!

Skype4Com's chat functions are not supported in the newer Skype versions. They were deprecated somewhere in-between 2013-2014.
From Skype's blog post Feature evolution and support for the Skype Desktop API:
Iā€™m happy to share that we will be extending support for two of the most widely used features ā€“ call recording and compatibility with hardware devices ā€“ until we determine alternative options or retire the current solution. Although chat via third party applications, will cease to work as previously communicated.

It has been a while since I have worked with COM Skype bots, but your code seems to be fine.
Nevertheless, I would suggest you to move to a modern approach on bots. Please check out the new Microsoft Bot Framework

Related

Ozeki VoIP not receiving audio

I have been working on a Softphone project in c# and have been using Ozeki VoIP SDK.
So far I have got the softphone successfully registering and making outbound calls. However, I can not hear the called party but they can hear me. (Me being the softphone and called party being a Cisco SPA504G Phone)
2-Way audio is fine when using other applications (Such as X-Lite)
The SIP Trace shows I am sending but not receiving any RTP packages, is this something I have done wrong in my code (See below) or an issue on the PBX side. (Thirdlane PBX)
void CreateCall()
{
string numberToDial = txtNum.Text;
logOutput("Call " + numberToDial);
txtLog.ScrollToCaret();
call = softphone.CreateCallObject(phoneLine, numberToDial);
call.CallStateChanged += call_CallStateChanged;
call.Start();
}
private void SetupDevices()
{
connector.Connect(microphone, mediaSender);
connector.Connect(mediaReceiver, speaker);
mediaSender.AttachToCall(call);
mediaReceiver.AttachToCall(call);
microphone.Start();
speaker.Start();
}
void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
if (e.State == CallState.Answered)
{
WireUpCallEvents();
}
if (e.State == CallState.Completed)
{
WireDownCallEvents();
}
logOutput("Call state: " + e.State);
}
private void WireUpCallEvents()
{
call.CallStateChanged += (call_CallStateChanged);
SetupDevices();
}
private void WireDownCallEvents()
{
call.CallStateChanged -= (call_CallStateChanged);
}
After trying another extension on another PBX I found that the issue was caused by the additional setting "nat" not being set. Once I set this with the value "yes" I was able to get 2-way audio on both parties.
This was a Thridlane PBX so the setting was in "Selected Tenants PBX" => "Extensions and Contacts" => "User Extensions" => (The extension number I was using) => Phone.

Create Windows Session programmatically from Console or Windows Service

How can I programmatically log in to windows to create a Windows Logon Session?
I need a way that works from a WinForms app, from a Console app, and (most important) from a Windows Service.
One other requirement is that I need it to work on a the local system that the program/service is running on and also for remote systems.
If there's a way to do this using pInvoke/Win32 API I am open to that too.
I found these similar questions/answers in my research:
Programmatically create and launch and RDP session (without gui)
The answer here says it's possible but and gives a link but the sample code from the link doesn't work
Create a Windows Session from a service via the Win32 API
No Solution to the question asked
Create Windows session programmatically
No Solution but the OP mentioned in a comment that http://freerdp.com worked for him.
I've created a simple utility that I believe meets all the requirements in the question. You'll need to add a COM reference to Microsoft Terminal Services Active Client 1.0 Type Library (ActiveX).
I thought it might not work for creating a session on the local machine but I tested in in 2012R2 running as a Service and it actually can. The same exact method can be called from a WinForms app or from a Console app. When launched from a WinForms or Console app, the a form is shown for a few seconds so I made sure to set the control to enabled = false so it can't be interacted with.
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AxMSTSCLib;
namespace Utility.RemoteDesktop
{
public class Client
{
private int LogonErrorCode { get; set; }
public void CreateRdpConnection(string server, string user, string domain, string password)
{
void ProcessTaskThread()
{
var form = new Form();
form.Load += (sender, args) =>
{
var rdpConnection = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
form.Controls.Add(rdpConnection);
rdpConnection.Server = server;
rdpConnection.Domain = domain;
rdpConnection.UserName = user;
rdpConnection.AdvancedSettings9.ClearTextPassword = password;
rdpConnection.AdvancedSettings9.EnableCredSspSupport = true;
if (true)
{
rdpConnection.OnDisconnected += RdpConnectionOnOnDisconnected;
rdpConnection.OnLoginComplete += RdpConnectionOnOnLoginComplete;
rdpConnection.OnLogonError += RdpConnectionOnOnLogonError;
}
rdpConnection.Connect();
rdpConnection.Enabled = false;
rdpConnection.Dock = DockStyle.Fill;
Application.Run(form);
};
form.Show();
}
var rdpClientThread = new Thread(ProcessTaskThread) { IsBackground = true };
rdpClientThread.SetApartmentState(ApartmentState.STA);
rdpClientThread.Start();
while (rdpClientThread.IsAlive)
{
Task.Delay(500).GetAwaiter().GetResult();
}
}
private void RdpConnectionOnOnLogonError(object sender, IMsTscAxEvents_OnLogonErrorEvent e)
{
LogonErrorCode = e.lError;
}
private void RdpConnectionOnOnLoginComplete(object sender, EventArgs e)
{
if (LogonErrorCode == -2)
{
Debug.WriteLine($" ## New Session Detected ##");
Task.Delay(10000).GetAwaiter().GetResult();
}
var rdpSession = (AxMsRdpClient9NotSafeForScripting)sender;
rdpSession.Disconnect();
}
private void RdpConnectionOnOnDisconnected(object sender, IMsTscAxEvents_OnDisconnectedEvent e)
{
Environment.Exit(0);
}
}
}
On a side note I found this question that says there may be a way to use the ActiveX control (for RDP) without using a windows form at all. I saw the example they gave and I was unsure hot to use their code for this situation.
ActiveX control without a form
If there's anyone out there who understands how to do this without hosting the ActiveX control on a Form please post an example.

How do I know if a telegram user joined my channel?

I am writing a C# desktop app.in this app I write a telegram Id of a user and it says that user is member of the channel or not. my bot is admin of the channel.
I use telegram.bot v9 nugget and searched about this issue all day.
I tried using GetChatMembersCountAsync() in v13 and a lot of other methods but didn't work.
static private Api bot = new Api("Token");
long id;
string channel="#ChannelName";
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
id = long.Parse(textBox7.Text);
if (IsMember(id,channel))
MessageBox.Show("This user is member of channel");
else
MessageBox.Show("This user is not a member of channel");
}
private bool IsMember(long id,string channelName)
{
//??????????????
}
Is there a method for a telegram bot access to list of members of a channel? what should I write in the IsMember() method?
Thank you very much
This problem solved by updating telegram.bot nugget to v10 and using GetChatMemberAsync method.
private bool IsMember(long id,string channelName)
{
var t = bot.GetChatMemberAsync(channelName, id);
if (t.Result.Status.ToString().Length > 25)
return false;
return true;
}
thank you
You can use getChatMember method to do that, see following example.
Regarding to Telegram Bot API documentation currently there is no method available for bots to get a list of chat members (channel or group).
Here is a small trick:
You can check the updates (messages) came from Telegram to your webhook, if new_chat_members field has a value and the chat_id field indicates that it's from your channel, then you may save the information about the recent users who joined your channel.

How to programmatically pair a bluetooth device

I recently bought a Lilypad Simblee BLE Board and I'd like to pair it programmatically to my computer (using the 32feet.NET library in C#).
I'm aware the "How to programmatically pair a bluetooth device" has already been asked on StackOverflow (here for example), however for some reason, all my attempts to pair the device programmatically have failed. Indeed, I successfully paired the device with the "Manage Bluetooth devices" window in Windows 10 Settings panel (Settings > Devices > Bluetooth).
Firstly, I don't know the pairing method (either legacy or SSP) to use with my device. Windows never asked me for a PIN or something, so I guess it's SSP, but I'm unsure.
I searched on Google how to do a SSP pairing request with 32feet.NET: I found this.
However, once it discovered my device (the device discovery works properly), the pairing request instantly fails.
My code:
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
namespace HLK_Client
{
class HLKBoard
{
public event HLKBoardEventHandler HLKBoardConnectionComplete;
public delegate void HLKBoardEventHandler(object sender, HLKBoardEventArgs e);
private BluetoothClient _bluetoothClient;
private BluetoothComponent _bluetoothComponent;
private List<BluetoothDeviceInfo> _inRangeBluetoothDevices;
private BluetoothDeviceInfo _hlkBoardDevice;
private EventHandler<BluetoothWin32AuthenticationEventArgs> _bluetoothAuthenticatorHandler;
private BluetoothWin32Authentication _bluetoothAuthenticator;
public HLKBoard()
{
_bluetoothClient = new BluetoothClient();
_bluetoothComponent = new BluetoothComponent(_bluetoothClient);
_inRangeBluetoothDevices = new List<BluetoothDeviceInfo>();
_bluetoothAuthenticatorHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(_bluetoothAutenticator_handlePairingRequest);
_bluetoothAuthenticator = new BluetoothWin32Authentication(_bluetoothAuthenticatorHandler);
_bluetoothComponent.DiscoverDevicesProgress += _bluetoothComponent_DiscoverDevicesProgress;
_bluetoothComponent.DiscoverDevicesComplete += _bluetoothComponent_DiscoverDevicesComplete;
}
public void ConnectAsync()
{
_inRangeBluetoothDevices.Clear();
_hlkBoardDevice = null;
_bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, false, null);
}
private void PairWithBoard()
{
Console.WriteLine("Pairing...");
bool pairResult = BluetoothSecurity.PairRequest(_hlkBoardDevice.DeviceAddress, null);
if (pairResult)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine("Fail"); // Instantly fails
}
}
private void _bluetoothComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
_inRangeBluetoothDevices.AddRange(e.Devices);
}
private void _bluetoothComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
for (int i = 0; i < _inRangeBluetoothDevices.Count; ++i)
{
if (_inRangeBluetoothDevices[i].DeviceName == "HLK")
{
_hlkBoardDevice = _inRangeBluetoothDevices[i];
PairWithBoard();
return;
}
}
HLKBoardConnectionComplete(this, new HLKBoardEventArgs(false, "Didn't found any \"HLK\" discoverable device"));
}
private void _bluetoothAutenticator_handlePairingRequest(object sender, BluetoothWin32AuthenticationEventArgs e)
{
e.Confirm = true; // Never reach this line
}
}
}
Why does the pairing request fail?
The answer to the question you linked has a plausible suggestion... did you read it?
Also you should look at this question as well.
32feet library is built around legacy pairing, so that you either need to know the pin of the device you are connecting to, or you supply it with a null to get a popup window to enter a pin.
It also says that the windows function used by 32feet is deprecated in newer versions of windows. If that's true, the reason it's failing instantly is because you've passed a null pin in your pairing request and for it to proceed windows needs to show a dialog which no longer exists.
What happens if you try to connect with the pin "0000" or "1234" ?
I'm looking at the source code of WindowsBluetoothSecurity.cs in 32feet.net and I see if a pairing request fails, it logs the error code to Debug.WriteLine, any chance you could post that error code here?
One good work around to this problem might be to import BluetoothAuthenticateDeviceEx and use that manually to complete the pairing request. If you don't want to do this manually, it looks like in the latest version of the 32feet source, there is actually a SSP pairing method that utilises this method but it's not public and it's not used anywhere so you'll need to access it via reflection:
typeof(BluetoothSecurity)
.GetMethod("PairRequest", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(null, new object[] { _hlkBoardDevice.DeviceAddress, BluetoothAuthenticationRequirements.MITMProtectionNotRequired });

LiveConnectClient missing eventhandlers Live SDK 5.3 WP8

hi there :) il get right to it.
Problem :
when i try to instanciate LiveConnectClient and then try to access the event : GetCompleted
which supose to be in the LiveConnectClient is not showing and on all the examples i been looking at even those on here are using it. this is not the only class this is happening on it is also happening on LiveAuthClient as well no events even the post on the net says there should be.
i tried to reinstall Vs2012 and sdk wp8 and live sdk from scratch but have not solved it
for refrence i using this example to see if i can it to work :
//event triggered when Skydrive sign in status is changed
private void btnSignIn_SessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
{
//if the user is signed in
if (e.Status == LiveConnectSessionStatus.Connected)
{
session = e.Session;
client = new LiveConnectClient(e.Session);
infoTextBlock.Text = "Accessing SkyDrive...";
//get the folders in their skydrive
client.GetCompleted +=
new EventHandler<LiveOperationCompletedEventArgs>(btnSignin_GetCompleted);
client.GetAsync("me/skydrive/files?filter=folders,albums");
}
//otherwise the user isn't signed in
else
{
infoTextBlock.Text = "Not signed in.";
client = null;
}
}
i got no luck solving it and running out of ideas. So im hoping one of u boys out there can shed some light on it or lend a hand with dew wise words :)
thanks in advance. and i do apologies if this is to long a post.
regards jens
Indeed, it seems like those events have been removed in the latest versions of the SDK. You don't need them though, thanks to the async/await keywords. First, mark your method as async, then call the GetAsync method with the await keyword. And place afterward the code you would normally put in the GetCompleted event:
private async void btnSignIn_SessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
{
//if the user is signed in
if (e.Status == LiveConnectSessionStatus.Connected)
{
session = e.Session;
client = new LiveConnectClient(e.Session);
infoTextBlock.Text = "Accessing SkyDrive...";
//get the folders in their skydrive
var result = await client.GetAsync("me/skydrive/files?filter=folders,albums");
// Do here what you would normally do in btnSignin_GetCompleted
}
//otherwise the user isn't signed in
else
{
infoTextBlock.Text = "Not signed in.";
client = null;
}
}

Categories