This commit is contained in:
Ilya Pirozhneko 2016-02-07 13:28:41 +03:00
parent 84cbe0afc8
commit 103e41fb2b
8 changed files with 91 additions and 80 deletions

View file

@ -110,12 +110,12 @@ Get user id by phone number.
_Example_: _Example_:
``` ```
var res = await client.ImportContact(phoneNumber); var res = await client.ImportContactByPhoneNumber("791812312323");
``` ```
* phoneNumber - **string**, phone number in international format (eg. 791812312323) * phoneNumber - **string**, phone number in international format (eg. 791812312323)
**Returns**: **int?**, User Id with phoneNumber **Returns**: **int?**, User Id or null if no such user.
####Get Contact By Username ####Get Contact By Username
Get user id by userName. Get user id by userName.
@ -128,7 +128,7 @@ var res = await client.ImportByUserName(userName);
* userName - **string**, user name (eg. telegram_bot) * userName - **string**, user name (eg. telegram_bot)
**Returns**: **int?**, User Id with phoneNumber **Returns**: **int?**, User Id or null if no such user.
####Send Message To Contact ####Send Message To Contact
Send text message to specified user Send text message to specified user
@ -179,10 +179,59 @@ var hist = await client.GetMessagesHistoryForContact(userId, offset, limit);
Contributing is highly appreciated! Contributing is highly appreciated!
###How to add new functions ###How to add new functions
It's really simple to add new functionality to TLSharp.
###What things can I Implement? Adding new functions is easy.
TODO:
* Just create a new Request class in Requests folder.
* Derive it from MTProtoRequest.
Requests specification you can find in [Telegram API](link) reference.
_Example_:
```
public class ExampleRequest : MTProtoRequest
{
private int _someParameter;
// pass needed parameters through constructor, and save it to private vars
public InitConnectionRequest(int someParameter)
{
_someParameter = someParameter;
}
// send all needed params to Telegram
public override void OnSend(BinaryWriter writer)
{
writer.Write(_someParameter);
}
// read a received data from Telegram
public override void OnResponse(BinaryReader reader)
{
_someParameter = reader.ReadUInt32();
}
public override void OnException(Exception exception)
{
throw new NotImplementedException();
}
public override bool Responded { get; }
public override bool Confirmed => true;
}
```
More advanced examples you can find in [Requests folder](link).
###What things can I Implement (Project Roadmap)?
* Factor out current TL language implementation, and use [this one](link)
* Add possibility to get current user Chats and Users
* Fix Chat requests (Create, AddUser)
* Add Updates handling
* Add possibility to work with Channels
# FAQ # FAQ
@ -201,6 +250,17 @@ It's Telegram restrictions. See [this](https://core.telegram.org/api/errors#420-
Now TLSharp is basic realization of Telegram protocol, you can be a contributor or a sponsor to speed-up developemnt of any feature. Now TLSharp is basic realization of Telegram protocol, you can be a contributor or a sponsor to speed-up developemnt of any feature.
#### Nothing helps
Create an issue in project bug tracker.
**Attach this information**:
* Full problem description and exception message
* Stack-trace
* Your code that runs in to this exception
Without information listen above your issue will be closed.
# License # License
**Please, provide link to an author when you using library** **Please, provide link to an author when you using library**

View file

@ -1,66 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using TLSharp.Core.MTProto;
namespace TLSharp.Core.Requests
{
public class ImportContactRequest : MTProtoRequest
{
private List<InputContact> Contact { get; set; }
private bool Replace { get; set; }
public List<ImportedContact> imported;
public List<User> users;
public ImportContactRequest(List<InputContact> contact, bool shouldReplace = true)
{
Contact = contact;
Replace = shouldReplace;
}
public override void OnSend(BinaryWriter writer)
{
writer.Write(0xda30b32d);
writer.Write(0x1cb5c415);
writer.Write(Contact.Count);
foreach (var item in Contact)
{
item.Write(writer);
}
writer.Write(Replace ? 0x997275b5 : 0xbc799737);
}
public override void OnResponse(BinaryReader reader)
{
var code = reader.ReadUInt32();
var result = reader.ReadInt32(); // vector code
int imported_len = reader.ReadInt32();
this.imported = new List<ImportedContact>(imported_len);
for (int imported_index = 0; imported_index < imported_len; imported_index++)
{
ImportedContact imported_element;
imported_element = TL.Parse<ImportedContact>(reader);
this.imported.Add(imported_element);
}
reader.ReadInt32(); // vector code
int users_len = reader.ReadInt32();
this.users = new List<User>(users_len);
for (int users_index = 0; users_index < users_len; users_index++)
{
User users_element;
users_element = TL.Parse<User>(reader);
this.users.Add(users_element);
}
}
public override void OnException(Exception exception)
{
throw new NotImplementedException();
}
public override bool Confirmed { get { return true; } }
private readonly bool responded;
public override bool Responded { get { return responded; } }
}
}

