如何使用asp.net和c#发送电报信息?

时间:2017-09-28 09:38:02

标签: c# asp.net telegram

我用c#编写了一个asp代码,用于向手机号码发送消息。我有一个有效的api_id和api_hash。该程序在C#windows窗体应用程序中运行良好。但是,我在bool isConnected = await telegram.ConnectAsync();上使用C#在ASP.net上遇到了一个新错误。

错误消息是:

  

类型' System.IO.FileNotFoundException'的例外情况发生在mscorlib.dll但未在用户代码中处理。   附加信息:无法加载文件或程序集' Ionic.ZLib,Version = 2.0.0.14,Culture = neutral,PublicKeyToken = null'或其中一个依赖项。系统找不到指定的文件。

如何解决此类错误?

ASP.Net:

    %@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Mobile Number"></asp:Label>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Message"></asp:Label>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    <div>

    </div>
    </form>
</body>
</html>

C#:

using System;
using System.Linq;
using TLSharp.Core;
using TeleSharp.TL;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected async void Button1_Click(object sender, EventArgs e)
    {
        string Alert="";
        string phoneNumber = "*********";
        int api_id = ******; string api_hash = "********************";
        var store = new FileSessionStore();
        TLUser user = null;
        var client = new TelegramClient(api_id, api_hash, store);
        bool isConnected = await client.ConnectAsync();
        if (!isConnected)
            Alert = "Check wifi connection!";
        while (!client.IsUserAuthorized())
        {
            var phoneCodeHash = await client.SendCodeRequestAsync(phoneNumber);
            Alert = "Input verification code on your phone here:";
            var code = "*****"; // you can change code in debugger, code will send via telegram to you
            user = await client.MakeAuthAsync(phoneNumber, phoneCodeHash, code);
        }
        string[] phonelist = new string[2];

        if (client.IsUserAuthorized())
        {
            //get available contacts
            var result = await client.GetContactsAsync();

            //find recipient in contacts
            var userr = result.users.lists.Where(x => x.GetType() == typeof(TLUser)).Cast<TLUser>().FirstOrDefault(x => x.phone == "98"+TextBox1.Text);

            //send message
            await client.SendMessageAsync(new TLInputPeerUser() { user_id = userr.id }, TextBox2.Text);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Asp.net网页和代码文件是在用户首次请求资源时动态编译的。

因此,在您的代码中,您引用了第三方程序集。 IDE中的所有内容都将正确验证。但在实际使用中,第三方程序集引用另一个程序集。在这种情况下,Ionic.ZLib组件。

添加缺少的程序集的最简单方法是使用Ionic.Zlib Nuget package

Package Manager
PM>  Install-Package Ionic.Zlib -Version 1.9.1.5 
>  dotnet add package Ionic.Zlib --version 1.9.1.5