add console projects

This commit is contained in:
meysam navaei 2018-02-06 00:57:15 +03:30
parent a3b58d6bb0
commit d86d7609e0
13 changed files with 760 additions and 70 deletions

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ApiHash" value="d4a1f5268c9af7bc69cc336a443b4ac4" />
<add key="ApiId" value="171836" />
<add key="NumberToAuthenticate" value="989224913689" />
<add key="CodeToAuthenticate" value="" />
<add key="PasswordToAuthenticate" value=""/>
<add key="NotRegisteredNumberToSignUp" value=""/>
<add key="NumberToSendMessage" value="989128702514"/>
<add key="UserNameToSendMessage" value="getmedia_contact"/>
<add key="NumberToGetUserFull" value=""/>
<add key="NumberToAddToChat" value="989128702514"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>

View file

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{15296FBD-23B9-4786-8CC0-65C872FBFDAC}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ClientConsoleApp</RootNamespace>
<AssemblyName>ClientConsoleApp</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\TeleSharp.TL\TeleSharp.TL.csproj">
<Project>{d6144517-91d2-4880-86df-e9ff5d7f383a}</Project>
<Name>TeleSharp.TL</Name>
</ProjectReference>
<ProjectReference Include="..\..\TLSharp.Core\TLSharp.Core.csproj">
<Project>{400d2544-1cc6-4d8a-a62c-2292d9947a16}</Project>
<Name>TLSharp.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TeleSharp.TL;
using TLSharp.Core;
namespace ClientConsoleApp
{
class Program
{
private static string NumberToSendMessage => ConfigurationManager.AppSettings[nameof(NumberToSendMessage)];
static void Main(string[] args)
{
System.Threading.Thread.Sleep(2000);
Console.WriteLine("Hello World!");
var client = GetTlgClient().Result;
var normalizedNumber = NumberToSendMessage.StartsWith("+") ?
NumberToSendMessage.Substring(1, NumberToSendMessage.Length - 1) :
NumberToSendMessage;
var result = client.GetContactsAsync().Result;
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Phone == normalizedNumber);
var rr = client.SendTypingAsync(new TLInputPeerUser() { UserId = user.Id }).Result;
Thread.Sleep(3000);
var rrr = client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, "TEST").Result;
}
private static async Task<TLSharp.Core.TelegramClient> GetTlgClient()
{
string ApiHash = ConfigurationManager.AppSettings["ApiHash"];
int ApiId = int.Parse(ConfigurationManager.AppSettings["ApiId"]);
//string AuthCode = ConfigurationManager.AppSettings["AuthCode"];
string MainPhoneNumber = ConfigurationManager.AppSettings["NumberToAuthenticate"];
var client = new TelegramClient(ApiId, ApiHash, null, "session", null, "127.0.0.1", 5000);
var phoneCodeHash = string.Empty;
authenticated:
Console.WriteLine("Start app");
client.ConnectAsync().Wait();
Console.WriteLine("MakeAuthentication");
await MakeAuthentication(client, MainPhoneNumber);
Console.WriteLine("Connected");
if (!client.IsUserAuthorized())
{
Console.WriteLine("User not autorized");
if (string.IsNullOrEmpty(phoneCodeHash))
{
phoneCodeHash = await client.SendCodeRequestAsync(MainPhoneNumber);
goto authenticated;
}
Console.WriteLine("Plase enter new AuthCode:");
var AuthCode = Console.ReadLine();
var user = await client.MakeAuthAsync(MainPhoneNumber, phoneCodeHash, AuthCode);
Console.WriteLine("Login successfull.");
}
return client;
}
private static async Task MakeAuthentication(TLSharp.Core.TelegramClient client, string mainPhoneNumber)
{
var hash = await client.SendCodeRequestAsync(mainPhoneNumber);
Console.WriteLine("waiting for code");
var code = Console.ReadLine();
var user = await client.MakeAuthAsync(mainPhoneNumber, hash, code);
if (!client.IsUserAuthorized())
{
hash = await client.SendCodeRequestAsync(mainPhoneNumber);
Console.WriteLine("please try again.add code");
code = Console.ReadLine();
user = await client.MakeAuthAsync(mainPhoneNumber, hash, code);
}
}
private static void TestTcpClient()
{
var tcpClient = new TcpClient();
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000));
if (tcpClient.Connected)
{
using (var memoryStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write((long)0);
binaryWriter.Write(1234);
binaryWriter.Write(20);
binaryWriter.Write(8000);
byte[] packet = memoryStream.ToArray();
tcpClient.GetStream().WriteAsync(packet, 0, packet.Length).Wait();
}
}
}
System.Threading.Thread.Sleep(20000);
}
}
}

View file

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ClientConsoleApp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClientConsoleApp")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("15296fbd-23b9-4786-8cc0-65c872fbfdac")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]