View file

@ -85,6 +85,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
<Compile Include="Requests\CreateChatRequest.cs" />
<None Include="Requests\AddChatUserRequest.cs" />
<None Include="Requests\DeleteChatUserRequest.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using TLSharp.Core.Auth; using TLSharp.Core.Auth;
using TLSharp.Core.MTProto; using TLSharp.Core.MTProto;
@ -183,7 +184,7 @@ namespace TLSharp.Core
return inputFile; return inputFile;
} }
public async Task<Boolean> SendMediaMessage(int contactId, InputFile file) public async Task<bool> SendMediaMessage(int contactId, InputFile file)
{ {
var request = new Message_SendMediaRequest( var request = new Message_SendMediaRequest(
new InputPeerContactConstructor(contactId), new InputPeerContactConstructor(contactId),
@ -195,8 +196,11 @@ namespace TLSharp.Core
return true; return true;
} }
public async Task<int?> ImportContact(string phoneNumber) public async Task<int?> ImportContactByPhoneNumber(string phoneNumber)
{ {
if (!validateNumber(phoneNumber))
throw new InvalidOperationException("Invalid phone number. It should be only digit string, from 5 to 20 digits.");
var request = new ImportContactRequest(new InputPhoneContactConstructor(0, phoneNumber, "My Test Name", String.Empty)); var request = new ImportContactRequest(new InputPhoneContactConstructor(0, phoneNumber, "My Test Name", String.Empty));
await _sender.Send(request); await _sender.Send(request);
await _sender.Recieve(request); await _sender.Recieve(request);
@ -208,6 +212,9 @@ namespace TLSharp.Core
public async Task<int?> ImportByUserName(string username) public async Task<int?> ImportByUserName(string username)
{ {
if (string.IsNullOrEmpty(username))
throw new InvalidOperationException("Username can't be null");
var request = new ImportByUserName(username); var request = new ImportByUserName(username);
await _sender.Send(request); await _sender.Send(request);
await _sender.Recieve(request); await _sender.Recieve(request);
@ -231,5 +238,12 @@ namespace TLSharp.Core
return request.messages; return request.messages;
} }
private bool validateNumber(string number)
{
var regex = new Regex("^\\d{7,20}$");
return regex.IsMatch(number);
}
} }
} }

View file

@ -65,7 +65,7 @@ namespace TLSharp.Tests
} }
[TestMethod] [TestMethod]
public async Task ImportContact() public async Task ImportContactByPhoneNumber()
{ {
// User should be already authenticated! // User should be already authenticated!
@ -76,7 +76,7 @@ namespace TLSharp.Tests
Assert.IsTrue(client.IsUserAuthorized()); Assert.IsTrue(client.IsUserAuthorized());
var res = await client.ImportContact(NumberToSendMessage); var res = await client.ImportContactByPhoneNumber(NumberToSendMessage);
Assert.IsNotNull(res); Assert.IsNotNull(res);
} }
@ -114,7 +114,7 @@ namespace TLSharp.Tests
} }
[TestMethod] [TestMethod]
public async Task SendMessage() public async Task ImportContactByPhoneNumberAndSendMessage()
{ {
// User should be already authenticated! // User should be already authenticated!
@ -124,7 +124,7 @@ namespace TLSharp.Tests
Assert.IsTrue(client.IsUserAuthorized()); Assert.IsTrue(client.IsUserAuthorized());
var res = await client.ImportContact(NumberToSendMessage); var res = await client.ImportContactByPhoneNumber(NumberToSendMessage);
Assert.IsNotNull(res); Assert.IsNotNull(res);
@ -140,7 +140,7 @@ namespace TLSharp.Tests
Assert.IsTrue(client.IsUserAuthorized()); Assert.IsTrue(client.IsUserAuthorized());
var res = await client.ImportContact(NumberToSendMessage); var res = await client.ImportContactByPhoneNumber(NumberToSendMessage);
Assert.IsNotNull(res); Assert.IsNotNull(res);
@ -158,7 +158,7 @@ namespace TLSharp.Tests
Assert.IsTrue(client.IsUserAuthorized()); Assert.IsTrue(client.IsUserAuthorized());
var res = await client.ImportContact(NumberToSendMessage); var res = await client.ImportContactByPhoneNumber(NumberToSendMessage);
Assert.IsNotNull(res); Assert.IsNotNull(res);
const string testFile = "TEST"; const string testFile = "TEST";